#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
int MOD;
int add(int a, int b) { return (a + b >= MOD) ? a + b - MOD : a + b; }
int dec(int a, int b) { return (a - b < 0) ? a - b + MOD : a - b; }
int mul(long long a, long long b) {
  return (1LL * (a % MOD) * (b % MOD)) % MOD;
}
int calc(long long a, long long d, long long num, long long lim) {
  num = min(num, (lim + d - a) / d);
  long long sum;
  if (num & 1)
    sum = mul(num, (num - 1) / 2);
  else
    sum = mul(num / 2, num - 1);
  return add(mul(a, num), mul(d, sum));
}
int Query(long long l, long long r, long long L, long long R, long long u,
          long long v, long long a, long long d) {
  if (r < L || R < l) return 0;
  if (L <= l && r <= R)
    return dec(calc(a, d, r - l + 1, v), calc(a, d, r - l + 1, u - 1));
  return add(Query(l, (l + r >> 1), L, R, u, v, a, d << 1),
             Query((l + r >> 1) + 1, r, L, R, u, v, a + d, d << 1));
}
int main() {
  long long n = read();
  int q = read();
  MOD = read();
  while (q--) {
    long long L = read(), R = read(), u = read(), v = read();
    printf("%d\n", Query(1, n, L, R, u, v, 1, 1));
  }
}
