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

void Fill(vector <vector <int>> & grid, vector <vector <long long>> & sum, int n, int m, int iban, int jban)
{
    for (int i=iban; i<n; i++)
    {
        for (int j = jban; j<m; j++)
        {
            if (i==0 && j==0)
                continue;
            if (i==0)
            {
                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];
                continue;
            }
            if (j==0)
            {
                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];
                continue;
            }
            if (i != iban || j != jban)
                sum[i][j]= min(sum[i][j-1], sum[i-1][j])+grid[i][j];
        }
    }
}

void Copy(vector <vector <int>> & grid, vector <vector <long long>> sum, int n, int m, int iban, int jban)
{
    sum[iban][jban] = LONG_MAX;
    Fill(grid, sum, n, m, iban, jban);
    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];
        }
    }
    vector <vector <long long>> sum(n, vector <long long> (m, 0));
    sum[0][0] = grid[0][0];
    Fill(grid, sum, n, m, 0, 0);
    int q;
    cin >> q;
    for (int i=0; i<q; i++)
    {
        int x, y;
        cin >> x >> y;
        Copy(grid, sum, n, m, x-1, y-1);
    }
    return 0;
}