#include <bits/stdc++.h>
long long n;
int m, mod;
long long q_l, q_r, u, v;
long long whole(long long start, long long end, long long upper) {
  if (upper < start) return 0;
  long long ct = (upper - start) / end + 1;
  upper = start * 2 + (ct - 1) * end;
  if (ct % 2 == 0)
    ct >>= 1;
  else
    upper >>= 1;
  return ct % mod * (upper % mod) % mod;
}
long long query(long long cur_l, long long cur_r, long long st_start,
                long long st_end) {
  if (cur_l >= q_l && cur_r <= q_r)
    return (whole(st_start, st_end, v) - whole(st_start, st_end, u - 1) + mod) %
           mod;
  long long mid = (cur_l + cur_r) >> 1, ans = 0;
  if (mid >= q_l) (ans += query(cur_l, mid, st_start, st_end * 2)) %= mod;
  if (mid + 1 <= q_r)
    (ans += query(mid + 1, cur_r, st_start + st_end, st_end * 2)) %= mod;
  return ans;
}
int main() {
  scanf("%I64d%d%d", &n, &m, &mod);
  do {
    scanf("%I64d%I64d%I64d%I64d", &q_l, &q_r, &u, &v);
    if (v > n) v = n;
    if (u > v)
      puts("0");
    else
      printf("%I64d\n", query(1, n, 1, 1));
  } while (--m);
  return 0;
}
