/**
 *    author:  otera    
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60;
#define rep(i, n) for(int i = 0; i < n; ++ i)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
    int n, m; cin >> n;
    vector<int> x(n), y(n);
    rep(i, n) {
        cin >> x[i] >> y[i];
    }
    cin >> m;
    vector<int> r(m + 1, 0);
    vector<int> sx(m + 1, 1), sy(m + 1, 1);
    vector<int> nx(m + 1, 0), ny(m + 1, 0);
    rep(i, m) {
        int op; cin >> op;
        r[i + 1] = r[i];
        sx[i + 1] = sx[i], sy[i + 1] = sy[i];
        nx[i + 1] = nx[i], ny[i + 1] = ny[i];
        if(op == 1) {
            r[i + 1] ^= 1;
            swap(sx[i + 1], sy[i + 1]);
            swap(nx[i + 1], ny[i + 1]);
            sy[i + 1] *= (-1); //?
            ny[i + 1] *= (-1); //?
        } else if(op == 2) {
            r[i + 1] ^= 1;
            swap(sx[i + 1], sy[i + 1]);
            swap(nx[i + 1], ny[i + 1]);
            sx[i + 1] *= (-1); //?
            nx[i + 1] *= (-1); //?
        } else if(op == 3) {
            sx[i + 1] *= (-1);
            nx[i + 1] *= (-1);
            int p; cin >> p;
            nx[i + 1] += 2 * p;
        } else {
            sy[i + 1] *= (-1);
            ny[i + 1] *= (-1);
            int p; cin >> p;
            ny[i + 1] += 2 * p;
        }
    }
    int q; cin >> q;
    rep(_, q) {
        int a, b; cin >> a >> b;
        -- b;
        int X = x[b], Y = y[b];
        if(r[a]) swap(X, Y);
       // cerr << a << " " << sy[a] << " " << ny[a] << "\n";
        cout << X * sx[a] + nx[a] << " " << Y * sy[a] + ny[a] << "\n";
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(20);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

template<typename T>
T gcd(const T &a, const T &b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

template<typename T>
T lcm(const T &a, const T &b) {return a*(b/gcd(a,b));}

template<typename T>
T extgcd(const T &a, const T &b, T &x, T &y){
    if(b == 0) {x = 1, y = 0; return a;}
    T g = extgcd(b, a%b, y, x);
    y -= (a/b)*x;
    return g;
}

int mod(const long long &a, const int &m){
    int ret = a%m;
    return ret+(ret < 0? m : 0);
}

int modinv(const int &a, const int &m){ //aとmは互いに素
    int x, y;
    extgcd(a, m, x, y);
    return mod(x, m);
}

template<typename T>
T floor_sum(const T &n, const T &m, T a, T b){ //Σ(floor((a*i+b)/m)) (0<=i<n)
    T ret = (a/m)*(n*(n-1)/2)+(b/m)*n;
    a %= m, b %= m;
    T y = (a*n+b)/m;
    if(y == 0) return ret;
    ret += floor_sum(y, a, m, a*n-(m*y-b));
    return ret;
}

template<typename T>
pair<T, T> Chinese_reminder_theorem(const T &a1, const T &m1, const T &a2, const T &m2){
    T x, y, g = extgcd(m1, m2, x, y);
    if((a2-a1)%g != 0) return make_pair(0, -1);
    T m = m1*(m2/g);
    T tmp = mod(x*((a2-a1)/g), m2/g);
    T a = (m1*tmp+a1) % m;
    return make_pair(a, m);
}

bool prepare_Garner(vector<int> &a, vector<int> &m){ //mの各要素がそれぞれ互いに素とは限らない場合の前処理
    int n = a.size();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < i; j++){
            int g = gcd(m[i], m[j]);
            if((a[i]-a[j])%g != 0) return false;
            m[i] /= g, m[j] /= g;
            int gi = gcd(m[i], g), gj = g/gi;
            do{
                g = gcd(gi, gj);
                gi *= g, gj /= g;
            } while(g > 1);
            m[i] *= gi, m[j] *= gj;
        }
    }
    return true;
}

int Garner(vector<int> a, vector<int> m, const int &M){ //mの各要素はそれぞれ互いに素
    m.push_back(M);
    vector<long long> coeffs(m.size(), 1);
    vector<long long> constants(m.size(), 0);
    for(int k = 0; k < (int)a.size(); k++){
        long long x = a[k]-constants[k], y = modinv(coeffs[k], m[k]);
        long long t = mod(x*y, m[k]);
        for(int i = k+1; i < m.size(); i++){
            constants[i] += t*coeffs[i], constants[i] %= m[i];
            coeffs[i] *= m[k], coeffs[i] %= m[i];
        }
    }
    return constants.back();
}

int main(){
    int T; cin >> T;

    while(T--){
        ll X, Y, P, Q; cin >> X >> Y >> P >> Q;

        ll A = 2*(X+Y), B = P+Q;
        ll L = lcm(A, B);

        ll ans = LLONG_MAX;

        for(ll i = X; i < X+Y; i++){
            for(ll j = P; j < P+Q; j++){
                pll p = Chinese_reminder_theorem(i, A, j, B);
                if(p.second == -1) continue;
                if(p.first >= max(i, j)) chmin(ans, p.first);
                else chmin(ans, p.first+L);
            }
        }

        if(ans == LLONG_MAX) cout << "infinity\n";
        else cout << ans << '\n';
    }
}