#include "iostream"
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    int doha[n][m];
    for(int i = 0; i < n;i++) for(int j = 0; j < m;j++)cin >> doha[i][j];
    int q;
    cin >> q;
    for(int i = 0; i < q;i++)
    {
        int x =0, y = 0;
        int xb,yb;
        cin >> yb >> xb;
        xb--;yb--;
        int n2 = doha[0][0];
        bool lastD = false;
        while(x != m-1  || y != n-1) {
            int d = -1, l = -1;
            if (y < n)d = x == xb && y + 1 == yb ? -1 : doha[y + 1][x];
            if (x < m-1)l = x + 1 == xb && y == yb ? -1 : doha[y][x + 1];
            if(d == -1 && l == -1)
            {
                n2-=doha[y][x];
                if(lastD)
                {
                    y--;
                    d = -1;
                }
                else
                {
                    x--;
                    l = -1;
                }
            }
            if (d < l || l == -1) {
                y++;
                lastD = true;
            } else {
                x++;
                lastD = false;
            }
            n2 += doha[y][x];
        }
        cout << n2 << "\n";
    }

}