#include<stdio.h>
int b1,b,n,d,a,x,y,sack[30010][60],cards[30010][2];
int main()
{
scanf("%d%d%d",&b1,&n,&d);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&cards[i][1],&cards[i][0]);//cards[i][0] is profit and cards[i][1] is price
}
for(;d--;printf("%d\n",sack[y][b1]))
{
scanf("%d%d%d%d",&a,&b,&x,&y);
for(int i=0;i<=y;i++)
{
for(int j=0;j<=b1;j++)
{
sack[i][j]=0;
}
}
cards[a][1]=b;
//dp knapsack
for(int i=x;i<=y;i++)
{
for(int j=1;j<=b1;j++)
{
if(cards[i][1]<=j&&sack[i-1][j]<cards[i][0]+sack[i-1][j-cards[i][1]])
{
sack[i][j]=cards[i][0]+sack[i-1][j-cards[i][1]];
}
else
{
sack[i][j]=sack[i-1][j];
}
}
}
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KaW50IGIxLGIsbixkLGEseCx5LHNhY2tbMzAwMTBdWzYwXSxjYXJkc1szMDAxMF1bMl07CmludCBtYWluKCkKewogICAgc2NhbmYoIiVkJWQlZCIsJmIxLCZuLCZkKTsKICAgIAogICAgZm9yKGludCBpPTE7aTw9bjtpKyspCiAgICB7CiAgICAgICAgc2NhbmYoIiVkJWQiLCZjYXJkc1tpXVsxXSwmY2FyZHNbaV1bMF0pOy8vY2FyZHNbaV1bMF0gaXMgcHJvZml0IGFuZCBjYXJkc1tpXVsxXSBpcyBwcmljZQogICAgfQogICAgCiAgICBmb3IoO2QtLTtwcmludGYoIiVkXG4iLHNhY2tbeV1bYjFdKSkKICAgIHsKICAgICAgICAKICAgICAgICBzY2FuZigiJWQlZCVkJWQiLCZhLCZiLCZ4LCZ5KTsKICAgICAgICAKICAgICAgICBmb3IoaW50IGk9MDtpPD15O2krKykKICAgICAgICB7CiAgICAgICAgICAgIGZvcihpbnQgaj0wO2o8PWIxO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc2Fja1tpXVtqXT0wOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIAogICAgICAgIGNhcmRzW2FdWzFdPWI7CiAgICAgICAgCiAgICAgICAgLy9kcCBrbmFwc2FjawogICAgICAgIAogICAgICAgIGZvcihpbnQgaT14O2k8PXk7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGludCBqPTE7ajw9YjE7aisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZihjYXJkc1tpXVsxXTw9aiYmc2Fja1tpLTFdW2pdPGNhcmRzW2ldWzBdK3NhY2tbaS0xXVtqLWNhcmRzW2ldWzFdXSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBzYWNrW2ldW2pdPWNhcmRzW2ldWzBdK3NhY2tbaS0xXVtqLWNhcmRzW2ldWzFdXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBzYWNrW2ldW2pdPXNhY2tbaS0xXVtqXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9