#include <bits/stdc++.h>
using namespace std;
long long mdc(long long a, long long b) { return (b ? mdc(b, a % b) : a); }
long long f(long long u, long long v, long long b, long long delta, long long n,
            long long wanted, long long mod) {
  long long n1, n2;
  if (!wanted) return 0;
  if (n != wanted) {
    n1 = n / 2 + (n % 2 ? 1 : 0), n2 = n / 2;
    if (n1 >= wanted) return f(u, v, b, delta * 2, n1, wanted, mod);
    return (f(u, v, b, delta * 2, n1, n1, mod) +
            f(u, v, b + delta, delta * 2, n2, wanted - n1, mod)) %
           mod;
  }
  n1 = (u + delta - b) / delta;
  if (mdc(u + delta - b, delta) != delta) ++n1;
  if ((u + delta - b) > delta * n) return 0;
  if ((u + delta - b) < delta) n1 = 1;
  n2 = (v + delta - b) / delta;
  if ((v + delta - b) < delta) return 0;
  if ((v + delta - b) > delta * n) n2 = n;
  n = n2 - n1 + 1;
  if (delta == 1) {
    if ((n2 - n1) % 2 == 0)
      return ((b + (((((n2 + n1 - 2) / 2) % mod) * delta) % mod)) * (n % mod)) %
             mod;
    return (((2 * b) % mod + ((delta % mod) * ((n2 + n1 - 2) % mod)) % mod) *
            ((n / 2) % mod)) %
           mod;
  }
  return ((((b % mod) + ((delta / 2) % mod) * ((n2 + n1 - 2) % mod)) % mod) *
          (n % mod)) %
         mod;
}
int main() {
  long long n, m, mod;
  cin >> n >> m >> mod;
  for (int i = 0; i < m; ++i) {
    long long l, r, u, v;
    cin >> l >> r >> u >> v;
    long long r1 = f(u, v, 1, 1, n, r, mod), r2 = f(u, v, 1, 1, n, l - 1, mod);
    cout << (r1 - r2 + mod) % mod << endl;
  }
  return 0;
}
