Expected value of the greatest common divisor

Each of K values is chosen uniformly from its own interval; find the expected gcd of the K chosen numbers as a fraction mod 1e9+7.

Hard9ProbabilityMathNumber theoryDynamic programmingNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given two sequences AA and BB of length KK. A sequence XX is built from AA and BB: each XiX_i is an integer picked uniformly at random from the integers between AiA_i and BiB_i, inclusive, and every XiX_i is picked independently.

Given AA and BB, write a program that computes the expected value of gcd(X0,X1,,XK1)\gcd(X_0, X_1, \dots, X_{K-1}).

Input

The first line contains the number of test cases TT (1T501 \le T \le 50).

The first line of each test case contains KK (2K52 \le K \le 5). Each of the next KK lines contains AiA_i and BiB_i, separated by a space. (1AiBi2000001 \le A_i \le B_i \le 200000)

Output

For each test case, print one answer on its own line.

Write the expected value as an irreducible fraction P/QP/Q. Print NN (0N109+60 \le N \le 10^9+6) such that P+Q×NP + Q \times N is divisible by 109+710^9+7. If no such NN exists, print 1-1.