#include <bits/stdc++.h>
using namespace std;
int m;
long long n, ans, mo, L, R, x, y;
long long Sum(long long S, long long d, long long num) {
  long long xx, yy, res;
  S %= mo;
  d %= mo;
  xx = S * (num % mo) % mo;
  if (num & 1)
    yy = (num % mo * ((num - 1) / 2 % mo) % mo) * d % mo;
  else
    yy = (num / 2 % mo * ((num - 1) % mo) % mo) * d % mo;
  res = (xx + yy) % mo;
  return res;
}
long long Calc(long long l, long long r, long long x, long long y, long long s,
               long long d, long long before) {
  long long res = 0;
  if (l == r) {
    if (before + l >= L && before + l <= R) res = s % mo;
    return res;
  }
  long long mid = (l + r) / 2;
  long long X1, Y1, X2, Y2, S1, S2, D;
  X1 = x / 2 + 1;
  Y1 = (y + 1) / 2;
  X2 = (x + 1) / 2;
  Y2 = y / 2;
  if (x & 1)
    S1 = s, S2 = (s + d) % mo;
  else
    S1 = (s + d) % mo, S2 = s;
  D = 2 * d % mo;
  if (X1 <= Y1) {
    if (l >= L && mid <= R)
      res = (res + Sum(S1, D, Y1 - X1 + 1)) % mo;
    else if (l <= R && mid >= L)
      res = (res + Calc(l, mid, X1, Y1, S1, D, before)) % mo;
  }
  if (X2 <= Y2) {
    if (mid + 1 >= L && r <= R)
      res = (res + Sum(S2, D, Y2 - X2 + 1)) % mo;
    else if (mid + 1 <= R && r >= L)
      res = (res + Calc(mid + 1, r, X2, Y2, S2, D, before + mid - l + 1)) % mo;
  }
  return res;
}
int main() {
  scanf("%I64d%d%I64d", &n, &m, &mo);
  for (int i = 1; i <= m; ++i) {
    scanf("%I64d%I64d%I64d%I64d", &L, &R, &x, &y);
    if (y > n) y = n;
    if (x < 1) x = 1;
    if (y < x) {
      printf("0\n");
      continue;
    }
    long long ans = Calc(1, n, x, y, x % mo, 1, 0);
    printf("%I64d\n", ans);
  }
  return 0;
}
