Problem: Let a be an array consisting of n numbers. The array's elements are numbered from 1 to n, even is an array consisting of the numerals whose numbers are even in a (eveni = a2i, 1 ≤ 2i ≤ n), odd is an array consisting of the numberals whose numbers are odd in а (oddi = a2i - 1, 1 ≤ 2i - 1 ≤ n). Then let's define the transformation of array F(a) in the following manner:

  * if n > 1, F(a) = F(odd) + F(even), where operation " + " stands for the arrays' concatenation (joining together) 
  * if n = 1, F(a) = a



Let a be an array consisting of n numbers 1, 2, 3, ..., n. Then b is the result of applying the transformation to the array a (so b = F(a)). You are given m queries (l, r, u, v). Your task is to find for each query the sum of numbers bi, such that l ≤ i ≤ r and u ≤ bi ≤ v. You should print the query results modulo mod.

Input

The first line contains three integers n, m, mod (1 ≤ n ≤ 1018, 1 ≤ m ≤ 105, 1 ≤ mod ≤ 109). Next m lines describe the queries. Each query is defined by four integers l, r, u, v (1 ≤ l ≤ r ≤ n, 1 ≤ u ≤ v ≤ 1018).

Please do not use the %lld specificator to read or write 64-bit integers in C++. Use %I64d specificator.

Output

Print m lines each containing an integer — remainder modulo mod of the query result.

Examples

Input

4 5 10000
2 3 4 5
2 4 1 3
1 2 2 4
2 3 3 5
1 3 3 4


Output

0
5
3
3
3


Input

2 5 10000
1 2 2 2
1 1 4 5
1 1 2 5
1 1 1 3
1 2 5 5


Output

2
0
0
1
0

Note

Let's consider the first example. First let's construct an array b = F(a) = F([1, 2, 3, 4]). 

  * Step 1. F([1, 2, 3, 4]) = F([1, 3]) + F([2, 4])
  * Step 2. F([1, 3]) = F([1]) + F([3]) = [1] + [3] = [1, 3]
  * Step 3. F([2, 4]) = F([2]) + F([4]) = [2] + [4] = [2, 4]
  * Step 4. b = F([1, 2, 3, 4]) = F([1, 3]) + F([2, 4]) = [1, 3] + [2, 4] = [1, 3, 2, 4]

Thus b = [1, 3, 2, 4]. Let's consider the first query l = 2, r = 3, u = 4, v = 5. The second and third positions in the array b do not have numbers in the range [4, 5], so the sum obviously equals zero. Let's consider the second query l = 2, r = 4, u = 1, v = 3. The second and third positions have two numbers that belong to the range [1, 3], their sum equals 5.

Solution (in JAVA):

Solution:
import static java.lang.Math.min;

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

public class D implements Runnable {

	void solve() {
		long n = nextLong();
		int m = nextInt();
		int mod = nextInt();
		for (int i = 0; i < m; i++) {
			long l = nextLong() - 1;
			long r = nextLong() - 1;
			long a = nextLong() - 1;
			long b = nextLong() - 1;
			long ans1 = get(n, l, r, min(n - 1, b), mod);
			long ans2 = get(n, l, r, min(n - 1, a - 1), mod);
			// System.err.println(ans1 + " " + ans2);
			long ans = ((ans1 - ans2) % mod + mod) % mod;
			// long stupid = 0;
			// for (long j = l; j <= r; j++) {
			// long e = get(n, j);
			// if (e >= a && e <= b) {
			// stupid += e + 1;
			// stupid %= mod;
			// }
			// }
			// if (stupid != ans) {
			// System.err.println(stupid + " " + ans);
			// throw new AssertionError();
			// }
			out.println(ans);
		}
	}

	static long get(long n, long k) {
		if (n == 1) {
			return k == 0 ? 0 : Long.MAX_VALUE;
		}
		long mid = (n + 1) >> 1;
		if (k >= mid) {
			return get(n - mid, k - mid) * 2 + 1;
		} else {
			return get(mid, k) * 2;
		}
	}

	static int get(long n, long l, long r, long x, int mod) {
		if (x < 0) {
			return 0;
		}
		// System.err.println("X = " + x);
		// long a = get1(n, r, x, mod);
		// long b = get1(n, l - 1, x, mod);
		// long c = get2(n, r, x, mod);
		// long d = get2(n, l - 1, x, mod);
		// System.err.println(a + " " + b + " " + c + " " + d);
		// return (int) (((a - b + c - d) % mod + mod) % mod);
		long ret = 0;
		get3(n, r, x, mod);
		ret += A + B;
		get3(n, l - 1, x, mod);
		ret -= A + B;
		return (int) (((ret % mod) + mod) % mod);
	}

