#include <bits/stdc++.h>
using namespace std;
int M;
int rst;
long long u, v;
void Query(long long L, long long R, long long l, long long r, long long e,
           int x) {
  long long r1, r2;
  if (L == l && R == r) {
    if (!x) {
      r1 = v + 1;
      r2 = v + 2;
      if (r1 % 2)
        r2 /= 2;
      else
        r1 /= 2;
      r1 %= M, r2 %= M;
      rst = (rst + (r1 * r2) % M) % M;
      r1 = u, r2 = u + 1;
      if (r1 & 1)
        r2 /= 2;
      else
        r1 /= 2;
      r1 %= M;
      r2 %= M;
      rst = (rst + M - (r1 * r2) % M) % M;
    } else {
      long long le = ((u >> x) << x) + e, ri = ((v >> x) << x) + e;
      if (le < u) le += 1LL << x;
      if (ri > v) ri -= 1LL << x;
      if (le <= ri) {
        r1 = le + ri + 2;
        r2 = (ri >> x) - (le >> x) + 1;
        if (r1 % 2)
          r2 >>= 1;
        else
          r1 >>= 1;
        r1 %= M, r2 %= M;
        rst = (rst + r1 * r2) % M;
      }
    }
    return;
  }
  long long m = (L + R) >> 1;
  if (l <= m) Query(L, m, l, min(r, m), e, x + 1);
  if (m < r) Query(m + 1, R, max(m + 1, l), r, e + (1LL << x), x + 1);
}
int main() {
  long long n;
  int m;
  scanf("%I64d%d%d", &n, &m, &M);
  while (m--) {
    long long l, r;
    scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
    if (u > n) {
      puts("0");
      continue;
    }
    if (v > n) v = n;
    rst = 0;
    u--;
    v--;
    Query(0, n - 1, l - 1, r - 1, 0, 0);
    printf("%d\n", rst);
  }
  return 0;
}
