PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000337. Chuỗi cặp số may mắn (Lucky Chains)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chuỗi cặp số may mắn (Lucky Chains)

Lucky Chains

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A pair of positive integers (x,y)(x,y) is lucky if gcd⁡(x,y)=1\gcd(x,y)=1.

Consider (x,y),(x+1,y+1),(x+2,y+2),…(x,y),(x+1,y+1),(x+2,y+2),\dots. The lucky-chain length is the number of consecutive lucky pairs from the beginning, ending immediately before the first pair whose GCD is greater than 11.

Output 0 when the initial pair is already not lucky, and -1 if every pair in the infinite sequence is lucky.

You are given nn pairs with x<yx<y; their difference stays constant as both numbers increase.

Input

The first line contains nn, followed by nn lines containing pairs xi,yix_i,y_i.

Output

Print nn lines: the lucky-chain length for each pair, or -1 if it is infinite.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le12, 1≤xi<yi≤2001\le x_i<y_i\le200.

  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000, 1≤xi<yi≤1051\le x_i<y_i\le10^5.

  • Subtask 3 (50 points): 1≤n≤1061\le n\le10^6, 1≤xi<yi≤1071\le x_i<y_i\le10^7.

Examples

Example 1

Input

4
5 15
13 37
8 9
10009 20000

Output

0
1
-1
79

Explanation

  • The GCD of (5,15)(5,15) is already 55, so no increment is needed.
  • One increment changes (13,37)(13,37) to (14,38)(14,38), whose GCD is 22.
  • The difference in (8,9)(8,9) stays 11, so no common divisor greater than 11 can appear.
  • The last pair has difference 9991=97⋅1039991=97\cdot103. Its first value, 1000910009, leaves remainder 1818 modulo both primes. The first multiple of 9797 is 7979 increments away and the first multiple of 103103 is 8585 increments away. Thus the answer is 7979.