#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
// your code goes here
int n,q,c; cin>>n>>q>>c;
ll dp[c+1][101][101];
// Initialize all time layers to 0
for(int t=0;t<=c;t++){
for(int x=0;x<=100;x++){
for(int y=0;y<=100;y++){
dp[t][x][y]=0;
}
}
}
while(n--){
int x,y,s; cin>>x>>y>>s;
for(int t=0;t<=c;t++){
ll val= s+t;
val=val%(c+1);
dp[t][x][y]+=val;
}
}
for(int t=0;t<=c;t++){
for(int x=1;x<=100;x++){
for(int y=1;y<=100;y++){
dp[t][x][y]+=dp[t][x-1][y]+dp[t][x][y-1]-dp[t][x-1][y-1];
}
}
}
while(q--){
int t,x1,y1,x2,y2;
cin>>t>>x1>>y1>>x2>>y2;
t=t%(c+1);
cout<<dp[t][x2][y2]+dp[t][x1-1][y1-1]-dp[t][x1-1][y2]-dp[t][x2][y1-1]<<endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmludCBtYWluKCkgewogICAgLy8geW91ciBjb2RlIGdvZXMgaGVyZQogICAgaW50IG4scSxjOyBjaW4+Pm4+PnE+PmM7CiAgICBsbCBkcFtjKzFdWzEwMV1bMTAxXTsKICAgIC8vIEluaXRpYWxpemUgYWxsIHRpbWUgbGF5ZXJzIHRvIDAKICAgIGZvcihpbnQgdD0wO3Q8PWM7dCsrKXsKICAgICAgICBmb3IoaW50IHg9MDt4PD0xMDA7eCsrKXsKICAgICAgICAgICAgZm9yKGludCB5PTA7eTw9MTAwO3krKyl7CiAgICAgICAgICAgICAgICBkcFt0XVt4XVt5XT0wOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICB3aGlsZShuLS0pewogICAgICAgIGludCB4LHksczsgY2luPj54Pj55Pj5zOwogICAgICAgIGZvcihpbnQgdD0wO3Q8PWM7dCsrKXsKICAgICAgICAgICAgbGwgdmFsPSBzK3Q7CiAgICAgICAgICAgIHZhbD12YWwlKGMrMSk7CiAgICAgICAgICAgIGRwW3RdW3hdW3ldKz12YWw7CiAgICAgICAgfQogICAgfQogICAgCiAgICBmb3IoaW50IHQ9MDt0PD1jO3QrKyl7CiAgICAgICAgZm9yKGludCB4PTE7eDw9MTAwO3grKyl7CiAgICAgICAgICAgIGZvcihpbnQgeT0xO3k8PTEwMDt5KyspewogICAgICAgICAgICAgICAgZHBbdF1beF1beV0rPWRwW3RdW3gtMV1beV0rZHBbdF1beF1beS0xXS1kcFt0XVt4LTFdW3ktMV07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICB3aGlsZShxLS0pewogICAgICAgIGludCB0LHgxLHkxLHgyLHkyOwogICAgICAgIGNpbj4+dD4+eDE+PnkxPj54Mj4+eTI7CiAgICAgICAgdD10JShjKzEpOwogICAgICAgIGNvdXQ8PGRwW3RdW3gyXVt5Ml0rZHBbdF1beDEtMV1beTEtMV0tZHBbdF1beDEtMV1beTJdLWRwW3RdW3gyXVt5MS0xXTw8ZW5kbDsKICAgIH0KICAgIHJldHVybiAwOwp9