// Arup Guha
// 3/4/2020
// Solution to 2020 Mercer Programming Contest Problem 6: Pigeon-Hole Proofs

import java.util.*;

public class pigeonhole {

	final public static int NO_SOL = -2;
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
	
		// Process each case.
		for (int loop=0; loop<nC; loop++) {
		
			int n = stdin.nextInt();
			
			// dp[i] will store first digit that achieves this mod.
			int[] dp = new int[n+1];
			Arrays.fill(dp, NO_SOL);
			
			// This is an ending point.
			dp[0] = -1;
			
			// This list will store 3, 30, 300, etc. all mod n.
			int loc = 0, curVal = 3;
			ArrayList<Integer> pow3 = new ArrayList<Integer>();
			pow3.add(curVal);
			
			// Keep on adding new digits till we get a solution.
			while (true) {
				
				// This will store new answers reachable with this current digit location.
				int[] tmp = new int[n+1];
				Arrays.fill(tmp, NO_SOL);
				
				// j is the old mod we are building off of.
				for (int j=0; j<=n; j++) {
					
					// Special case...one digit does it.
					if (curVal == 0) {
						dp[n] = loc;
						break;
					}
					
					// Here is an old case to build off of.
					if (dp[j] != NO_SOL) {
						
						// Adding this term to it gives it the mod of idx.
						int idx = (j + curVal)%n;
						if (idx == 0) idx = n;
						
						// Store this in tmp, not dp, to avoid using a digit twice.
						tmp[idx] = loc;
					}
				}
				
				// Only copy over new results.
				for (int j=0; j<=n; j++)
					if (dp[j] == NO_SOL && tmp[j] != NO_SOL)
						dp[j] = tmp[j];
				
				// Go to next digit.
				loc++;
				curVal = (curVal*10)%n;
				pow3.add(curVal);
				
				// A non-zero solution.
				if (dp[n] >= 0) break;
			}
			
			
			curVal = n;
			int[] res = new int[loc];
			
			// Build back the answer.
			while (true) {
				
				// Set this digit to 3.
				res[dp[curVal]] = 3;
				
				// Get the resulting mod value after subtracing out this digit's contribution.
				curVal = (curVal - pow3.get(dp[curVal])+n)%n;
				
				// Time to get out, we got it to add up.
				if (curVal == 0) break;
			}
			
			// Output as a string, in the correct order.
			char[] ans = new char[res.length];
			for (int i=res.length-1; i>=0; i--)
				ans[res.length-1-i] = (char)('0'+res[i]);
			System.out.println(new String(ans));
		}
	}
}