AtCoder Regular Contest 127
F
±AB
AtCoder
⌀ 3723
原题
← E→
翻译中…

Problem Statement

翻译中…

Given are integers $A$, $B$, $V$, and $M$, where $A$ and $B$ are guaranteed to be coprime. Additionally, you have an integer $x$. Initially, $x=V$.

翻译中…

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

翻译中…
  • Replace the value of $x$ with $x+A$.
  • Replace the value of $x$ with $x-A$.
  • Replace the value of $x$ with $x+B$.
  • Replace the value of $x$ with $x-B$.
翻译中…

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

翻译中…

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

翻译中…

For each input file, solve $T$ test cases.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 10^5$
  • $1 \leq A < B \leq M \leq 10^9$
  • $A$ and $B$ are coprime.
  • $0 \leq V \leq M$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

T
case_1
case_2
\vdots
case_3

翻译中…

Each case is in the following format:

翻译中…

A B V M

翻译中…

Output

翻译中…

Print the answer for each case.

翻译中…

样例

Input #1
5
3 5 0 5
1 2 5 10
5 8 4 9
10 99 48 106
500000000 500000001 123456789 900000000
Output #1
4
11
4
10
800000002
In the first case, $x$ can take four values: $x=0,2,3,5$.
题目信息
题号
arc127_f
时间限制
8 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。