#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
long long n, md, m;
long long gt(long long h, long long l, long long r, long long f, long long s,
             long long b, long long val) {
  if (b < val) return 0;
  if (s < l || r < f) return 0;
  if (f <= l && r <= s) {
    long long x = (b - val) / (1LL << h);
    long long w = min(x + 1, r - l + 1);
    x %= md;
    w %= md;
    val %= md;
    return ((((x * (x + 1) / 2) % md) * ((1LL << h) % md)) % md +
            (w * val) % md) %
           md;
  }
  long long mid = (l + r) >> 1;
  return (gt(h + 1, l, mid, f, s, b, val) +
          gt(h + 1, mid + 1, r, f, s, b, val + (1LL << h))) %
         md;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> md;
  while (m--) {
    long long f, s, lb, ub;
    cin >> f >> s >> lb >> ub;
    if (lb > n) {
      cout << 0 << endl;
    } else {
      ub = min(ub, n);
      cout << (gt(0, 1, n, f, s, ub, 1) - gt(0, 1, n, f, s, lb - 1, 1) + md) %
                  md
           << endl;
    }
  }
  return 0;
}
