#arc127f. [arc127_f]±AB

[arc127_f]±AB

Problem Statement

Given are integers AA, BB, VV, and MM, where AA and BB are guaranteed to be coprime. Additionally, you have an integer xx. Initially, x=Vx=V.

You can do the following four kinds of operations any number of times in any order.

  • Replace the value of xx with x+Ax+A.

  • Replace the value of xx with xAx-A.

  • Replace the value of xx with x+Bx+B.

  • Replace the value of xx with xBx-B.

Here, 0leqxleqM0 \\leq x \\leq M must hold at any moment during this process.

Under this condition, find how many different values xx can take.

For each input file, solve TT test cases.

Constraints

  • 1leqTleq1051 \\leq T \\leq 10^5
  • 1leqA<BleqMleq1091 \\leq A < B \\leq M \\leq 10^9
  • AA and BB are coprime.
  • 0leqVleqM0 \\leq V \\leq M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

TT case1case_1 case2case_2 vdots\\vdots case3case_3

Each case is in the following format:

AA BB VV MM

Output

Print the answer for each case.


Sample Input 1

5
3 5 0 5
1 2 5 10
5 8 4 9
10 99 48 106
500000000 500000001 123456789 900000000

Sample Output 1

4
11
4
10
800000002

In the first case, xx can take four values: x=0,2,3,5x=0,2,3,5.