#include <iostream>

using namespace std;
int main ()
{
    int n, m;
    cin >> n >> m;
    int matrica[n][m];
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=m; j++)
        {
            cin >> matrica[i][j];
        }
    }

    int q;
    cin >> q;
    for (int i=1; i<=q; i++)
    {
        int minnavijaca;
        int x, y;
        cin >> x >> y;
        if (x==1)
        {
            int a, b;
            a=0; b=0;
            for (int i=1; i<y; i++)
                a=a+matrica[1][i];
            for (int i=y-1; i<=m; i++)
                b=b+matrica[2][i];
            minnavijaca=a+b;
        }
        if (x==2)
        {
            int a=0;
            for (int i=1; i<=y+1; i++)
                a=a+matrica[1][i];
            cout << a << endl;
            int b=0;
            for (int i=y+1; i<=m; i++)
                b=b+matrica[2][i];
            int bmin=b;
            for (int i=y+2; i<=m; i++)
            {
                b=b-matrica[2][i-1]+matrica[1][i];
                if (b<bmin)
                    bmin=b;
            }
            minnavijaca=a+bmin;
        }
        cout << minnavijaca << endl;
    }



}