#include <bits/stdc++.h>
int md;
int s(long long n) {
  return (n % 2 == 0 ? (n / 2 % md) * ((n + 1) % md)
                     : (n % md) * ((n + 1) / 2 % md)) %
         md;
}
int sum, cnt;
void solve(long long n, long long k, long long a) {
  int sum0, cnt0, sum1, cnt1;
  if (k <= 0 || a <= 0)
    sum = cnt = 0;
  else if (k >= n) {
    if (a > n) a = n;
    sum = s(a), cnt = a % md;
  } else {
    solve((n + 1) / 2, k, (a + 1) / 2), sum0 = sum, cnt0 = cnt;
    solve(n / 2, k - (n + 1) / 2, a / 2), sum1 = sum, cnt1 = cnt;
    sum = ((long long)sum0 * 2 - cnt0 + md + sum1 * 2) % md;
    cnt = (cnt0 + cnt1) % md;
  }
}
int main() {
  long long n;
  int m;
  scanf("%lld%d%d", &n, &m, &md);
  while (m--) {
    long long l, r, a, b;
    int ans;
    scanf("%lld%lld%lld%lld", &l, &r, &a, &b), l--, a--;
    ans = 0;
    solve(n, r, b), ans = (ans + sum) % md;
    solve(n, r, a), ans = (ans - sum + md) % md;
    solve(n, l, b), ans = (ans - sum + md) % md;
    solve(n, l, a), ans = (ans + sum) % md;
    printf("%d\n", ans);
  }
  return 0;
}
