public static void main(String[] args) throws NumberFormatException, IOException {		
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(in.readLine().trim());
		long[] potencias ={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000L};
		for (int i = 0; i < t; i++) {
			TreeSet<Long> arbol = new TreeSet<Long>();
			String s[] = in.readLine().trim().split(" +");
			int h = Integer.parseInt(s[0]);
			long k = Integer.parseInt(s[1]);
			int n = (((""+k).length())-h>=0?(""+k).length()-h:0);
			k=(k/(potencias[n]))==0?(k%(potencias[n])):(k/(potencias[n]));
			long may = k;
			arbol.add(k);
			k *= k;			
			for(n = (((""+k).length()-h>=0)?(""+k).length()-h:0); !arbol.contains(k=(k/(potencias[n]))==0?(k%(potencias[n])):(k/(potencias[n])));){
				arbol.add(k);
				if (may < k)may = k;
				if (may == potencias[n]-1)break;
				k *= k;
				n = (((""+k).length()-h>=0)?(""+k).length()-h:0);
			}
			System.out.println(may);
		}
	}