#include <bits/stdc++.h>
using namespace std;
long long N, MOD, L, R, U, V, ans;
int T;
void init() { scanf("%I64d%I64d%I64d%I64d", &L, &R, &U, &V); }
inline long long G(long long x, long long y, long long z, long long lim) {
  if (lim < x) return 0;
  long long term, sum;
  term = (y < ((lim - x) / z) + 1 ? y : ((lim - x) / z) + 1);
  sum = x + x + (term - 1) * z;
  if (term % 2 == 0)
    term /= 2;
  else
    sum /= 2;
  return ((term % MOD) * (sum % MOD)) % MOD;
}
inline long long F(long long x, long long y) {
  if (!x || !y) return 0;
  long long l = 1, r = N, mid, ret = 0, bg = 1;
  for (long long p = 0; l < r; p++) {
    mid = (l + r) >> 1;
    if (x <= mid)
      r = mid;
    else {
      ret += G(bg, mid - l + 1, 1LL << (p + 1), y), ret %= MOD;
      l = mid + 1, bg += 1LL << p;
    }
  }
  if (bg <= y) ret = (ret + bg) % MOD;
  return ret;
}
void work() {
  long long a = F(R, V), b = F(R, U - 1), c = F(L - 1, V), d = F(L - 1, U - 1);
  ans = (a - b + MOD - c + MOD + d) % MOD;
  printf("%d\n", (int)ans);
}
int main() {
  for (scanf("%I64d %d %I64d", &N, &T, &MOD); T; T--) {
    init();
    work();
  }
  return 0;
}
