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

class Mat
{
public:
    ll a[2][2];
    ll b[2];

    Mat()
    {
        a[0][0] = 1;
        a[0][1] = 0;
        a[1][0] = 0;
        a[1][1] = 1;
        b[0] = 0;
        b[1] = 0;
    }

    Mat(ll m, ll p)
    {
        if (m == 1) {
            a[0][0] = 0;
            a[0][1] = 1;
            a[1][0] = -1;
            a[1][1] = 0;
            b[0] = 0;
            b[1] = 0;
        } else if (m == 2) {
            a[0][0] = 0;
            a[0][1] = -1;
            a[1][0] = 1;
            a[1][1] = 0;
            b[0] = 0;
            b[1] = 0;
        } else if (m == 3) {
            a[0][0] = -1;
            a[0][1] = 0;
            a[1][0] = 0;
            a[1][1] = 1;
            b[0] = 2 * p;
            b[1] = 0;
        } else {
            a[0][0] = 1;
            a[0][1] = 0;
            a[1][0] = 0;
            a[1][1] = -1;
            b[0] = 0;
            b[1] = 2 * p;
        }
    }

    Mat mul(Mat s)
    {
        // this * s
        Mat ans = Mat();
        ans.a[0][0] = this->a[0][0] * s.a[0][0] + this->a[0][1] * s.a[1][0];
        ans.a[0][1] = this->a[0][0] * s.a[0][1] + this->a[0][1] * s.a[1][1];
        ans.a[1][0] = this->a[1][0] * s.a[0][0] + this->a[1][1] * s.a[1][0];
        ans.a[1][1] = this->a[1][0] * s.a[0][1] + this->a[1][1] * s.a[1][1];
        ans.b[0] = this->a[0][0] * s.b[0] + this->a[0][1] * s.b[1] + this->b[0];
        ans.b[1] = this->a[1][0] * s.b[0] + this->a[1][1] * s.b[1] + this->b[1];

        return ans;
    }

    void print()
    {
        cout << a[0][0] << ' ' << a[0][1] << ' ' << b[0] << endl
             << a[1][0] << ' ' << a[1][1] << ' ' << b[1] << endl;
    }
};

int main()
{
    ll N;
    cin >> N;
    vector<pair<ll, ll>> P(N);
    for (ll i = 0; i < N; ++i)
        cin >> P[i].first >> P[i].second;
    ll M;
    cin >> M;
    //前処理　行列表現
    Mat mat[M + 1];
    mat[0] = Mat();
    for (ll i = 0; i < M; ++i) {
        ll n;
        cin >> n;
        Mat tmp;
        if (n == 1 || n == 2) {
            tmp = Mat(n, 0);
        } else {
            ll p;
            cin >> p;
            tmp = Mat(n, p);
        }
        mat[i + 1] = tmp.mul(mat[i]);
        //mat[i+1].print();
    }

    ll Q;
    cin >> Q;
    for (ll i = 0; i < Q; ++i) {  //クエリ
        ll A, B;
        cin >> A >> B;
        --B;

        Mat q = mat[A];

        ll x = P[B].first * q.a[0][0] + P[B].second * q.a[0][1] + q.b[0];
        ll y = P[B].first * q.a[1][0] + P[B].second * q.a[1][1] + q.b[1];

        cout << x << ' ' << y << endl;
    }
}
// #pragma GCC optimize("Ofast")	
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
 
const ll nax = 200002;
const ll p = 1e9 + 7;

ll dp[nax][2];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	vector<ll> mn(n + 2, INT_MAX), mx(n + 2, INT_MIN);
	for (int i = 0; i < n; ++i) {
		ll x, c;
		cin >> x >> c;
		mn[c] = min(mn[c], x);
		mx[c] = max(mx[c], x);
	}
	mn[n + 1] = mx[n + 1] = 0;
	mn[0] = mx[0] = 0;

	for (int i = 1; i <= n + 1; ++i) {
		if (mn[i] == INT_MAX) {
			dp[i][0] = dp[i - 1][0];
			dp[i][1] = dp[i - 1][1];
			mn[i] = mn[i - 1];
			mx[i] = mx[i - 1];
			continue;
		}
		dp[i][0] = mx[i] - mn[i] + min(abs(mx[i] - mx[i - 1]) + dp[i - 1][1],
									 abs(mx[i] - mn[i - 1]) + dp[i - 1][0]);
		dp[i][1] = mx[i] - mn[i] + min(abs(mn[i] - mx[i - 1]) + dp[i - 1][1],
									 abs(mn[i] - mn[i - 1]) + dp[i - 1][0]);
	}
	cout << dp[n + 1][0];
}