Package for this problem was not updated by the problem writer or Codeforces administration after we’ve upgraded the judging servers. To adjust the time limit constraint, solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then value 800 ms will be displayed and used to determine the verdict.

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

D. Visit of the Great

time limit per test

3 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputThe 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*^{2l} + 1, *k*^{2l + 1} + 1, ..., *k*^{2r} + 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.

Examples

Input

2

3 1 10 2

5 0 4 3

Output

0

0

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*.

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Mar/21/2018 17:59:45 (d3).

Desktop version, switch to mobile version.

User lists

Name |
---|