LUCKYNUM - Lucky Numbers

In some Asian countries, 8 and 6 are considered lucky digits. Any number containing only 8s and 6s is considered lucky number, e.g. 6, 8, 66, 668, 88, 886 …. Nguyen is a student who likes mathematics very much. Nguyen likes lucky numbers but only of the form

S = 8…86…6

where S has at least one digit and the number of 8s or 6s can be zero. Examples of S are 8, 88, 6, 66, 86, 886, 8866 …

Given a positive integer X (1 < X < 10 000), Nguyen wants to find the smallest lucky number S which has at most 200 digits and is divisible by X.

Your task is to write a program to find that number for Nguyen.

Input

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data test, there is one single line containing the integer X.

Output

For each data test, write in one line the smallest lucky number S which is divisible by X. In case there is no such a number which has at most 200 digits, write -1.

Example

Sample Input
4
6
8
43
5	

Sample Output
6
8
86
-1

Được gửi lên bởi:Jimmy
Ngày:2009-01-04
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:ACM Regional, Ho Chi Minh City 2008

hide comments
2021-05-27 18:02:07
Tham khảo: https://vnspoj.github.io/problems/LUCKYNUM
2017-08-01 10:20:05
trâu cũng ac
2017-05-11 17:21:22
mo hinhn bai toan la gi vay
2017-04-17 14:34:47
sub điên cuồng vì đọc thiếu dòng số nhỏ nhất :v
@phanduy16
2017-03-04 12:42:29 xin đừng quên tôi
THAM KHẢO THUẬT TOÁN VÀ CODE TẠI: http://yeulaptrinh.pw/671/luckynum-spoj/
2016-08-18 17:41:25 Sue
1 đấm AC, 0.00s, QHĐ =)))
2016-05-22 04:48:01
QHD cơ bản =)))
2016-04-11 17:44:54
dùng BFS hoặc vét cũng được chỉ có hơn 20k trường hợp thôi à vs lại dùng mod thì ko cần phải vận dụng số lớn đâu :v mk đã AC 0.00 nè
2016-03-01 09:43:48
Tham khảo tại : http://phuotvnspoj.blogspot.com/2016/02/luckynum-lucky-numbers-trong-mot-so.html
2015-11-15 04:24:30
http://tomorrowlandv2.blogspot.com/2015/11/lucky-numbers-ma-bai-luckynum-trong-mot.html
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.