#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << *i << " ";
  cout << endl;
}
template <class T>
void pvp(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << "(" << i->first << ", " << i->second << ") ";
  cout << endl;
}
template <class T>
void chmin(T &t, T f) {
  if (t > f) t = f;
}
template <class T>
void chmax(T &t, T f) {
  if (t < f) t = f;
}
int in_c() {
  int c;
  for (; (c = getchar()) <= ' ';) {
    if (!~c) throw ~0;
  }
  return c;
}
int in() {
  int x = 0, c;
  for (; (unsigned)((c = getchar()) - '0') >= 10;) {
    if (c == '-') return -in();
    if (!~c) throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((unsigned)((c = getchar()) - '0') < 10);
  return x;
}
long long In() {
  long long x = 0, c;
  for (; (unsigned)((c = getchar()) - '0') >= 10;) {
    if (c == '-') return -In();
    if (!~c) throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((unsigned)((c = getchar()) - '0') < 10);
  return x;
}
void Out(long long x) {
  if (x >= 10) Out(x / 10);
  putchar('0' + x % 10);
}
long long N;
int Q;
long long MO;
long long mul(long long a, long long b) {
  a %= MO;
  b %= MO;
  return a * b % MO;
}
long long mulDiv2(long long a, long long b) {
  a %= MO;
  b %= MO;
  if (a % 2 == 0)
    a /= 2;
  else
    b /= 2;
  return a * b % MO;
}
long long calc(long long u, long long p, long long m) {
  chmin(u, N);
  if (u < p) return 0;
  long long k = (u - p) / m;
  return (mul(mulDiv2(k, k + 1), m) + mul(k + 1, p)) % MO;
}
long long calc(long long u, long long v, long long p, long long m) {
  long long ret = 0;
  ret += calc(v, p, m);
  ret -= calc(u - 1, p, m);
  ret %= MO;
  return ret;
}
long long solve(long long l, long long r, long long u, long long v, long long a,
                long long b, long long p, long long m) {
  chmax(l, a);
  chmin(r, b);
  if (l > r) return 0;
  long long ret = 0;
  if (l == a && r == b) {
    ret += calc(u, v, p, m);
  } else {
    long long odd = (b - a + 1 + 1) / 2;
    ret += solve(l, r, u, v, a, a + odd - 1, p, m * 2);
    ret += solve(l, r, u, v, a + odd, b, p + m, m * 2);
  }
  ret %= MO;
  return ret;
}
int main() {
  int q;
  long long l, r, u, v;
  for (; cin >> N >> Q >> MO;) {
    for (q = 0; q < Q; ++q) {
      l = In();
      r = In();
      u = In();
      v = In();
      long long res = solve(l, r, u, v, 1, N, 1, 1);
      res %= MO;
      res += MO;
      res %= MO;
      Out(res);
      puts("");
    }
  }
  return 0;
}
