#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T checkmin(T &a, T b) {
  return (a < b) ? a : a = b;
}
template <class T>
inline T checkmax(T &a, T b) {
  return (a > b) ? a : a = b;
}
template <class T>
T GCD(T a, T b) {
  if (a < 0) return GCD(-a, b);
  if (b < 0) return GCD(a, -b);
  return (a == 0) ? b : GCD(b % a, a);
}
template <class T>
T LCM(T a, T b) {
  if (a < 0) return LCM(-a, b);
  if (b < 0) return LCM(a, -b);
  return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;
}
namespace Poor {
long long N, Odd, Even;
int M, Mod;
long long DoOdd(long long L, long long R, long long U, long long V) {
  if (L > R) return 0;
  checkmax(L, U / 2LL + 1);
  checkmin(R, (V + 1LL) / 2LL);
}
long long DoEven(long long L, long long R, long long U, long long V) {
  if (L > R) return 0;
}
void Run() {
  cin >> N >> M >> Mod;
  Odd = (N + 1) / 2LL;
  Even = N / 2LL;
  for (int i = 0; i < M; ++i) {
    long long L, R, U, V;
    cin >> L >> R >> U >> V;
    cout << (DoOdd(L, min(R, Odd), U, V) + DoEven(max(Odd + 1, L), R, U, V)) %
                Mod
         << endl;
  }
}
}  // namespace Poor
namespace Poor2 {
long long N, U, V, Mod;
int M;
long long Ask(long long a1, long long d, long long n, long long S,
              long long T) {
  if (S == 1 && T == n) {
    long long Left = max((U - a1 + d - 1LL) / d, 0LL);
    long long Right = min((V - a1) / d, n - 1LL);
    while (U > a1 + Left * d) ++Left;
    while (V < a1 + Right * d) --Right;
    if (Left > Right) return 0;
    return (((((a1 % (Mod * 2LL)) +
               ((Left % (Mod * 2LL)) * (d % (Mod * 2LL))) % (Mod * 2LL) +
               (a1 % (Mod * 2LL)) +
               (Right % (Mod * 2LL) * (d % (Mod * 2LL))) % (Mod * 2LL)) %
              (Mod * 2LL))) *
            ((Right - Left + 1LL) % (Mod * 2LL)) % (Mod * 2LL)) /
           2LL;
  }
  long long odd = (n + 1LL) / 2LL;
  long long res = 0;
  if (S <= odd) res += Ask(a1, d * 2LL, odd, S, min(T, odd));
  if (odd < T)
    res += Ask(a1 + d, d * 2LL, n - odd, max(S, odd + 1LL) - odd, T - odd);
  res %= Mod;
  return res;
}
void Run() {
  cin >> N >> M >> Mod;
  for (int i = 0; i < M; ++i) {
    long long S, T;
    cin >> S >> T >> U >> V;
    cout << Ask(1, 1, N, S, T) << endl;
  }
}
}  // namespace Poor2
int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  Poor2::Run();
}
