#include <bits/stdc++.h>
long long n;
long long m;
long long u, v, x, y;
long long MOD(long long _x) { return (_x + m) % m; }
long long sum(long long a, long long b, long long c) {
  if (c < b) return 0;
  c -= b;
  long long re = MOD(MOD(c / a + 1) * MOD(b));
  long long ct = c / a;
  MOD(ct);
  if (ct % 2 == 0)
    re += MOD(MOD(a) * MOD(MOD(ct / 2) * MOD(ct + 1)));
  else
    re += MOD(MOD(a) * MOD(MOD(ct) * MOD((ct + 1) / 2)));
  return re;
}
long long f1(long long f, long long l, long long pre, long long st) {
  if (l < u or f > v)
    return 0;
  else if (u <= f and l <= v)
    return MOD(sum(pre, st, y) - sum(pre, st, x - 1));
  return MOD(f1(f, (f + l) / 2, 2 * pre, st) +
             f1((f + l) / 2 + 1, l, 2 * pre, pre + st));
}
void init() {
  int q;
  scanf("%I64d %d %I64d", &n, &q, &m);
  for (int i = 0; i < q; i++) {
    scanf("%I64d %I64d %I64d %I64d", &u, &v, &x, &y);
    if (y > n) y = n;
    if (x > n)
      printf("0\n");
    else
      printf("%I64d\n", MOD(f1(1, n, 1, 1)));
  }
}
int main() { init(); }
