#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int idemoMalenaZivotJePredNama( int n, int m,int mat[n][m])
{
    int dp[n][m];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if((i == 0)&&(j==0))
            {
                dp[i][j] = mat[i][j];
            }
            else if (i==0)
            {
                dp[i][j] = mat[i][j] + dp[i][j-1];
            }
            else if(j == 0)
            {
                dp[i][j] = mat[i][j] + dp[i-1][j];              
            }
            else{
                dp[i][j] = mat[i][j] + fmin(dp[i][j-1], dp[i-1][j]);
            }
        }
    }
    return dp[n - 1][m - 1];
}
int main()
{
    int n = 3, m = 4, z, g, k;
    scanf("%d%d", &n, &m);
    int a[n][m];
    for(int i = 0; i <n; i ++)
    {
        for(int j = 0; j < m; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    scanf("%d", &z);
    for(int i = 0; i < z; i++)
    {
        scanf("%d%d", &g, &k);
        g = g-1;
        k = k-1;
        int temp = a[g][k];
        a[g][k] = 1000000001;
        printf("%d \n", idemoMalenaZivotJePredNama(n, m, a));
        a[g][k] = temp;
    }
}