#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const int MAXN = int(2e5 + 7);
int m, mod;
long long n;
int solve(long long a1, long long d, long long tl, long long tr, long long l,
          long long r, long long u, long long v) {
  if (l > tr || r < tl) return 0;
  if (tl > tr) return 0;
  if (l <= tl && tr <= r) {
    long long an = a1 + (tr - tl) * d;
    if (u > a1) a1 += (u - a1 + d - 1) / d * d;
    if (an > v) an -= (an - v + d - 1) / d * d;
    if (an < a1) return 0;
    long long n = (an - a1) / d + 1;
    if ((a1 + an) & 1)
      return (((n / 2) % mod) * ((a1 + an) % mod)) % mod;
    else
      return ((n % mod) * (((a1 + an) / 2) % mod)) % mod;
  }
  long long tm = (tl + tr) / 2;
  return (solve(a1, d * 2, tl, tm, l, r, u, v) +
          solve(a1 + d, d * 2, tm + 1, tr, l, r, u, v)) %
         mod;
}
int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    long long l, r, u, v;
    cin >> l >> r >> u >> v;
    cout << solve(1, 1, 1, n, l, r, u, v) << endl;
  }
  return 0;
}
