#include <bits/stdc++.h>
long long mod;
long long solve(int dep, long long idx, long long left, long long right,
                long long l, long long r, long long u, long long v) {
  if (right < l) return 0;
  if (left > r) return 0;
  if (left >= l && right <= r) {
    long long delta = (1LL << dep);
    long long begin = idx, end = idx + (right - left) * delta;
    if (end < u) return 0;
    if (begin > v) return 0;
    if (begin < u) begin = begin + (u - begin) / delta * delta;
    if (begin < u) begin += delta;
    if (end > v) end = end - (end - v) / delta * delta;
    if (end > v) end -= delta;
    if (begin > end) return 0;
    long long total = (end - begin) / delta + 1;
    return ((total % mod) * ((begin + end) % mod)) % mod;
  }
  long long delta = (1LL << dep);
  long long mid = (left + right) / 2;
  return (solve(dep + 1, idx, left, mid, l, r, u, v) +
          solve(dep + 1, idx + delta, mid + 1, right, l, r, u, v)) %
         mod;
}
int main() {
  long long n;
  int m;
  while (scanf("%I64d%d%I64d", &n, &m, &mod) == 3) {
    mod *= 2;
    for (int i = 0; i < m; i++) {
      long long l, r, u, v;
      scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
      printf("%I64d\n", solve(0, 1, 1, n, l, r, u, v) / 2);
    }
  }
  return 0;
}
