Toys

Count the number of distinct toys formed by strings joining n evenly spaced clamps on one disc to m on another, where rotating each disc independently gives the same toy, modulo 1,000,000,007.

Hard9CombinatoricsNumber theoryMathBit manipulationNo attempts yetTime limit2sMemory limit512 MB

Problem

The head judge builds one toy out of two discs, a red one and a blue one. An axis runs through the centers of both discs, and each disc turns around that axis on its own.

The red disc has nn clamps spaced evenly around its edge, and the blue disc has mm. A clamp on the red disc can be joined to a clamp on the blue disc by a flexible string. A clamp can carry several strings, but two clamps are joined by at most one string.

For given nn and mm, count the different toys the head judge can build, modulo 1,000,000,007. Two toys are the same when turning the discs around the axis makes one of them look exactly like the other, and the two turning angles do not have to be equal. Only the pair of clamps that a string joins matters, not the path the string takes through the space between the discs.

In the figure below, (a) and (b) are the same toy. Turn the red disc on the left one step counterclockwise and the blue disc on the right four steps counterclockwise, and (a) becomes (b). (c) is a different toy.

Three examples of the two disc toy

Input

The first line contains the number of data sets PP (1P10001 \le P \le 1000).

Each of the next PP lines holds one data set: the data set number KK, then the integers nn and mm, separated by spaces (2n,m1072 \le n, m \le 10^7). The data sets are independent of each other and are processed the same way.

Output

For each data set print one line: the data set number KK, a single space, then the number of different toys for that nn and mm, modulo 1,000,000,007.