#include <bits/stdc++.h>
using namespace std;
long long n, m, p;
long long getmax(long long a, long long b) { return a > b ? a : b; }
long long getmin(long long a, long long b) { return a < b ? a : b; }
long long getans(long long s, long long d, long long lower, long long upper,
                 long long st, long long ed, long long down, long long up) {
  if (lower == st && upper == ed) {
    down = getmax(s, down);
    up = getmin(s + (upper - lower) * d, up);
    if (up < down) return 0;
    long long p1, p2, t1 = (up - s + 1) / d + ((up - s + 1) % d != 0),
                      t2 = (down - s) / d + ((down - s) % d != 0);
    if (t1 & 1)
      p1 = ((t1 % p) * (((t1 - 1) / 2) % p)) % p;
    else
      p1 = (((t1 / 2) % p) * ((t1 - 1) % p)) % p;
    if (t2 & 1)
      p2 = ((t2 % p) * (((t2 - 1) / 2) % p)) % p;
    else
      p2 = (((t2 / 2) % p) * ((t2 - 1) % p)) % p;
    return ((t1 * (s % p) % p + p1 * (d % p) % p - t2 * (s % p) % p -
             p2 * (d % p) % p) %
                p +
            p) %
           p;
  }
  long long mid = (lower + upper) >> 1;
  if (ed <= mid)
    return getans(s, d * 2, lower, mid, st, ed, down, up);
  else if (st > mid)
    return getans(s + d, d * 2, mid + 1, upper, st, ed, down, up);
  else
    return (getans(s, d * 2, lower, mid, st, mid, down, up) +
            getans(s + d, d * 2, mid + 1, upper, mid + 1, ed, down, up)) %
           p;
}
int main() {
  cin >> n >> m >> p;
  while (m--) {
    long long l, r, u, v;
    cin >> l >> r >> u >> v;
    cout << getans(1, 1, 1, n, l, r, u, v) << endl;
  }
  return 0;
}
