#include <bits/stdc++.h>
#define N 1100000006
using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;

    long long mat[n][m];

    for(int i = 0 ; i < n ; i++)
        for(int j = 0 ; j < m ; j++)
            cin >> mat[i][j];

    long long q;
    cin >>q;
    while(q--){
        long long dp[n][m], x ,y;
        cin >> x >> y;
        x--; y--;

        dp[0][0] = mat[0][0];

        for(int i = 1 ; i < n ; i++){
            if(x == 0 && y == i) dp[0][i] = N;
            else dp[0][i] = mat[0][i] + dp[0][i-1];
        }


        for(int i = 1 ; i < m ; i++){
            if(y == 0 && x == i) dp[i][0] = N;
            else dp[i][0] = mat[i][0] + dp[i-1][0];
        }

        for(int i = 1 ; i < n ; i++)
            for(int j = 1 ; j < m ; j++){
                if(i == x && j == y) dp[i][j] = N;
                else dp[i][j] = mat[i][j] + min(dp[i-1][j],dp[i][j-1]);
            }
        cout << dp[n-1][m-1]<< endl;
        /*for(int i = 0 ; i < n ; i++)
        {
            for(int j = 0 ; j < m ; j++)
                cout << dp[i][j] << ' ';
            cout << endl;
        }*/
    }

}