Henry is ten years old and has a talent for mathematics. His mathematics teacher, Ahmes, taught him fractions today, and Henry enjoyed computing with them. While playing around he discovered that a fraction smaller than 1 can be written as a sum of several distinct unit fractions. A unit fraction is a fraction whose numerator is 1. Every fraction Henry tried turned out to be a sum of distinct unit fractions. For example, 234 can be written as a sum of two unit fractions, 61+1381.
Henry proudly told Ahmes about the discovery. Ahmes was delighted, praised his young student, and named such a sum of distinct unit fractions a Henry representation. The Henry representation of a fraction ba is a list of distinct unit fractions whose total equals ba. The two of them studied the idea further and proved that every fraction smaller than 1 has a Henry representation. They also found that a Henry representation is not unique. For example, 75=21+51+701=21+61+211=21+71+141 gives three different Henry representations. By the definition, the same unit fraction cannot appear twice, so 32=31+31 is not a Henry representation.
Ahmes and Henry also devised a simple method for finding a Henry representation. Given a fraction ba built from positive integers a and b with a<b, first compute the largest unit fraction x11 with x11≤ba. Then repeat the same process on what is left after subtracting x11 from ba: compute the largest unit fraction x21 with x21≤ba−x11 and subtract it. Repeat until nothing is left. This yields distinct unit fractions x11,x21,x31,… in order, and their sum is exactly ba. Ahmes and Henry proved that the algorithm always terminates and outputs distinct unit fractions summing to ba, that is, a Henry representation.
Ahmes and Henry asked you to implement their algorithm as a computer program. Given a fraction ba smaller than 1, run the algorithm of Ahmes and Henry and print the denominator of the last unit fraction of the Henry representation it produces. For example, if ba=75, the algorithm produces 75=21+51+701, so your program must print 70.
Read the input from standard input. The first line contains the number of test data T as an integer. Each test datum is one line holding two integers a and b (1≤a<b≤10000), which denote the input fraction ba. The integers a and b are coprime. If running the algorithm of Ahmes and Henry on ba produces the unit fractions x11,x21,x31,…,xm1 in this order, you may assume that bx1x2⋯xm−1<231.
Write the output to standard output. For each test datum print exactly one line containing a single integer. That integer must equal the denominator of the last unit fraction of the Henry representation produced by the algorithm of Ahmes and Henry on the input fraction ba.