Number Game (Small)

Count the pairs (A, B) in the given rectangle from which the first player wins the subtract-a-multiple game where hitting zero loses.

Medium7Game theoryMathNo attempts yetTime limit5sMemory limit512 MB

Problem

Arya and Bran play a game. At the start, two positive integers AA and BB are written on a blackboard. The players move in turns and Arya moves first. On a turn, a player picks a positive integer kk and either replaces AA with Ak×BA - k \times B, or replaces BB with Bk×AB - k \times A. Whoever first makes one of the two numbers drop to zero or below loses.

For example, if the numbers start at (12,51)(12, 51), the game can go like this.

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

Call a starting position (A,B)(A, B) a winning position if Arya can always win from it, whatever Bran does.

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 holds the number of test cases TT. Each of the next TT lines holds four integers A1A_1, A2A_2, B1B_1, B2B_2 separated by spaces.

Limits

  • 1T1001 \le T \le 100
  • 1A1A21,000,0001 \le A_1 \le A_2 \le 1{,}000{,}000
  • 1B1B21,000,0001 \le B_1 \le B_2 \le 1{,}000{,}000
  • A2A130A_2 - A_1 \le 30
  • B2B130B_2 - B_1 \le 30

Output

For each test case, print one line in the form Case #x: y. Here xx is the test 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.