185_D. Visit of the Great_5209
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.
import static java.util.Arrays.deepToString;

import java.io.*;
import java.math.*;
import java.util.*;

public class D {

	static int solve(int k, long l, long r, int p) {
		if (p == 2)
			return k % 2 == 0 ? 1 : 0;
		if (k == 1)
			return 2;
		long res = k % 2 == 0 ? 1 : powFast(2, l - r, p);
		long mult = (powTwo(k, l, p) + p - 1) % p;
		if (mult != 0) {
			res = (res * (powTwo(k, r + 1, p) + p - 1)) % p;
			res = (res * (inverse(mult, p))) % p;
		} else {
			res = (res * powFast(2, r - l + 1, p)) % p;
		}
		return (int) res;
	}

	static long inverse(long a, long p) {
		return powFast(a, p - 2, p);
	}

	static long powFast(long a, long n, long p) {
		if (n < 0) {
			return inverse(powFast(a, -n, p), p);
		}
		if (n == 0)
			return 1;
		long t = powFast(a, n / 2, p);
		t = (t * t) % p;
		return n % 2 == 0 ? t : (t * a) % p;
	}

	static long powTwo(long a, long b, long p) {
		return a % p == 0 ? 0 : powFast(a, powFast(2, b, p - 1), p);
	}

	// static long powMod(long a, long n, long mod) {
	// return val(a).modPow(val(n), val(mod)).longValue();
	// }

	static int brute(int k, long l, long r, int p) {
		BigInteger LCM = BigInteger.ONE;
		for (long i = l; i <= r; i++) {
			BigInteger cur = val(k).pow(1 << i).add(BigInteger.ONE);
			LCM = lcm(LCM, cur);
		}
		return LCM.mod(val(p)).intValue();
	}

	static BigInteger lcm(BigInteger a, BigInteger b) {
		return a.multiply(b).divide(a.gcd(b));
	}

	static void maxTest() {
		int p = 1000000007;
		long MX = (long) 1e18;
		Random rng = new Random();
		long l = (rng.nextLong() % MX + MX) % MX, r = (rng.nextLong() % MX + MX) % MX;
		for (int it = 0; it < 100000; it++) {
			if (l > r) {
				long tmp = l;
				l = r;
				r = tmp;
			}
			int res = solve(199999, l, r, p);
			debug(199999, l, r, p, res);
		}
	}

	public static void main(String[] args) throws Exception {
		reader = new BufferedReader(new InputStreamReader(System.in));
		writer = new PrintWriter(System.out);

		int T = nextInt();
		for (int i = 0; i < T; i++) {
			int k = nextInt();
			long L = nextLong(), R = nextLong();
			int p = nextInt();
			int res = solve(k, L, R, p);
			writer.println(res);
		}

		writer.close();
	}

	static BufferedReader reader;
	static PrintWriter writer;
	static StringTokenizer tok = new StringTokenizer("");
	static long systemTime;

	static void debug(Object... o) {
		System.err.println(deepToString(o));
	}

	static void setTime() {
		systemTime = System.currentTimeMillis();
	}

	static void printTime() {
		System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
	}

	static void printMemory() {
		System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
	}

	static String next() {
		while (!tok.hasMoreTokens()) {
			String w = null;
			try {
				w = reader.readLine();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (w == null)
				return null;
			tok = new StringTokenizer(w);
		}
		return tok.nextToken();
	}

	static int nextInt() {
		return Integer.parseInt(next());
	}

	static long nextLong() {
		return Long.parseLong(next());
	}

	static double nextDouble() {
		return Double.parseDouble(next());
	}

	static BigInteger nextBigInteger() {
		return new BigInteger(next());
	}

	static BigInteger val(long x) {
		return BigInteger.valueOf(x);
	}
}