#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int n,q,c; cin>>n>>q>>c;
int dp[c+1][101][101]={0};
while(n--){
int x,y,s; cin>>x>>y;
cin>>dp[0][x][y];
}
//
for(int x=1;x<=100;x++){
for(int y=1;y<=100;y++){
cout<<dp[0][x][y]<<" ";
}
cout<<endl;
}
//
for(int i=1;i<=c;i++){
for(int x=1;x<=100;x++){
for(int y=1;y<=100;y++){
dp[i][x][y]= 1+dp[i-1][x][y];
if(dp[i][x][y]>c) dp[i][x][y]=0;
}
}
}
for(int i=0;i<=c;i++){
for(int x=1;x<=100;x++){
for(int y=1;y<=100;y++){
dp[i][x][y]+=dp[i][x-1][y]+dp[i][x][y-1]-dp[i][x-1][y-1];
}
}
}
while(q--){
int t,x1,y1,x2,y2;
cin>>t>>x1>>y1>>x2>>y2;
if(t<=c){
cout<<dp[t][x2][y2]+dp[t][x1-1][y1-1]-dp[t][x1-1][y2]-dp[t][x2][y1-1]<<endl;
}
else{
cout<<"0\n";
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKCWludCBuLHEsYzsgY2luPj5uPj5xPj5jOwoJaW50IGRwW2MrMV1bMTAxXVsxMDFdPXswfTsKCXdoaWxlKG4tLSl7CgkJaW50IHgseSxzOyBjaW4+Png+Pnk7CgkJY2luPj5kcFswXVt4XVt5XTsKCX0KCS8vCglmb3IoaW50IHg9MTt4PD0xMDA7eCsrKXsKCQlmb3IoaW50IHk9MTt5PD0xMDA7eSsrKXsKCQkJY291dDw8ZHBbMF1beF1beV08PCIgIjsKCQl9CgkJY291dDw8ZW5kbDsKCX0KCS8vCglmb3IoaW50IGk9MTtpPD1jO2krKyl7CgkJZm9yKGludCB4PTE7eDw9MTAwO3grKyl7CgkJCWZvcihpbnQgeT0xO3k8PTEwMDt5KyspewoJCQkJZHBba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