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

void dp(vector <vector <int>> & grid, int n, int m, int iban, int jban)
{

    vector <vector <long long>> sum(n, vector <long long> (m, 0));
    sum[0][0] = grid[0][0];
    sum[iban][jban] = LONG_MAX;
    for (int j = 1; j<m; j++)
    {
        if (sum[0][j-1] == LONG_MAX)
            sum[0][j]= LONG_MAX;
        else if (sum[0][j]!=LONG_MAX)
        sum[0][j]= sum[0][j-1]+ grid[0][j];
    }
    for (int i = 1; i<n; i++)
    {
        if (sum[i-1][0] == LONG_MAX)
            sum[i][0]= LONG_MAX;
        else if (sum[i][0]!=LONG_MAX)
            sum[i][0]= sum[i-1][0]+ grid[i][0];
    }
    for (int i=1; i<n; i++)
    {
        for (int j = 1; j<m; j++)
        {
            if (i != iban || j != jban)
                sum[i][j]= min(sum[i][j-1], sum[i-1][j])+grid[i][j];
        }
    }
    cout << sum[n-1][m-1] << " ";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector <vector <int>> grid(n, vector <int> (m));
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++)
        {
            cin >> grid[i][j];
        }
    }
    int q;
    cin >> q;
    for (int i=0; i<q; i++)
    {
        int x, y;
        cin >> x >> y;
        dp(grid, n, m, x-1, y-1);
    }
    return 0;
}