#include <bits/stdc++.h>
using namespace std;
long long n, m, mod;
long long answer(long long l, long long r, long long u, long long v,
                 long long lo = 0, long long hi = n - 1, long long s = 1,
                 long long d = 1) {
  if (lo > r || hi < l) return 0;
  if (l <= lo && hi <= r) {
    long long s1, e1, tool = hi - lo + 1;
    if (u > s + (tool - 1) * d || v < s) return 0;
    if (u <= s)
      s1 = 0;
    else
      s1 = (u - s - 1) / d + 1;
    if (v >= s + (tool - 1) * d)
      e1 = tool - 1;
    else
      e1 = (v - s) / d;
    long long x = s * 2 + (s1 + e1) * d;
    long long y = e1 - s1 + 1;
    if (x & 1)
      y /= 2;
    else
      x /= 2;
    return ((x % mod) * (y % mod)) % mod;
  }
  long long mid = (lo + hi) / 2;
  return ((long long)answer(l, r, u, v, lo, mid, s, 2 * d) +
          (long long)answer(l, r, u, v, mid + 1, hi, s + d, 2 * d)) %
         mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    long long l, r, u, v;
    cin >> l >> r >> u >> v;
    l--;
    r--;
    cout << (long long)answer(l, r, u, v) % mod << endl;
  }
  return 0;
}
