Under construction

Still being built. Things may change

Birthday Numbers I

Time limit1sMemory limit256 MB

Summary
Find the numerically smallest number made only of digits 3, 5 and 8 whose digits add up to N, or -1 when none exists.
Level

Medium4 of 10

Topics
Greedy, Math
Solved
No attempts yet

Problem

Seunghyun was born on 5 March 1998, so he likes the digits 3, 5 and 8 that appear in his date of birth. He decided to call a positive integer a birthday number when its decimal representation consists only of the digits 3, 5 and 8. For example 3, 533 and 858553 are birthday numbers, while 3518, 1642 and 88808 are not.

Given a positive integer NN, find the smallest birthday number whose digits sum to NN.

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100). Each of the next TT lines contains one positive integer NN (1≤N≤1 000 0001 \le N \le 1\,000\,000).

Output

For each test case, print on its own line the smallest birthday number whose digits sum to NN. If no such number exists, print -1.

Examples4

  1. Example 1

    Input
    3
    11
    16
    12
    
    Expected output
    38
    88
    3333
    
  2. Example 2

    Input
    20
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    Expected output
    -1
    -1
    3
    -1
    5
    33
    -1
    8
    333
    55
    38
    3333
    58
    338
    555
    88
    3338
    558
    388
    5555
    
  3. Example 3

    Input
    4
    1
    2
    4
    7
    
    Expected output
    -1
    -1
    -1
    -1
    
  4. Example 4

    Input
    8
    3
    5
    8
    6
    10
    16
    9
    24
    
    Expected output
    3
    5
    8
    33
    55
    88
    333
    888