#include <bits/stdc++.h>
using namespace std;
long long n, l, r, u, v, ans;
int MOD, m;
void updata(long long num, long long d, long long cnt) {
  if (cnt == 1) {
    if (num >= u && num <= v) ans = (ans + num % MOD) % MOD;
    return;
  }
  long long l = (u - num) / d, r = (v - num) / d;
  if (num + l * d < u) l++;
  if (num + r * d > v) r--;
  l = max(0ll, l);
  r = min(r, cnt - 1);
  if (l > r) return;
  long long g = (num + l * d + num + r * d), h = (r - l + 1);
  if (g % 2 == 0)
    g /= 2;
  else
    h /= 2;
  g %= MOD;
  h %= MOD;
  ans += g * h % MOD;
  ans %= MOD;
}
void solve(long long st, long long ed, long long num, long long d, long long l,
           long long r) {
  if (l == st && r == ed) {
    updata(num, d, ed - st + 1);
    return;
  }
  long long mid = (st + ed) / 2;
  if (r <= mid)
    solve(st, mid, num, d * 2, l, r);
  else if (l > mid)
    solve(mid + 1, ed, num + d, d * 2, l, r);
  else {
    solve(st, mid, num, d * 2, l, mid);
    solve(mid + 1, ed, num + d, d * 2, mid + 1, r);
  }
}
int main() {
  scanf("%I64d%d%d", &n, &m, &MOD);
  while (m--) {
    scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
    ans = 0;
    solve(1ll, n, 1ll, 1ll, l, r);
    printf("%I64d\n", ans);
  }
}
