#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int r, c;
  cin >> r >> c;
  
  vector<vector<ll>> a(r, vector<ll>(c - 1)), b(r - 1, vector<ll>(c));
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c - 1; j++) {
      cin >> a.at(i).at(j);
    }
  }
  for (int i = 0; i < r - 1; i++) {
    for (int j = 0; j < c; j++) {
      cin >> b.at(i).at(j);
    }
  }
  
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(2, 2e18)));
  vector<ll> dp2(c, 2e18), dp3(c, 0);
  dp[0][0][0] = 0;
  dp2[0] = 0;
  priority_queue<tuple<ll, ll, ll, ll>, vector<tuple<ll, ll, ll, ll>>, greater<tuple<ll, ll, ll, ll>>> pq;
  pq.push(make_tuple(0, 0, 0, 0));
  while (!pq.empty()) {
    auto [dist, ix, iy, iz] = pq.top();
    pq.pop();
    if (dp[ix][iy][iz] < dist) continue;
    if (iz == 0) {
      if (iy < c - 1 && dp[ix][iy + 1][iz] > dp[ix][iy][iz] + a[ix][iy]) {
        dp[ix][iy + 1][iz] = dp[ix][iy][iz] + a[ix][iy];
        pq.push(make_tuple(dp[ix][iy + 1][iz], ix, iy + 1, iz));
      }
      if (iy > 0 && dp[ix][iy - 1][iz] > dp[ix][iy][iz] + a[ix][iy - 1]) {
        dp[ix][iy - 1][iz] = dp[ix][iy][iz] + a[ix][iy - 1];
        pq.push(make_tuple(dp[ix][iy - 1][iz], ix, iy - 1, iz));
      }
      if (ix < r - 1 && dp[ix + 1][iy][iz] > dp[ix][iy][iz] + b[ix][iy]) {
        dp[ix + 1][iy][iz] = dp[ix][iy][iz] + b[ix][iy];
        pq.push(make_tuple(dp[ix + 1][iy][iz], ix + 1, iy, iz));
      }
      if (dp[ix][iy][iz + 1] > dp[ix][iy][iz] + 1) {
        dp[ix][iy][iz + 1] = dp[ix][iy][iz] + 1;
        pq.push(make_tuple(dp[ix][iy][iz + 1] , ix, iy, iz + 1));
      }
    }
    else {
      if (ix > 0 && dp[ix - 1][iy][iz] > dp[ix][iy][iz] + 1) {
        dp[ix - 1][iy][iz] = dp[ix][iy][iz] + 1;
        pq.push(make_tuple(dp[ix - 1][iy][iz] , ix - 1, iy, iz));
      }
      if (dp[ix][iy][iz - 1] > dp[ix][iy][iz]) {
        dp[ix][iy][iz - 1] = dp[ix][iy][iz];
        pq.push(make_tuple(dp[ix][iy][iz - 1] , ix, iy, iz - 1));
      }
    }
  }
  
  
  cout << min(dp[r - 1][c - 1][0], dp[r - 1][c - 1][1]) << '\n';
}
#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define debug(x) cerr << #x << ": " << x << endl;
#define fin(a) { cout << a << endl; return 0; }
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using priq     = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;

template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
    return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
    return out << '{' << p.first << ',' << p.second << '}';
}
template<class T, class U> pair<T,U> operator+(const pair<T,U> &l, const pair<T,U> &r) {
    return make_pair(l.first+r.first, l.second+r.second); 
}
template<class T, class U> pair<T,U> operator-(const pair<T,U> &l, const pair<T,U> &r) {
    return make_pair(l.first-r.first, l.second-r.second);
}
template<class T, class U> pair<T,U> operator*(const pair<T,U> &l, const pair<T,U> &r) {
    return make_pair(l.first*r.first, l.second*r.second);
}
template<class T, class U> pair<T,U> operator/(const pair<T,U> &l, const pair<T,U> &r) {
    return make_pair(l.first/r.first, l.second/r.second);
}
template<class T, class U> pair<T,U>& operator+=(pair<T,U> &l, const pair<T,U> &r) { return l = l+r; }
template<class T, class U> pair<T,U>& operator-=(pair<T,U> &l, const pair<T,U> &r) { return l = l-r; }
template<class T, class U> pair<T,U>& operator*=(pair<T,U> &l, const pair<T,U> &r) { return l = l*r; }
template<class T, class U> pair<T,U>& operator/=(pair<T,U> &l, const pair<T,U> &r) { return l = l/r; }

template<class T>
istream& operator>>(istream &in, vector<T> &v) {
    for(auto &&e : v) in >> e;
    return in;
}
template<class T>
ostream& operator<<(ostream &out, const vector<T> &v) {
    for(const auto &e : v) out << e << ' ';
    return out;
}

/*----------------------------------------------------------------------------------------------------*/


int main(){
    int n;
    cin >> n;
    V<Pl> ps(n);
    cin >> ps;

    int m;
    cin >> m;
    V<V<ll>> xs(m+1,V<ll>(3)), ys(m+1,V<ll>(3)); // vector = {a, b, c}, point = ax + by + c
    xs[0] = {1,0,0};
    ys[0] = {0,1,0};
    Rep(i,m) {
        int ty;
        cin >> ty;
        if(ty == 1) {
            xs[i] = ys[i-1];
            ys[i] = { -xs[i-1][0], -xs[i-1][1], -xs[i-1][2] };
        }
        if(ty == 2) {
            ys[i] = xs[i-1];
            xs[i] = { -ys[i-1][0], -ys[i-1][1], -ys[i-1][2] };
        }
        if(ty == 3) {
            ll p; cin >> p;
            ys[i] = ys[i-1];
            xs[i] = { -xs[i-1][0], -xs[i-1][1], -xs[i-1][2]+2*p };
        }
        if(ty == 4) {
            ll p; cin >> p;
            xs[i] = xs[i-1];
            ys[i] = { -ys[i-1][0], -ys[i-1][1], -ys[i-1][2]+2*p };
        }
    }

    int q;
    cin >> q;
    V<Pl> anss(q);
    rep(i,q) {
        int a, b;
        cin >> a >> b;
        b--;
        ll x = ps[b].fi;
        ll y = ps[b].se;
        ll ansx = xs[a][0]*x + xs[a][1]*y + xs[a][2];
        ll ansy = ys[a][0]*x + ys[a][1]*y + ys[a][2];
        anss[i] = make_pair(ansx, ansy);
    }
    for(Pl ans : anss) {
        cout << ans.fi << ' ' << ans.se << endl;
    }
}