Codeforces Round 118 (Div. 1)
D
Visit of the Great
Codeforces
2600
原题
← CE →
翻译中…

The Great Mushroom King descended to the dwarves, but not everyone managed to see him. Only the few chosen ones could see the King.

翻译中…

We know that only $*LCM*(*k*^{2^{*l*}} + 1, *k*^{2^{*l* + 1}} + 1, ..., *k*^{2^{*r*}} + 1)$ dwarves can see the Great Mushroom King. Numbers $*k*$, $*l*$, $*r*$ are chosen by the Great Mushroom King himself in some complicated manner which is unclear to common dwarves.

翻译中…

The dwarven historians decided to document all visits of the Great Mushroom King. For each visit the dwarven historians know three integers $*k*_{*i*}$, $*l*_{*i*}$, $*r*_{*i*}$, chosen by the Great Mushroom King for this visit. They also know a prime number $*p*_{*i*}$. Help them to count the remainder of dividing the number of dwarves who can see the King, by number $*p*_{*i*}$, for each visit.

翻译中…

Input

翻译中…

The first line contains the single integer $*t*$ $(1 ≤ *t* ≤ 10^{5})$ — the number of the King's visits.

翻译中…

Each of the following $*t*$ input lines contains four space-separated integers $*k*_{*i*}$, $*l*_{*i*}$, $*r*_{*i*}$ and $*p*_{*i*}$ $(1 ≤ *k*_{*i*} ≤ 10^{6}$; $0 ≤ *l*_{*i*} ≤ *r*_{*i*} ≤ 10^{18}; 2 ≤ *p*_{*i*} ≤ 10^{9})$ — the numbers, chosen by the Great Mushroom King and the prime module, correspondingly.

翻译中…

It is guaranteed that for all visits number $*p*_{*i*}$ is prime.

翻译中…

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

翻译中…

Output

翻译中…

For each visit print the answer on a single line — the remainder of dividing the number of the dwarves who can see the King this time, by number $*p*_{*i*}$. Print the answers for the visits in the order, in which the visits are described in the input.

翻译中…

Note

翻译中…

We consider that $*LCM*(*a*_{1}, *a*_{2}, ..., *a*_{*n*})$ represents the least common multiple of numbers $*a*_{1}$, $*a*_{2}$, $...$, $*a*_{*n*}$.

翻译中…

We consider that $*x*^{0} = 1$, for any $*x*$.

翻译中…

样例

Input #1
2
3 1 10 2
5 0 4 3
Output #1
0
0
InputCopy OutputCopy
题目信息
题号
185D
时间限制
3 s
内存限制
256 MB
标签
数学
数论

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