#include <bits/stdc++.h>
using namespace std;
long long int mod;
long long int n;
inline long long int getMod(long long int v) { return ((v % mod + mod)) % mod; }
long long int seriesSum(long long int a, long long int d, long long int n) {
  long long int ret = 0;
  assert(d % 2 == 0);
  d /= 2;
  a %= mod;
  n %= mod;
  ret = getMod((a + getMod(n - 1) * d));
  ret = (n * ret) % mod;
  return ret;
}
long long int oddSeries(long long int a, long long int n, long long int d,
                        long long int u, long long int x) {
  if (a > u) return 0;
  long long int tm = (u - a) / d + 1;
  long long int mn = min(n, tm);
  return (seriesSum(a, d, mn) + (getMod(x) * getMod(mn)) % mod) % mod;
}
long long int evenSeries(long long int a, long long int n, long long int d,
                         long long int u, long long int x) {
  if (a > u) return 0;
  long long int tm = (u - a) / d + 1;
  long long int mn = min(n, tm);
  return (seriesSum(a, d, mn) + (getMod(x) * getMod(mn)) % mod) % mod;
}
long long int oddCumulative(long long int size, long long int gap,
                            long long int u, long long int l,
                            long long int constant) {
  if (u < 0)
    return 0;
  else if (l <= 0) {
    return 0;
  } else if (size <= l) {
    return oddSeries(1, size, gap, u, constant);
  } else {
    long long int leftSize = size / 2 + size % 2;
    long long int rightSize = size - leftSize;
    return (oddCumulative(leftSize, gap * 2, u, l, constant) +
            (oddCumulative(rightSize, gap * 2, u - gap, l - leftSize,
                           (constant + gap) % mod))) %
           mod;
  }
}
long long int evenCumulative(long long int size, long long int gap,
                             long long int u, long long int l,
                             long long int constant) {
  if (u < 0)
    return 0;
  else if (l <= 0) {
    return 0;
  } else if (size <= l) {
    return evenSeries(2, size, gap, u, constant);
  } else {
    long long int leftSize = size / 2 + size % 2;
    long long int rightSize = size - leftSize;
    return (evenCumulative(leftSize, gap * 2, u, l, constant) +
            (evenCumulative(rightSize, gap * 2, u - gap, l - leftSize,
                            (constant + gap) % mod))) %
           mod;
  }
}
long long int query(long long int l, long long int u) {
  long long int leftSize = n / 2 + n % 2;
  long long int rightSize = n - leftSize;
  return oddCumulative(leftSize, 2, u, l, 0) +
         evenCumulative(rightSize, 2, u, l - leftSize, 0);
}
long long int original[500];
long long int manualQuery(long long int x, long long int y, long long int a,
                          long long int b) {
  long long int ret = 0;
  int i, j;
  for (i = x; i <= y; i++) {
    if (a <= original[i] && original[i] <= b) {
      ret += original[i];
      ret %= mod;
    }
  }
  return ret;
}
int main() {
  int m;
  scanf("%lld %d %lld", &n, &m, &mod);
  int i, j;
  for (i = 0; i < m; i++) {
    long long int l, r, u, v;
    scanf("%lld %lld %lld %lld", &l, &r, &u, &v);
    long long int sum = getMod((query(r, v) - query(r, u - 1)));
    sum = getMod(sum - getMod((query(l - 1, v) - query(l - 1, u - 1))));
    printf("%lld\n", sum);
  }
  return 0;
}
