#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
using namespace std;
using ll = long long;

void A()
{
    string s; cin >> s;
    if (s[0] == s[1] && s[1] == s[2]) cout << "Won\n";
    else cout << "Lost\n";
}

void B()
{
    int n, limit; cin >> n >> limit;
    limit *= 100;
    for (int i = 1; i <= n; ++i) {
        int vol, percent; cin >> vol >> percent;
        limit -= vol*percent;
        if (limit < 0) {cout << i; return;}
    }
    cout << -1;
}

void C()
{
    int n; cin >> n;
    int a[n];
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        int mn = a[i];
        for (int j = i; j >= 0; --j) {
            if (a[j] < mn) mn = a[j];
            ans = max(ans, 1LL*mn*(i-j+1));
        }
    }
    cout << ans;
}

void D()
{
    int n; cin >> n;
    vector<string> v(n);
    vector<array<ll, 2>> dp(n+1);
    dp[0][0] = dp[0][1] = 1;
    for (int i = 1; i <= n; ++i) {
        cin >> v[i-1];
        if (v[i-1] == "OR") {
            dp[i][1] = dp[i-1][0] + 2*dp[i-1][1];
            dp[i][0] = dp[i-1][0];
        }
        else {
            dp[i][1] = dp[i-1][1];
            dp[i][0] = dp[i-1][1] + 2*dp[i-1][0];
        }
    }
    cout << dp[n][1];
}

void E()
{
    int n; cin >> n;
    vector<array<ll, 2>> pts(n);
    for (auto &[x, y] : pts)
        cin >> x >> y;
    int m; cin >> m;
    using row = array<ll, 3>;
    using matrix = array<row, 3>;
    vector<matrix> mat(m+1);
    mat[0][0][0] = mat[0][1][1] = mat[0][2][2] = 1;
    auto matmul = [&] (auto &a, auto &b) {
        array<array<ll, 3>, 3> res = array<array<ll, 3>, 3>();
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j)
                for (int k = 0; k < 3; ++k)
                    res[i][j] += a[i][k] * b[k][j];
        return res;
    };
    for (int i = 1; i <= m; ++i) {
        int type; cin >> type;
        if (type == 1) {
            mat[i] = matrix{row{0, 1, 0}, row{-1, 0, 0}, row{0, 0, 1}};
        }
        else if (type == 2) {
            mat[i] = matrix{row{0, -1, 0}, row{1, 0, 0}, row{0, 0, 1}};
        }
        else {
            int p; cin >> p;
            if (type == 3) {
                mat[i] = matrix{row{-1, 0, 2*p}, row{0, 1, 0}, row{0, 0, 1}};
            }
            else {
                mat[i] = matrix{row{1, 0, 0}, row{0, -1, 2*p}, row{0, 0, 1}};
            }
        }
        mat[i] = matmul(mat[i], mat[i-1]);
    }

    int q; cin >> q;
    while (q--) {
        int a, b; cin >> a >> b;
        auto [x, y] = pts[b-1];
        auto transform = mat[a];
        cout << x*transform[0][0] + y*transform[0][1] + transform[0][2] << ' ' << x*transform[1][0] + y*transform[1][1] + transform[1][2] << '\n';
    }
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
    E();
}
#include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
#define int long long
int safe_mod(int x, int m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}
pair<int,int> inv_gcd(int a, int b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};
    int s = b, t = a;
    int m0 = 0, m1 = 1;
    while (t) {
        int u = s / t;
        s -= t * u;
        m0 -= m1 * u;
        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}
pair<int,int> crt(const vector<int>& r, const vector<int>& m) {//if p.second == 0 then there is no answer 
    assert(r.size() == m.size());
    int n = (int)(r.size());
    int r0 = 0, m0 = 1;
    for (int i = 0; i < n; i++) {
        assert(1 <= m[i]);
        int r1 = safe_mod(r[i], m[i]), m1 = m[i];
        if (m0 < m1) {
            swap(r0, r1);
            swap(m0, m1);
        }
        if (m0 % m1 == 0) {
            if (r0 % m1 != r1) return {0, 0};
            continue;
        }
        int g, im;
        tie(g, im) = inv_gcd(m0, m1);
        int u1 = (m1 / g);
        if ((r1 - r0) % g) return {0, 0};
        int x = (r1 - r0) / g % u1 * im % u1;
        r0 += x * m0;
        m0 *= u1;
        if (r0 < 0) r0 += m0;
    }
    return {r0, m0};
}
signed main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--){
		int x,y,p,q;
		cin >> x >> y >> p >> q;
		int ans = (int)9e18;
		for(int i = x; i<x+y; i++){
			for(int j = p; j<p+q; j++){
				pair<int,int> a = crt({i,j},{x+y+x+y,p+q});
				if(a.second==0)continue;
				ans = min(ans,a.first);
			}
		}
		if(ans==(int)9e18){
			cout << "infinity\n";
		}
		else{
			cout << ans << "\n";
		}
	}
	return 0;
}