#include <bits/stdc++.h>
using namespace std;
long long n_, mod, l_, r_, m;
long long u, v;
void read(long long &x) {
  char ch;
  while (ch = getchar(), ch < '0')
    ;
  x = ch - '0';
  while (ch = getchar(), ch >= '0') x = x * 10 + ch - '0';
}
long long Get(long long st, long long ed, long long d) {
  if ((st + ed) & 1)
    return ((st + ed) % mod) * (((ed - st) / d + 1) / 2 % mod) % mod;
  return (st + ed) / 2 % mod * (((ed - st) / d + 1) % mod) % mod;
}
long long ans(long long st, long long d, long long n, long long l,
              long long r) {
  if (n == 1)
    if (st >= v && st <= u)
      return st;
    else
      return 0;
  if (st + (n - 1) * d < v || st > u) return 0;
  if (l == 1 && r == n)
    return Get(max(((v - st - 1 + d) / d) * d + st, st),
               min(((u - st) / d) * d + st, st + (n - 1) * d), d);
  if (r <= (n + 1) / 2) return ans(st, d * 2, (n + 1) / 2, l, r);
  if (l > (n + 1) / 2)
    return ans(st + d, d * 2, n / 2, l - (n + 1) / 2, r - (n + 1) / 2);
  return (ans(st, d * 2, (n + 1) / 2, l, (n + 1) / 2) +
          ans(st + d, d * 2, n / 2, 1, r - (n + 1) / 2)) %
         mod;
}
int main() {
  read(n_);
  read(m);
  read(mod);
  for (; m--;) {
    read(l_);
    read(r_);
    read(v);
    read(u);
    printf("%d\n", (int)ans(1, 1, n_, l_, r_));
  }
  return 0;
}
