#include <bits/stdc++.h>
long long u, v, mod;
long long get(long long delta, long long first, long long begin, long long end,
              long long l, long long r) {
  if (begin == l && end == r) {
    long long last = first + (r - l) * delta;
    if (first > v || last < u) return (0);
    if (first < u) first += (u - first) / delta * delta;
    if (first < u) first += delta;
    if (last > v) last -= (last - v) / delta * delta;
    if (last > v) last -= delta;
    if (first > last) return (0);
    long long cnt = ((last - first) / delta + 1) % mod,
              sum = (first + last) % mod;
    return (cnt * sum % mod);
  }
  long long mid = begin + end >> 1;
  if (r <= mid)
    return (get(delta << 1, first, begin, mid, l, r));
  else if (l > mid)
    return (get(delta << 1, first + delta, mid + 1, end, l, r));
  else
    return ((get(delta << 1, first, begin, mid, l, mid) +
             get(delta << 1, first + delta, mid + 1, end, mid + 1, r)) %
            mod);
}
int main() {
  long long n;
  int m;
  scanf("%I64d%d%I64d", &n, &m, &mod);
  mod <<= 1;
  while (m--) {
    long long l, r;
    scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
    printf("%I64d\n", get(1, 1, 1, n, l, r) / 2);
  }
  return (0);
}
