#include <iostream>
#include<vector>
using namespace std;


int calcMinCost(vector<vector<int>> costMat, int n, int m, int x, int y)
{

    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            sum += costMat[i][j];
        }
    }
    costMat[x][y] = sum;
    for (int i = 1; i < n; i++)
    {
        costMat[i][0] += costMat[i - 1][0];
    }

    for (int j = 1; j < m; j++) 
    
    {
        costMat[0][j] += costMat[0][j - 1];
    }
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            if (((i - 1) == x) && (j == y))
                costMat[i][j]+= costMat[i][j-1];
            else if(((i == x) && (j-1 == y)))
                costMat[i][j]+= costMat[i-1][j];
            else
                costMat[i][j] += min(costMat[i - 1][j], costMat[i][j - 1]);
        }
    }

    return costMat[n - 1][m - 1];
}

int main()
{
    int i, j, n, m, q;
    cin >> n >> m;
    vector<vector<int>> mat;
    mat.resize(n, vector<int>(m));
    for(i=0; i<n; i++)
        for (j = 0; j < m; j++)
        {
            cin >> mat[i][j];
        }
    cin >> q;
    vector<int> x;
    vector<int> y;
    vector<int> minput;
    x.resize(q);
    y.resize(q);
    minput.resize(q);
    for (i = 0; i < q; i++)
    {
        cin >> x[i] >> y[i];
    }

    for (i = 0; i < q; i++)
    {
        minput[i] = calcMinCost(mat, n, m, x[i]-1, y[i]-1);
    }

    for (i = 0; i < q; i++)
    {
        cout << minput[i];
        cout << "\n";
    }

    return 0;
}