#include <bits/stdc++.h>   using namespace std;   int Sky[105][105][15];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, q, c;     cin >> n >> q >> c;     c++;     for (int i = 1; i <= n; i++) {       int x, y, s;       cin >> x >> y >> s;       for (int r = 0; r < c; r++) Sky[x][y][r] += (s + r) % c;     }     for (int x = 1; x <= 100; x++)       for (int y = 1; y <= 100; y++)         for (int r = 0; r < c; r++)           Sky[x][y][r] = Sky[x][y][r] + Sky[x - 1][y][r] + Sky[x][y - 1][r] -                          Sky[x - 1][y - 1][r];     for (int i = 1; i <= q; i++) {       int t, x1, y1, x2, y2;       cin >> t >> x1 >> y1 >> x2 >> y2;       t %= c;       cout << Sky[x2][y2][t] - Sky[x1 - 1][y2][t] - Sky[x2][y1 - 1][t] +                   Sky[x1 - 1][y1 - 1][t]            <<   n ;     }   }   