#include <bits/stdc++.h>
using namespace std;
long long n, Mod, P[65], u, v;
int m;
long long Query(long long L, long long R, long long l, long long r, int dep,
                long long head) {
  if (L == l && r == R) {
    long long u1 = head, v1 = head + (R - L) * P[dep];
    if (v1 < u || u1 > v) return 0;
    long long u2 = u, v2 = v;
    u2 -= head;
    if (u2 <= 0)
      u2 = 0;
    else if (u2 % P[dep])
      u2 = (u2 - 1) / P[dep] * P[dep] + P[dep];
    u2 += head;
    v2 -= head;
    if (v2 <= 0)
      v2 = 0;
    else if (v2 % P[dep])
      v2 = v2 / P[dep] * P[dep];
    v2 += head;
    u1 = max(u1, u2);
    v1 = min(v1, v2);
    long long r1 = u1 + v1, r2 = (v1 - u1) / P[dep] + 1;
    if (r1 % 2)
      r2 /= 2;
    else
      r1 /= 2;
    return (r1 % Mod) * (r2 % Mod) % Mod;
  }
  long long mid = (L + R) >> 1;
  if (mid >= r)
    return Query(L, mid, l, r, dep + 1, head);
  else if (mid < l)
    return Query(mid + 1, R, l, r, dep + 1, head + P[dep]);
  else
    return (Query(L, mid, l, mid, dep + 1, head) +
            Query(mid + 1, R, mid + 1, r, dep + 1, head + P[dep])) %
           Mod;
}
int main() {
  scanf("%lld%d%lld", &n, &m, &Mod);
  for (int i = 0; i < 65; i++) P[i] = (1ll << i);
  while (m--) {
    long long L, R;
    scanf("%lld%lld%lld%lld", &L, &R, &u, &v);
    printf("%lld\n", Query(1, n, L, R, 0, 1));
  }
  return 0;
}
