#include <bits/stdc++.h>
long long n, m, mod, sum;
void q(long long l, long long r, long long u, long long v, long long mm,
       long long mr, long long id, long long num) {
  long long mid = id + (num - 1) / 2;
  if (l == id && r == id + num - 1) {
    long long st, ed;
    if (u % mm > mr % mm)
      st = (u / mm + 1) * mm + mr % mm;
    else
      st = (u / mm) * mm + mr % mm;
    if (v % mm < mr % mm)
      ed = (v / mm - 1) * mm + mr % mm;
    else
      ed = (v / mm) * mm + mr % mm;
    long long ed1;
    if (n % mm < mr % mm)
      ed1 = (n / mm - 1) * mm + mr % mm;
    else
      ed1 = (n / mm) * mm + mr % mm;
    ed = ed < ed1 ? ed : ed1;
    if (st > ed) return;
    long long d = (ed - st) / mm + 1, a = ed + st;
    if (d % 2 == 0)
      d /= 2;
    else
      a /= 2;
    d %= mod;
    a %= mod;
    sum += (a * d) % mod;
    sum %= mod;
  } else if (l > mid)
    q(l, r, u, v, mm * 2, mr + mm, mid + 1, num - (num + 1) / 2);
  else if (r <= mid)
    q(l, r, u, v, mm * 2, mr, id, (num + 1) / 2);
  else {
    q(l, mid, u, v, mm * 2, mr, id, (num + 1) / 2);
    q(mid + 1, r, u, v, mm * 2, mr + mm, id + (num + 1) / 2,
      num - (num + 1) / 2);
  }
}
int main() {
  while (scanf("%I64d%I64d%I64d", &n, &m, &mod) != EOF) {
    while (m--) {
      long long l, r, u, v;
      scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
      sum = 0;
      q(l - 1, r - 1, u, v, 1, 1, 0, n);
      printf("%I64d\n", sum);
    }
  }
  return 0;
}
