#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

int main()
{
    ull n, m;
    cin >> n >> m;
    vector<vector<ull>> a(n, vector<ull>(m, 0));
    for (ull i = 0; i < n; i++)
    {
        for (ull j = 0; j < m; j++)
        {
            cin >> a[i][j];
        }
    }
    ull q;
    cin >> q;
    while (q--)
    {
        ull x, y;
        cin >> x >> y;
        x--;
        y--;
        vector<vector<ull>> dp(n, vector<ull>(m, 0));
        dp[0][0] = a[0][0];
        for (ull i = 1; i < n; i++){
            dp[i][0] = dp[i - 1][0] + a[i][0];
            
        }
        for (ull j = 1; j < m; j++){
            dp[0][j] = dp[0][j - 1] + a[0][j]; 
        }
        dp[x][y] = -1;
        if(x==0){
            for(ull j=y; j<m; j++){
                dp[0][j]=-1;
            }
        }
        if(y==0){
            for(ull i=x; i<n; i++){
                dp[i][0]=-1;
            }
        }
        for (ull i = 1; i < n; i++)
        {
            for (ull j = 1; j < m; j++)
            {
                
                if (dp[i][j] != -1)
                {
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j];
                }
            }
        }

        cout << dp[n - 1][m - 1] << '\n';
    }
    return 0;
}