#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
long long n, q, mod;
long long calc(long long first, long long delta, long long l, long long r,
               long long u, long long v, long long n) {
  if (1 == l && n <= r) {
    long long ret = 0;
    if (v < first) return 0;
    long long last = first + delta * (n - 1);
    if (u > last) return 0;
    long long beg = u - first;
    beg /= delta;
    if (beg < 0) beg = 0;
    if (first + delta * beg < u) ++beg;
    beg = delta * beg + first;
    last = v - first;
    last /= delta;
    last = min(last, n - 1);
    last = last * delta + first;
    long long cnt = (last - beg) / delta + 1;
    return ((last % (2 * mod) + beg % (2 * mod)) * (cnt % (2 * mod)) / 2) % mod;
  }
  long long mid = (n + 1) / 2;
  long long ret = 0;
  if (l <= mid) ret = calc(first, delta * 2, l, min(mid, r), u, v, (n + 1) / 2);
  if (r > mid)
    ret += calc(first + delta, delta * 2, max(l, mid + 1) - mid, r - mid, u, v,
                n / 2);
  return ret % mod;
}
int main() {
  cin >> n >> q >> mod;
  while (q--) {
    long long l, r, u, v;
    cin >> l >> r >> u >> v;
    cout << calc(1, 1, l, r, u, v, n) << endl;
  }
  return 0;
}
