#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 5e2 + 9;
const int inf = 2e9;
const int LIM = maxn*maxn;
inline void mini(int &x,int y){if (x > y) x = y;}
inline void maxi(int &x,int y){if (x < y) x = y;}
int a[maxn],b[maxn],dp[LIM],NP[LIM],pre[LIM],suf[LIM];
bool mark[LIM];
void dynamic_programming(int n){
int sum = a[1];
dp[0] = b[1];
mark[a[1]] = 1;
mark[0] = 1;
for (int i = 2 ; i <= n ; i++){
sum += a[i];
for (int x = 0 ; x <= sum ; x++)
if (mark[x]) NP[x] = dp[x] + b[i];
for (int x = sum ; x >= a[i] ; x--)
if (mark[x - a[i]])
mark[x] = 1,maxi(NP[x],dp[x - a[i]]);
for (int x = 0 ; x <= sum ; x++){
dp[x] = NP[x];
NP[x] = 0;
}
}
int lim = LIM - 4;
for (int i = lim ; i >= 0 ; i--){
suf[i] = suf[i + 1];
if (mark[i]) maxi(suf[i],dp[i] + i);
}
for (int i = 0 ; i <= lim ; i++){
if (i > 0) pre[i] = pre[i - 1];
maxi(pre[i],dp[i]);
}
}
int getstate(int X,int Y){
int A,B;
//winning state
A = Y + 1;
if (suf[A] > X + Y) return 0;
//draw state
if (suf[A] == X + Y || pre[Y] >= X) return 1;
//lost state
return 2;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin >> n;
for (int i = 1 ; i <= n ; i++) cin >> a[i] >> b[i];
dynamic_programming(n);
int q,R[4];
R[0] = R[1] = R[2] = 0;
cin >> q;
while (q--){
int X,Y;
cin >> X >> Y;
R[getstate(X,Y)]++;
}
cout << R[0] << " " << R[1] << " " << R[2] << "\n";
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBsZGIgbG9uZyBkb3VibGUKI2RlZmluZSBmaSBmaXJzd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