#include <bits/stdc++.h>
using namespace std;
long long n;
int m, mod;
pair<int, long long> fnd(long long n, long long l, long long r, long long u,
                         long long v) {
  l = max(l, (long long)0);
  r = min(n - 1, r);
  u = max(u, (long long)1);
  v = min(n, v);
  if (l > r || u > v) return make_pair(0, 0);
  pair<int, long long> res;
  if (l == 0 && r == n - 1) {
    long long a, b;
    a = u + v;
    b = v - u + 1;
    if (a % 2 == 0)
      a /= 2;
    else
      b /= 2;
    res = make_pair(a % mod * (b % mod) % mod, v - u + 1);
    return res;
  }
  pair<int, long long> r1, r2;
  r1 = fnd((n + 1) / 2, l, r, u / 2 + 1, (v + 1) / 2);
  r2 = fnd(n / 2, l - (n + 1) / 2, r - (n + 1) / 2, (u + 1) / 2, v / 2);
  int a, b;
  a = ((2 * r1.first - r1.second % mod) % mod + mod) % mod;
  b = 2 * r2.first % mod;
  res = make_pair((a + b) % mod, r1.second + r2.second);
  return res;
}
int main() {
  int i, j, k;
  for (; cin >> n >> m >> mod;) {
    for (k = 0; k < m; ++k) {
      long long l, r, u, v;
      cin >> l >> r >> u >> v;
      --l;
      --r;
      int res = fnd(n, l, r, u, v).first;
      printf("%d\n", res);
    }
  }
  return 0;
}
