#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
const int INF = 1000000001;
long long mod;
long long solve(long long p, long long q, long long l, long long r, long long x,
                long long d = 0, long long suffix = 0) {
  ;
  if (x == -1) {
    ;
    return 0;
  }
  if (r < p || l > q) {
    ;
    return 0;
  }
  if (l <= p && q <= r) {
    if (x < suffix) return 0;
    long long a = (x - suffix) >> d;
    ;
    a %= mod;
    long long ret = (a * (a + 1) / 2) % mod;
    ret = (ret * ((1LL << d) % mod)) % mod;
    long long s = (suffix + 1) % mod;
    s = (s * (a + 1)) % mod;
    ret = (ret + s) % mod;
    ;
    return ret;
  }
  long long len = q - p + 1;
  return (solve(p, p + (len + 1) / 2 - 1, l, r, x, d + 1, suffix) +
          solve(p + (len + 1) / 2, q, l, r, x, d + 1, (1LL << d) | suffix)) %
         mod;
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  long long n;
  int m;
  cin >> n >> m >> mod;
  while (m--) {
    long long a, b, l, r;
    cin >> l >> r >> a >> b;
    if (a > n) {
      cout << 0 << endl;
      continue;
    }
    b = min(b, n);
    --l, --r, --a, --b;
    cout << (solve(0, n - 1, l, r, b) - solve(0, n - 1, l, r, a - 1) + mod) %
                mod
         << endl;
  }
  return 0;
}
