#include <bits/stdc++.h>
long long l, r, u, v;
long long N, ret, MOD, M, i, j, k, one = 1;
void count(long long left, long long right, long long ql, unsigned long long qr,
           long long lv, long long D) {
  if (left > right || ql > qr) return;
  long long n, m, rv;
  if (left == ql && right == qr) {
    n = right - left + 1;
    rv = lv + ((n - 1) << D);
    ql = (((u - lv + (one << D) - 1) >> D) << D) + lv;
    qr = (((v - lv) >> D) << D) + lv;
    ql = (ql <= lv ? lv : ql);
    qr = (qr >= rv ? rv : qr);
    if (!(u <= ql && ql <= qr && qr <= v)) return;
    n = ((qr - ql) >> D) + 1;
    ret += (((ql + qr) % MOD) * (((n - (n & 1)) >> 1) % MOD));
    ret += (((ql + qr) >> 1) * (n & 1));
    ret %= MOD;
    return;
  }
  m = (left + right) >> 1;
  n = (m <= qr ? m : qr);
  if (ql <= m) count(left, m, ql, n, lv, D + 1);
  n = (m + 1 >= ql ? m + 1 : ql);
  if (m < qr) count(m + 1, right, n, qr, lv + (one << D), D + 1);
  return;
}
int main() {
  scanf("%I64d %I64d %I64d", &N, &M, &MOD);
  for (i = 0; i < M; i++) {
    scanf("%I64d %I64d %I64d %I64d", &l, &r, &u, &v);
    ret = 0;
    count(1, N, l, r, 1, 0);
    printf("%I64d\n", ret);
  }
  return 0;
}
