#include <iostream>
#include <vector>

using namespace std;

void ispis(vector<vector <long int>>& mat, int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }

}
void ispis(vector<vector <int>>& mat, int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }

}
void unos(vector<vector <int>>& mat, int n, int m) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> mat[i][j];
}
bool naPutu(vector<vector <long int>>& mat2, int n, int m, int qi, int qj) {
    int i = n, j = m;
    while (i > 0 && j > 0) {
        if (mat2[i][j - 1] < mat2[i - 1][j])
            --j;
        else
            --i;
        if (i == qi - 1 && j == qj - 1)
            return true;
    }
    return false;
}
void novimin(vector<vector <int>>& mat, vector<vector <long int>> mat2, int n, int m, int qi, int qj) {
    int i, j;
    if (qi == n) {
        i = n - 1;
        mat2[qi - 1][qj] = mat2[qi - 2][qj] + mat[qi - 1][qj];
        for (j = qj + 1; j < m; ++j)
            mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
        cout << mat2[n - 1][m - 1];
        return;
    }
    if (qj == m) {
        j = m - 1;
        mat2[qi][qj - 1] = mat2[qi][qj - 2] + mat[qi][qj - 1];
        cout << qi << " " << qj - 1 << " " << mat2[qi][qj];
        for (i = qi + 1; i < n; ++i)
            mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
        cout << mat2[n - 1][m - 1];
        return;
    }
    mat2[qi - 1][qj] = mat2[qi][qj];

    if (qi == 1) {
        i = qi;
        for (j = qj - 1; j < m; ++j) {
            mat2[i][j] = mat2[i][j - 1] + mat[i][j];
        }
        for (i = qi + 1; i < n; ++i) {
            for (j = qj - 1; j < m; ++j)
                mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
        }
        cout << mat2[n - 1][m - 1];
        return;
    }
    else
        if (qj == 1) {
            j = qj;
            for (i = qi - 1; i < n; ++i) {
                mat2[i][j] = mat2[i - 1][j] + mat[i][j];
            }
            for (i = qi + 1; i < n; ++i) {
                for (j = qj + 1; j < m; ++j)
                    mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
            }
            cout << mat2[n - 1][m - 1];
            return;
        }
    i = qi - 1;
    mat2[qi - 1][qj] = mat2[qi - 2][qj] + mat[qi - 1][qj];
    for (j = qj + 1; j < m; ++j)
        mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
    qj = qj - 1;
    mat2[qi][qj - 1] = mat2[qi][qj - 2] + mat[qi][qj - 1];
    for (i = qi + 1; i < n; ++i)
        mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
    for (i = qi; i < n; ++i) {
        for (j = qj; j < m; ++j) {
            mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
        }
    }
    cout << mat2[n - 1][m - 1];
}
void put(vector<vector <int>>& mat, vector<vector <long int>>& mat2, int qi, int qj, int n, int m) {
    if (qi == n && qj == m - 1) {
        cout << mat2[n - 2][m - 1] + mat[n - 1][m - 1];
        return;
    }
    else
        if (qj == m && qi == n - 1) {
            cout << mat2[n - 1][m - 2] + mat[n - 1][m - 1];
            return;
        }
    if (!naPutu) {
        cout << mat2[n - 1][m - 1];
        return;
    }
    novimin(mat, mat2, n, m, qi, qj);


}
void setup(vector<vector<int>>& mat, vector<vector<long int>>& mat2, int n, int m) {

    unos(mat, n, m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            mat2[i][j] = 0;
        }
    }

    mat2[0][0] = mat[0][0];

    for (int i = 1; i < n; ++i)
        mat2[i][0] += mat2[i - 1][0] + mat[i][0];
    for (int j = 1; j < m; ++j)
        mat2[0][j] += mat[0][j] + mat2[0][j - 1];
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            mat2[i][j] = min(mat2[i - 1][j], mat2[i][j - 1]) + mat[i][j];
        }
    }
}
int main()
{
    int n, m, num, qi[50000], qj[50000];
    cin >> n >> m;
    vector<vector<int>> mat(n, vector<int>(m));
    vector<vector<long int>> mat2(n, vector<long int>(m));
    setup(mat, mat2, n, m);
    cin >> num;
    for (int i = 0; i < num; i++) {
        cin >> qi[i] >> qj[i];
    }
    for (int i = 0; i < num; i++) {
        put(mat, mat2, qi[i], qj[i], n, m);
        cout << endl;
    }
    return 0;
}