#include <bits/stdc++.h>
using namespace std;
long long n, m, mod, l, r, u, v;
long long go(long long b, long long le, long long numb, long long nume) {
  if (nume < l || numb > r) return 0;
  if (numb >= l && nume <= r) {
    long long k = (v - b) / le;
    k = min(k, (long long)(nume - numb));
    k %= mod;
    long long ans1 = 0;
    if (v >= b) {
      long long p1;
      if (k % 2)
        p1 = ((k % mod) * ((k + 1) / 2 % mod)) % mod;
      else
        p1 = ((k / 2 % mod) * ((k + 1) % mod)) % mod;
      ans1 =
          ((b % mod) * ((k + 1) % mod) % mod + (le % mod) * (p1 % mod)) % mod;
    }
    long long k1 = (u - 1 - b) / le;
    k1 = min(k1, (long long)(nume - numb));
    k1 %= mod;
    long long ans2 = 0;
    if (u - 1 >= b) {
      long long p2;
      if (k1 % 2)
        p2 = ((k1 % mod) * ((k1 + 1) / 2 % mod)) % mod;
      else
        p2 = ((k1 / 2 % mod) * ((k1 + 1) % mod)) % mod;
      ans2 =
          ((b % mod) * ((k1 + 1) % mod) % mod + (le % mod) * (p2 % mod)) % mod;
    }
    return (ans1 - ans2 + 2 * mod) % mod;
  } else {
    long long ans = 0;
    ans += go(b, 2 * le, numb, (numb + nume) / 2);
    ans += go(b + le, 2 * le, (numb + nume) / 2 + 1, nume);
    return ans % mod;
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> mod;
  for (long long i = 1; i <= m; i++) {
    cin >> l >> r >> u >> v;
    cout << go(1, 1, 1, n) << endl;
  }
  return 0;
}
