#include <iostream>
using namespace std;
int main(){

    int n,m;
    cin>>n>>m;
    int a[n+5][m+5];
    int q;
    for (int i=0;i<n;i++){
        for (int idot=0;idot<m;idot++){
            cin>>a[i][idot];
        }
    }
    cin>>q;
    bool p=false;
    int x,y,zbir,min;
    for (int i=0;i<q;i++){
        cin>>x>>y;
        zbir=0;
        p=false;
        min=1000000000;
        for (int idot=0;idot<m;idot++){
        	zbir=0;
            p=false;
            for (int j=0;j<=idot;j++){
                zbir+=a[0][j];
                if ((x==1) and (j+1==y)) p=true;
         //       cout<<"1 "<<j+1<<"    "<<a[0][j]<<'\n';
            }
       //     cout<<endl;
            for (int j=idot;j<m;j++){
         //       cout<<"2 "<<j+1<<"    "<<a[1][j]<<'\n';
                if ((x==2) and (y==j+1)) p=true;
                zbir+=a[1][j];
            }
            if ((zbir<min) and (p==false)) min=zbir;
      //      cout<<"zbir: "<<zbir<<endl;
        }
            if ((zbir<min) and (p==false)) min=zbir;

        cout<<min<<"\n";
    }
    return 0;
}