#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
  read(x);
  read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
  read(x);
  read(y);
  read(z);
}
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
long long n, m, i, j, k, l, r, u, v, p, mod;
long long ans, inv2;
long long pow(long long a, long long b, long long c) {
  long long ans = 1;
  for (; b; b >>= 1, a = a * a % c)
    if (b & 1) ans = ans * a % c;
  return ans;
}
long long calc(long long st, long long d, long long t, long long y) {
  if (y < st) return 0;
  long long z = min(t, (y + d - st) / d);
  long long tt1 = st + (z - 1) * d + st, tt2 = z;
  if (tt1 % 2 == 0)
    tt1 /= 2;
  else
    tt2 /= 2;
  return (tt1 % mod) * (tt2 % mod) % mod;
}
void go(long long len, long long st, long long d, long long x, long long y) {
  if (x == 0) return;
  long long t = (len + 1) / 2;
  if (x >= t) {
    ans = ((ans) + (calc(st, d * 2, t, y))) % mod;
    go(len - t, st + d, d * 2, x - t, y);
  } else {
    go(t, st, d * 2, x, y);
  }
}
long long c(long long x, long long y) {
  ans = 0;
  go(n, 1, 1, x, y);
  return ans;
}
int main() {
  read(n, m, mod);
  for (; m; m--) {
    read(l, r);
    read(u, v);
    long long res;
    res = c(r, v) + c(l - 1, u - 1) - c(r, u - 1) - c(l - 1, v);
    res = (res % mod + mod) % mod;
    printf("%I64d\n", res);
  }
  return 0;
}
