#include <bits/stdc++.h>
using namespace std;
long long MOD;
inline long long mul(long long a, long long b) {
  return ((a % MOD) * (b % MOD)) % MOD;
}
long long get_sum(long long p, long long q, long long mx) {
  if (mx < q) return 0;
  mx -= q;
  long long add = mul(mx / p + 1, q);
  long long cnt = mx / p;
  if (cnt % 2)
    add += mul(p, mul(cnt, (cnt + 1) / 2));
  else
    add += mul(p, mul(cnt / 2, cnt + 1));
  return add % MOD;
}
long long n, m;
long long mn, mx, x, y;
long long calc(long long lo, long long hi, long long p, long long q) {
  if (y < lo || hi < x) return 0;
  if (x <= lo && hi <= y)
    return (get_sum(p, q, mx) - get_sum(p, q, mn - 1) + MOD) % MOD;
  long long mid = (lo + hi) / 2;
  return (calc(lo, mid, 2 * p, q) + calc(mid + 1, hi, 2 * p, q + p)) % MOD;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> MOD;
  while (m--) {
    cin >> x >> y >> mn >> mx;
    if (mx > n) mx = n;
    if (mn > n)
      cout << 0 << endl;
    else
      cout << calc(1, n, 1, 1) << endl;
  }
  return 0;
}
