Number Game

Count the pairs in the given rectangle from which the first player wins the subtraction game where moving to zero or below loses.

Medium7Game theoryNumber theoryRecursionMathNo attempts yetTime limit5sMemory limit512 MB

Problem

Arya and Bran play a game. A blackboard starts with two positive integers AA and BB written on it. The players take turns and Arya goes first. On a turn a player picks a positive integer kk and replaces AA with Ak×BA - k \times B, or replaces BB with Bk×AB - k \times A. The player who makes one of the two numbers drop to zero or below loses.

If the numbers start at (12,51)(12, 51), one possible game runs like this.

  • Arya replaces 51 with 513×12=1551 - 3 \times 12 = 15, leaving (12,15)(12, 15) on the blackboard.
  • Bran replaces 15 with 151×12=315 - 1 \times 12 = 3, leaving (12,3)(12, 3).
  • Arya replaces 12 with 123×3=312 - 3 \times 3 = 3, leaving (3,3)(3, 3).
  • Bran replaces one 3 with 31×3=03 - 1 \times 3 = 0 and loses.

Call (A,B)(A, B) a winning position when Arya wins every game that starts from it, no matter how Bran plays.

You are given four integers A1A_1, A2A_2, B1B_1, B2B_2. Count the winning positions (A,B)(A, B) with A1AA2A_1 \le A \le A_2 and B1BB2B_1 \le B \le B_2.

Input

The first line contains the number of test cases TT. Each of the next TT lines holds one test case: the integers A1A_1, A2A_2, B1B_1, B2B_2, separated by spaces.

Limits

  • 1T1001 \le T \le 100
  • 1A1A21061 \le A_1 \le A_2 \le 10^6
  • 1B1B21061 \le B_1 \le B_2 \le 10^6

Output

For each test case print one line of the form Case #x: y, where xx is the case number starting from 1 and yy is the number of winning positions (A,B)(A, B) with A1AA2A_1 \le A \le A_2 and B1BB2B_1 \le B \le B_2.