// Arup Guha
// 8/17/2014
// Solution to 2014 UCF Locals Problem: Super Lucky Palindromes

import java.util.*;
import java.io.*;

public class lucky {

	final public static long TOO_BIG = -1L;
	final public static long MAX = 1000000000000000000L;
	final public static int[] LEN = {4, 7, 44, 47, 74, 77, 444};
	final public static int[][] ALL_NUM_4 = {{0,4},
											 {0,3,4,7},
											 {0,4,40,44},
											 {0,3,4,7,40,43,44,47},
											 {0,4,30,44,70,74},
											 {0,3,4,7,30,33,44,47,70,73,74,77},
											 {0,4,44,74,370,400,440,444}};

	// Generated by running go method with empty string input.
	final public static long[] NUMANS = {2L,8L,464L,4096L,18728400854L,75422540336L};

	public static long[][] combo;


	public static void main(String[] args) throws Exception {

		// Pascal's triangle is helpful to us - store -1 for anything over 10^18.
		combo = new long[445][445];
		for (int i=0; i<combo.length; i++) {
			combo[i][0] = 1;
			combo[i][i] = 1;
		}
		for (int i=2; i<combo.length; i++) {
			for (int j=1; j<i; j++) {
				if (combo[i-1][j-1] == TOO_BIG || combo[i-1][j] == TOO_BIG)
					combo[i][j] = TOO_BIG;
				else
					combo[i][j] = combo[i-1][j-1] + combo[i-1][j];
				if (combo[i][j] > MAX) combo[i][j] = TOO_BIG;
			}
		}

		Scanner stdin = new Scanner(new File("lucky.in"));
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Get 0-based rank.
			long rank = stdin.nextLong() - 1;

			// Solve and print.
			System.out.println("Query #"+loop+": "+solve(rank,0));
		}
		stdin.close();
	}

	public static String solve(long rank, int sizeLevel) {

		// Need to move to next bigger level.
		if (sizeLevel < NUMANS.length && rank >= NUMANS[sizeLevel]) return solve(rank-NUMANS[sizeLevel], sizeLevel+1);

		String ans = "";
		int numFours = 0;

		for (int i=0; i<LEN[sizeLevel]/2; i++) {

			// Count # of combos that have 4 in this spot.
			long thisDigitFour = go (ans.length()+1, numFours+1, sizeLevel);

			// Oddly enough, this works,
			if (rank < thisDigitFour || thisDigitFour == TOO_BIG) {
				ans = ans + "4";
				numFours++;
			}
			else	{
				ans = ans + "7";
				rank -= thisDigitFour;
			}
		}

		return fix(ans, LEN[sizeLevel]);
	}

	// Returns the correct super lucky palindrome given the prefix left and that its
	// final length will be length.
	public static String fix(String left, int length) {

		String rev = reverse(left);
		if (length%2 == 0) return left + rev;

		// count 4s so far.
		int numFours = 0;
		for (int i=0; i<left.length(); i++)
			if (left.charAt(i) == '4')
				numFours++;
		numFours *= 2;
		int numSevens = length - 1 - numFours;

		// Easy cases.
		if (in(LEN,numFours)) return left + "7" + rev;
		if (in(LEN,numSevens)) return left + "4" + rev;

		// See if 4 is missing.
		if (in(LEN,numFours+1)) return left + "4" + rev;
		return left + "7" + rev;
	}

	// Returns the reverse of str.
	public static String reverse(String str) {
		char[] ans = new char[str.length()];
		for (int i=0; i<ans.length; i++)
			ans[i] = str.charAt(str.length()-1-i);
		return new String(ans);
	}

	// Returns true iff arr contains val.
	public static boolean in(int[] arr, int val) {
		for (int i=0; i<arr.length; i++)
			if (arr[i] == val)
				return true;
		return false;
	}

	public static long go(int preLen, int curNum4, int lenIndex) {

		int myLen = LEN[lenIndex];
		int left = myLen - preLen*2;

		// Base case - string filled in. Just see if the number of fours can be set to a lucky number.
		if (left < 2) {
			for (int i=0; i<ALL_NUM_4[lenIndex].length; i++) {
				int foursLeft = ALL_NUM_4[lenIndex][i] - 2*curNum4;
				if (foursLeft >= 0 && foursLeft <= left)
					return 1L;
			}
			return 0L;
		}

		// Run recursion - count all settings with this prefix using combinatorics.
		long cnt = 0;
		for (int i=0; i<ALL_NUM_4[lenIndex].length; i++) {

			// Can't do the combo.
			if (2*curNum4 > ALL_NUM_4[lenIndex][i]) continue;

			// Number of fours we must add.
			int foursLeft = ALL_NUM_4[lenIndex][i] - 2*curNum4;

			// "Too many fours to add", so to speak.
			if (foursLeft > left) continue;

			// Number of combos that work to fill in empty slots.
			long toAdd = left > 1 ? combo[left/2][foursLeft/2]: 1;

			// Oops, too big.
			if (toAdd == TOO_BIG) return TOO_BIG;

			// Do it!
			cnt += toAdd;
		}

		return cnt;
	}
}