#include <bits/stdc++.h>
using namespace std;
long long n, m, MOD;
long long f(long long P, long long K, long long p, long long k, long long minW,
            long long maxW, long long M, long long R) {
  if (P == p && K == k) {
    long long a = minW, b = maxW;
    if (minW % M < R) a = minW - minW % M + R;
    if (minW % M > R) a = minW - minW % M + M + R;
    if (maxW % M < R) b = maxW - maxW % M - M + R;
    if (maxW % M > R) b = maxW - maxW % M + R;
    if (a <= b) {
      long long x = a + b;
      long long y = (b - a) / M + 1;
      if (x % 2 == 0)
        x /= 2;
      else
        y /= 2;
      x %= MOD;
      y %= MOD;
      return (x * y) % MOD;
    }
    return 0;
  }
  long long wyn = 0;
  long long R1 = (R != 0) ? R : M;
  long long R2 = (R != 0) ? R + M : 0;
  long long S = P + (K - P) / 2;
  if (max(P, p) <= min(S, k))
    wyn = (wyn + f(P, S, max(P, p), min(S, k), minW, maxW, M * 2, R1)) % MOD;
  if (max(S + 1, p) <= min(K, k))
    wyn = (wyn + f(S + 1, K, max(S + 1, p), min(K, k), minW, maxW, M * 2, R2)) %
          MOD;
  return wyn;
}
int main() {
  scanf("%I64d %I64d %I64d", &n, &m, &MOD);
  for (int i = 0; i < m; ++i) {
    long long l, r, u, v;
    scanf("%I64d %I64d %I64d %I64d", &l, &r, &u, &v);
    if (u > n)
      printf("0\n");
    else
      printf("%I64d\n", f(1, n, l, r, u, min(n, v), 1, 0));
  }
  return 0;
}
