#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
A convert(B x) {
  stringstream ss;
  ss << x;
  A ret;
  ss >> ret;
  return ret;
}
const int oo = ~0u >> 2;
const double eps = 1e-10;
const int fx[8][2] = {{-1, 0},  {1, 0},  {0, -1}, {0, 1},
                      {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
long long n, m, u, v, mo;
long long dg(long long a, long long b, long long n, long long l, long long r) {
  if (l == 0 && r == n) {
    long long x = min(n, max(0LL, (u - b + a - 1) / a));
    long long y = min(n, max(0LL, (v - b + a - 1) / a));
    if (x < y) {
      long long ret = 0;
      if ((y - x) % 2 == 0)
        ret = ((y - x) / 2 % mo) * ((x + y - 1) % mo);
      else
        ret = ((x + y - 1) / 2 % mo) * ((y - x) % mo);
      ret = ret % mo * (a % mo) + (y - x) % mo * (b % mo);
      return ret % mo;
    } else
      return 0LL;
  } else {
    long long m = (n + 1) / 2;
    if (r <= m) return dg(2 * a, b, m, l, r);
    if (m <= l) return dg(2 * a, b + a, n - m, l - m, r - m);
    return (dg(2 * a, b, m, l, m) + dg(2 * a, b + a, n - m, 0, r - m)) % mo;
  }
}
int main() {
  scanf("%I64d%I64d%I64d", &n, &m, &mo);
  while (m--) {
    long long l, r;
    scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
    ++v;
    printf("%I64d\n", dg(1, 1, n, l - 1, r));
  }
}