	static long A;
	static long B;

	static void get3(long n, long x, long y, int mod) {
		if (y < 0 || x < 0) {
			A = 0;
			B = 0;
			return;
		}
		if (n == 1) {
			A = 0;
			B = 1;
			return;
		}
		long mid = (n + 1) >> 1;
		if (x < mid) {
			get3(mid, x, y >> 1, mod);
			A = (A * 2) % mod;
		} else {
			get3(n - mid, x - mid, (y - 1) >> 1, mod);
			A = (f(y, mod) + A * 2 + B) % mod;
			B = (g(y, mod) + B) % mod;
		}
	}

	static long get1(long n, long x, long y, int mod) {
		if (y < 0 || x < 0) {
			return 0;
		}
		if (n == 1) {
			return 0;
		}
		long mid = (n + 1) >> 1;
		if (x < mid) {
			return (get1(mid, x, y >> 1, mod) * 2) % mod;
		} else {
			return ((f(y, mod) + get1(n - mid, x - mid, (y - 1) >> 1, mod) * 2 + get2(
					n - mid, x - mid, (y - 1) >> 1, mod)) % mod);
		}
	}

	static long get2(long n, long x, long y, int mod) {
		if (y < 0 || x < 0) {
			return 0;
		}
		if (n == 1) {
			return 1;
		}
		long mid = (n + 1) >> 1;
		if (x < mid) {
			return get2(mid, x, y >> 1, mod);
		} else {
			return (g(y, mod) + get2(n - mid, x - mid, (y - 1) >> 1, mod))
					% mod;
		}
	}

	static long g(long x, int mod) {
		if ((x & 1) == 1) {
			--x;
		}
		return (((x >> 1) + 1) % mod);
	}

	static long f(long x, int mod) {
		if ((x & 1) == 1) {
			--x;
		}
		x >>= 1;
		long y = x % mod;
		long z = ((long) mod * (mod + 1) >> 1) % mod;
		z = (z * ((x / mod) % mod)) % mod;
		return ((y * (y + 1) + z * 2) % mod);
	}

	FastScanner sc;
	PrintWriter out;

	public void run() {
		Locale.setDefault(Locale.US);
		try {
			File file = new File("d.in");
			if (file.canRead()) {
				System.setIn(new FileInputStream(file));
			}
			sc = new FastScanner(System.in);
			out = new PrintWriter(System.out);
			solve();
			sc.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	int nextInt() {
		return sc.nextInt();
	}

	String nextToken() {
		return sc.nextToken();
	}

	long nextLong() {
		return sc.nextLong();
	}

	double nextDouble() {
		return sc.nextDouble();
	}

	BigInteger nextBigInteger() {
		return sc.nextBigInteger();
	}

	class FastScanner extends BufferedReader {
		StringTokenizer st;
		boolean eof;
		String buf;
		String curLine;
		boolean createST;

		public FastScanner(String fileName) throws FileNotFoundException {
			this(fileName, true);
		}

		public FastScanner(String fileName, boolean createST)
				throws FileNotFoundException {
			super(new FileReader(fileName));
			this.createST = createST;
			nextToken();
		}

		public FastScanner(InputStream stream) {
			this(stream, true);
		}

		public FastScanner(InputStream stream, boolean createST) {
			super(new InputStreamReader(stream));
			this.createST = createST;
			nextToken();
		}

		String nextLine() {
			String ret = curLine;
			if (createST) {
				st = null;
			}
			nextToken();
			return ret;
		}

		String nextToken() {
			if (!createST) {
				try {
					curLine = readLine();
				} catch (Exception e) {
					eof = true;
				}
				return null;
			}
			while (st == null || !st.hasMoreTokens()) {
				try {
					curLine = readLine();
					st = new StringTokenizer(curLine);
				} catch (Exception e) {
					eof = true;
					break;
				}
			}
			String ret = buf;
			buf = eof ? "-1" : st.nextToken();
			return ret;
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}

		BigInteger nextBigInteger() {
			return new BigInteger(nextToken());
		}

		public void close() {
			try {
				buf = null;
				st = null;
				curLine = null;
				super.close();
			} catch (Exception e) {

			}
		}

		boolean isEOF() {
			return eof;
		}
	}

	public static void main(String[] args) {
		new D().run();
	}
}