#ARC111E. [ARC111E] Simple Math 3

[ARC111E] Simple Math 3

Score : 800800 points

Problem Statement

In this problem, you will be given TT test cases for each input.

Given integers AA, BB, CC, and DD, find the number of positive integers ii satisfying the following condition:

  • None of the integers between A+B×iA + B \times i and A+C×iA + C \times i (inclusive) is a multiple of DD.

We can prove from the constraints that the count is finite.

Constraints

  • 1T10,0001 \leq T \leq 10{,}000
  • 1A<D1 \leq A < D
  • 0B<C<D0 \leq B < C < D
  • 2D1082 \leq D \leq 10^8

Input

Input is given from Standard Input in the following format:

TT

A1A_1 B1B_1 C1C_1 D1D_1

::

ATA_T BTB_T CTC_T DTD_T

Output

Print TT lines.

The ii-th line should contain the answer for the ii-th case (AiA_i, BiB_i, CiC_i, DiD_i).

2
3 1 2 5
99 101 103 105
1
25

The pairs (A+B×i,A+C×i)(A + B \times i, A + C \times i) for the first case are listed below. We can see that only i=3i = 3 satisfies the condition.

  • i=1:(4,5)i = 1: (4, 5)
  • i=2:(5,7)i = 2: (5, 7)
  • i=3:(6,9)i = 3: (6, 9)
  • i=4:(7,11)i = 4: (7, 11)
  • i=5:(8,13)i = 5: (8, 13)
  • ::