#include <bits/stdc++.h>
using namespace std;
int Que;
long long k, L, R, mod;
void MOD1(long long &x, const long long &mod) { x = x - x / mod * mod; }
long long MOD2(const long long &x, const long long &mod) {
  return x - x / mod * mod;
}
long long go(const long long &a, long long pw, const long long &M) {
  if (pw == 0) return 1;
  long long ret = 1;
  long long now = a;
  while (pw) {
    if (pw & 1) {
      ret = ret * now;
      MOD1(ret, M);
    }
    now = now * now;
    MOD1(now, M);
    pw >>= 1;
  }
  return MOD2(ret, M);
}
int main() {
  scanf("%d", &Que);
  for (int i = 1; i <= Que; i++) {
    scanf("%d %I64d %I64d %d", &k, &L, &R, &mod);
    if (mod == 2) {
      printf("%d\n", 1 - (k & 1));
      continue;
    }
    if (k == 1) {
      printf("%d\n", 2 % mod);
      continue;
    }
    if (k % 2 == 0) {
      if (MOD2(k, mod) == 0) {
        printf("1\n");
        continue;
      }
      long long base = go(k, go(2, L, mod - 1), mod);
      long long ans;
      if (base == 0)
        ans = 1;
      else if (base == 1)
        ans = go(2, (R - L + 1) % (mod - 1), mod);
      else {
        ans = go(base, go(2, R - L + 1, mod - 1), mod);
        ans--;
        ans = (ans * go(base - 1, mod - 2, mod));
        MOD1(ans, mod);
      }
      printf("%d\n", (int)ans);
    } else {
      if (MOD2(k, mod) == 0) {
        printf("%d\n", go(go(2, (R - L) % (mod - 1), mod), mod - 2, mod));
        continue;
      }
      long long base = go(k, go(2, L, mod - 1), mod);
      long long ans;
      if (base == 0)
        ans = go(go(2, (R - L) % (mod - 1), mod), mod - 2, mod);
      else if (base == 1) {
        long long tmp = go(2, (R - L) % (mod - 1), mod);
        ans = tmp * 2;
        ans *= go(tmp, mod - 2, mod);
        MOD1(ans, mod);
      } else {
        ans = go(base, go(2, R - L + 1, mod - 1), mod);
        ans--;
        ans = ans * go(base - 1, mod - 2, mod);
        MOD1(ans, mod);
        ans *= go(go(2, (R - L) % (mod - 1), mod), mod - 2, mod);
        MOD1(ans, mod);
      }
      printf("%d\n", (int)ans);
    }
  }
  return 0;
}
