#include <cmath>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <set>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <climits>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <functional>
#include <cfloat>
#include <numeric>
#include <random>
#include <sstream>
#include <bitset>
#include <complex>
#include <chrono>
#include <cfloat>

using namespace std;

long long int gcd(long long int a, long long int b) {
	return b == 0 ? a : gcd(b, a % b);
}
long long int gx(long long int a, long long int b) {
	long long int ax{ 1 }, bx{ 0 }, ay{ 0 }, by{ 1 };
	const auto g = gcd(a, b);
	a /= g;
	b /= g;
	long long int r = b;
	while (r != 1) {
		const auto x = ax * a + bx * b;
		const auto y = ay * a + by * b;
		const auto q = x / y;
		r = x % y;
		const auto az = ax - q * ay;
		const auto bz = bx - q * by;
		ax = ay;
		bx = by;
		ay = az % b;
		by = bz % a;
	}
	return ay >= 0 ? ay : b + ay;;
}
int main() {
	int test_case; cin >> test_case;
	for (auto t = 0; t < test_case; ++t) {
		int n, s, k; cin >> n >> s >> k;
		const auto target = n - s;
		const auto g = gcd(n, k);
		if (target % g != 0) {
			cout << -1 << '\n';
		}
		else {
			const auto x = gx(k / g, n / g);
			cout << x * (target / g) % (n / g) << '\n';
		}
	}
}
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << endl;
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1) res *= x;
  res %= mod;
  return res;
}

struct MAT {
  vvll v = vvll(3, vll(3, 0));

  // MAT(vvll a) : v(a);

  MAT operator*(MAT a) {
    MAT res;
    rep(i, 3) rep(j, 3) rep(k, 3) res.v[i][j] += v[i][k] * a.v[k][j];
    return res;
  }

  vll operator*(vll a) {
    vll res = vll(3, 0);
    rep(i, 3) rep(k, 3) res[i] += v[i][k] * a[k];
    return res;
  }

  void print() {
    rep(i, 3) {
      rep(j, 3) cout << ' ' << v[i][j];
      ENDL;
    }
  }
  // http://satoh.cs.uec.ac.jp/ja/lecture/ComputerGraphics/2.pdf
};

signed main() {
  init();
  ll N;
  cin >> N;
  vvll X(N, vll(3, 1));
  rep(i, N) cin >> X[i][0] >> X[i][1];

  ll M;
  cin >> M;
  vector<MAT> mat;
  MAT m = {vvll({{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})};
  mat.pb(m);
  rep(i, M) {
    ll t;
    cin >> t;
    MAT a;
    if (t <= 2) {
      if (t == 1) a = {vvll({{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}})};
      if (t == 2) a = {vvll({{0, -1, 0}, {1, 0, 0}, {0, 0, 1}})};
    } else {
      ll p;
      cin >> p;
      if (t == 3) a = {vvll({{-1, 0, 2 * p}, {0, 1, 0}, {0, 0, 1}})};
      if (t == 4) a = {vvll({{1, 0, 0}, {0, -1, 2 * p}, {0, 0, 1}})};
    }
    m = a * m;
    mat.pb(m);
  }

  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll a, b;
    cin >> a >> b;
    b--;
    vll ans = mat[a] * X[b];
    cout << ans[0] << ' ' << ans[1] << endl;
  }

  return 0;
}