#include <bits/stdc++.h>
using namespace std;
long long n, m, mod;
long long gp(long long lo, long long hi, long long br) {
  if (lo > hi) return 0;
  if (br & 1)
    return ((((lo + hi) / 2) % mod) * (br % mod)) % mod;
  else
    return (((lo + hi) % mod) * ((br / 2) % mod)) % mod;
}
long long sum(long long beg, long long d, long long br, long long lo,
              long long hi) {
  long long end = beg + (br - 1) * d;
  if (end > hi) {
    end -= ((end - hi) / d) * d;
    if (end > hi) end -= d;
  }
  if (beg < lo) {
    beg += ((lo - beg) / d) * d;
    if (beg < lo) beg += d;
  }
  return gp(beg, end, (end - beg) / d + 1);
}
long long f(long long beg, long long d, long long br, long long l, long long r,
            long long lo, long long hi) {
  if (br <= 0 || l > r || br < l) return 0;
  if (l == 1 && br <= r) return sum(beg, d, br, lo, hi);
  long long brc = br / 2, brn = br - brc;
  return (f(beg, 2 * d, brn, l, min(r, brn), lo, hi) +
          f(beg + d, 2 * d, brc, max(l, brn + 1) - brn, r - brn, lo, hi)) %
         mod;
}
int main() {
  scanf("%I64d %I64d %I64d", &n, &m, &mod);
  while (m--) {
    long long a, b, c, d;
    scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &d);
    printf("%I64d\n", f(1, 1, n, a, b, c, d));
  }
  return 0;
}
