#include <bits/stdc++.h>
using namespace std;
long long N;
int Q, MOD;
long long start, final, lb, ub;
long long sum(long long f, long long gap, long long n) {
  n++;
  n %= MOD;
  f %= MOD;
  gap %= MOD;
  return (n * f % MOD + (n * (n - 1) / 2) % MOD * gap) % MOD;
}
long long solve(long long lo, long long hi, long long roof, long long first = 1,
                long long gap = 1) {
  if (lo > hi) return 0;
  if (lo > final || hi < start) return 0;
  if (first > roof) return 0;
  if (lo >= start && hi <= final) {
    long long p = (roof - first) / gap;
    p = min(p, hi - lo);
    return sum(first, gap, p);
  }
  long long mid = (lo + hi) / 2;
  return (solve(lo, mid, roof, first, gap * 2) +
          solve(mid + 1, hi, roof, first + gap, gap * 2)) %
         MOD;
}
int main() {
  scanf("%I64d %d %d", &N, &Q, &MOD);
  while (Q--) {
    scanf("%I64d %I64d %I64d %I64d", &start, &final, &lb, &ub);
    printf("%I64d\n", (solve(1, N, ub) - solve(1, N, lb - 1) + MOD) % MOD);
  }
  return 0;
}
