// Arup Guha
// 9/17/2015
// Solution to 2015 January USACO Bronze Problem: It's All About the Base

import java.util.*;
import java.io.*;

public class whatbase {

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(new File("whatbase.in"));
		int numCases = stdin.nextInt();
		PrintWriter out = new PrintWriter(new File("whatbase.out"));

		// Go through each route.
		for (int loop=0; loop<numCases; loop++) {

			int x = stdin.nextInt();
			int y = stdin.nextInt();
			int y100 = y/100;

			// Search for the base!
			for (int baseX = 10; baseX <=15000; baseX++) {

				// Get the value in this base, exactly.
				int value = getVal(x, baseX);

				// Get relatively small bounds for the other base.
				int highBase = (int)(Math.sqrt(value/y100)) + 1;
				int lowBase = (int)(Math.sqrt(value/(y100+1)));

				// Now just search in this range with y - recall to stay within [10,15000].
				int baseY = -1;
				for (int i=Math.max(10,lowBase); i<=Math.min(highBase,15000); i++) {
					if (getVal(y, i) == value) {
						baseY = i;
						break;
					}
				}

				// Means we got a solution, output it and break out of our search.
				if (baseY != -1) {
					out.println(baseX+" "+baseY);
					break;
				}
			}

		}

		out.close();
	}

	// Returns the decimal equivalent of x written in base base.
	public static int getVal(int x, int base) {
		int res = 0, pow = 1;
		while (x > 0) {
			res = res + (x%10)*pow;
			x /= 10;
			pow *= base;
		}
		return res;
	}
}