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(k2l + 1, k2l + 1 + 1, ..., k2r + 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 ki, li, ri, chosen by the Great Mushroom King for this visit. They also know a prime number pi. Help them to count the remainder of dividing the number of dwarves who can see the King, by number pi, for each visit.

Input

The first line contains the single integer t (1 ≤ t ≤ 105) — the number of the King's visits. 

Each of the following t input lines contains four space-separated integers ki, li, ri and pi (1 ≤ ki ≤ 106; 0 ≤ li ≤ ri ≤ 1018; 2 ≤ pi ≤ 109) — the numbers, chosen by the Great Mushroom King and the prime module, correspondingly. 

It is guaranteed that for all visits number pi 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 pi. 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(a1, a2, ..., an) represents the least common multiple of numbers a1, a2, ..., an.

We consider that x0 = 1, for any x.
#include <bits/stdc++.h>
using namespace std;
long long calc(const long long &x, const long long &pow, const long long &pp) {
  long long res = 1, tmp = (x % pp);
  long long cur = pow;
  while (cur > 0) {
    if (cur % 2 == 0) {
      tmp = (tmp * tmp) % pp;
      cur = cur / 2;
    } else {
      res = (res * tmp) % pp;
      cur = cur - 1;
    }
  }
  return res;
}
int obr() {
  long long k, l, r, p;
  cin >> k >> l >> r >> p;
  long long ans;
  if (p == 2) {
    cout << (1 - (k % 2)) << endl;
    return 0;
  }
  if (k == 1)
    ans = 2 % p;
  else {
    long long st_l = calc(2, l, p - 1), st_r = calc(2, r + 1, p - 1);
    long long rev_2 = (p + 1) / 2;
    long long ch = (calc(k, st_r, p) + p - 1) % p;
    long long zn = (calc(k, st_l, p) + p - 1) % p;
    if (k % p == 0) zn = ch = p - 1;
    if (zn == 0) {
      ch = calc(2, r - l + 1, p);
    } else {
      zn = calc(zn, p - 2, p);
      ch = (ch * zn) % p;
    }
    if (k % 2) {
      long long to_div = calc(rev_2, r - l, p);
      ch = (ch * to_div) % p;
    }
    ans = ch;
  }
  cout << ans << endl;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int i = 1; i <= t; ++i) obr();
  return 0;
}
