Bessie the cow is taking computing classes at her local college, and she recently learned how to write numbers in different bases.
A number written in base B has digit places representing 1, B, B2, B3, and so on from right to left. In the familiar base 10 system the places represent 1, 10, 100, 1000, and so on. The digit sequence 1234 read in base 10 means 1(1000)+2(100)+3(10)+4(1). The same digit sequence read in base 5 means 1(125)+2(25)+3(5)+4(1), which is 194 in base 10. When the base grows, the number represented by a fixed digit sequence grows too. For example, 1234 in base 7 is larger than 1234 in base 6.
In base B each digit ranges from 0 to B−1, so base 10 digits run from 0 to 9 and base 5 digits run from 0 to 4. Bases larger than 10 are allowed. Computer scientists often use base 16, where the letters A to F stand for the digit values 10 to 15. In base 16 the sequence BEEF means 11(4096)+14(256)+14(16)+15, which is 48879 in base 10.
Bases much larger than 10 interest Bessie. She took a number N and wrote it down in two different bases X and Y, both between 10 and 15000. In both cases she got a sequence of 3 digits, and every one of those digits was between 1 and 9. Bessie has since forgotten N, X, and Y. Given only the two 3-digit sequences she wrote down, find the two bases X and Y she used.
Because X and Y can be large, a program that tries every possible pair (X,Y) checks nearly 150002 possibilities and does not finish within the time limit.
The first line contains an integer K. Each of the next K lines holds one test case, made of two 3-digit numbers. The first is a number N written in base X, and the second is the same N written in base Y. N, X, and Y may differ from test case to test case.
Print K lines, one per test case. On each line print X and Y for that test case, separated by a single space. Each test case is guaranteed to have exactly one answer.
The number 8892 written in base 47 is 419, and written in base 35 it is 792.