#include <bits/stdc++.h>
long long n, m, MOD, mn, mx, A, l, r;
void c(long long he, long long N, long long d, long long l, long long r) {
  if (he > mx || he + (N - 1) * d < mn) return;
  if (l == 1 && r == N) {
    long long st, ed;
    if (he >= mn)
      st = 1;
    else
      st = (mn - he - 1) / d + 2;
    if (mx >= he + (N - 1) * d)
      ed = N;
    else
      ed = (mx - he) / d + 1;
    long long a = he * 2 + (st + ed - 2) * d, b = ed - st + 1;
    if (a & 1)
      b >>= 1;
    else
      a >>= 1;
    A = (A + ((a % MOD) * (b % MOD)) % MOD) % MOD;
    return;
  }
  long long z = (N + 1) >> 1;
  if (r <= z)
    c(he, z, d * 2, l, r);
  else if (l > z)
    c(he + d, N - z, d * 2, l - z, r - z);
  else
    c(he, z, d * 2, l, z), c(he + d, N - z, d * 2, 1, r - z);
}
int main() {
  scanf("%I64d%I64d%I64d", &n, &m, &MOD);
  while (m--) {
    scanf("%I64d%I64d%I64d%I64d", &l, &r, &mn, &mx);
    A = 0;
    c(1, n, 1, l, r);
    printf("%I64d\n", A);
  }
  return 0;
}
