#include <bits/stdc++.h>
using namespace std;
long long n, mod, s, s1, s2;
inline long long get(long long id, long long rem, long long l, long long r,
                     long long b, long long e, long long x) {
  if (e < l || r < b) {
    return 0;
  }
  if (b <= l && r <= e) {
    if (x < rem) {
      return 0;
    }
    s = (x - rem) / (1LL << id);
    s1 = s;
    s2 = s + 1;
    if (s % 2 == 0) {
      s1 /= 2;
    } else {
      s2 /= 2;
    }
    return (((s + 1) % mod) * (rem % mod) % mod +
            ((1LL << id) % mod) * (s1 % mod) % mod * (s2 % mod) % mod) %
           mod;
  }
  long long lrem = rem, rrem = rem + (1LL << id);
  if (rem == 0) {
    rrem = 0;
    lrem = (1LL << id);
  }
  return (get(id + 1, lrem, l, (l + r) / 2, b, e, x) +
          get(id + 1, rrem, (l + r) / 2 + 1, r, b, e, x)) %
         mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long m, l, r, u, v;
  cin >> n >> m >> mod;
  while (m--) {
    cin >> l >> r >> u >> v;
    cout << (get(0, 0, 1, n, l, r, min(n, v)) -
             get(0, 0, 1, n, l, r, min(n + 1, u) - 1) + mod) %
                mod
         << endl;
  }
}
