#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> a;
vector<vector<int>> memo;
int n,m;
int x,y;

int guzva(int i, int j){
    if(i == x && j == y)
        return -1;
    if(memo[i][j] != -1)
        return memo[i][j];
    if(i==n-1 && j==m-1)
        return a[i][j];
    int desno, dole;
    if(i < n-1)
        dole = guzva(i+1,j);
    if(j < m-1)
        desno = guzva(i,j+1);
    if(i == n-1 || dole == -1){
        if(desno == -1)
            return -1;
        return memo[i][j] = a[i][j] + desno;
    }
    if(j == m-1 || desno == -1){
        if(dole == -1)
            return -1;
        return memo[i][j] = a[i][j] + dole;
    }
    return memo[i][j] = a[i][j] + min(desno,dole);
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> m;
    a.resize(n);
    memo.resize(n);
    for(int i=0; i<n; i++){
        a[i].resize(m);
        memo[i].assign(m,-1);
        for(int j=0; j<m; j++){
            cin >> a[i][j];
        }
    }
    int q;
    cin >> q;
    while(q--){
        cin >> x >> y;
        x--;y--;
        cout << guzva(0,0) << '\n';
        for(int i=0; i<n; i++){
            memo[i].assign(m,-1);
        }
    }
}