#include <bits/stdc++.h>
using namespace std;
long long int n, m, mod;
long long int l, r, u, v;
long long cal(long long int a, long long int b, long long int c) {
  a -= b;
  if (a < 0)
    a = -1;
  else
    a = a >> c;
  long long int B = b + a * (1ll << c);
  long long int s;
  if ((++a) & 1)
    s = ((B + b) / 2 % mod) * (a % mod);
  else
    s = ((B + b) % mod) * (a / 2 % mod);
  return s % mod;
}
long long int JXDS(long long int l1, long long int r1, long long int down,
                   long long int up) {
  if (u > n) return 0;
  long long int ans = 0;
  if (l1 < l || r1 > r) {
    long long int m = (l1 + r1) / 2;
    if (l <= m) ans += JXDS(l1, m, down, up + 1) % mod;
    if (r > m) ans += JXDS(m + 1, r1, down + (1LL << up), up + 1) % mod;
    ans %= mod;
  } else {
    ans = cal(v, down, up) - cal(u - 1, down, up);
    while (ans < 0) ans += mod;
    ans %= mod;
  }
  return ans;
}
int main() {
  cin >> n >> m >> mod;
  while (m--) {
    cin >> l >> r >> u >> v;
    v = min(v, n);
    long long int jiaxianduanshu = JXDS(1, n, 1, 0);
    cout << jiaxianduanshu % mod << endl;
  }
  return 0;
}
