#include <bits/stdc++.h>
#define endl "\n"
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0), cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef priority_queue<PII, vector<PII>, greater<PII>> Qu;
inline int read() { char c = getchar(); int tot = 1; while ((c < '0' || c>'9') && c != '-') c = getchar(); if (c == '-') { tot = -1; c = getchar(); }int sum = 0; while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); }return sum * tot; }
inline void write(ll x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); }
const int N = 1e5 + 9;
const int INF = 0x3f3f3f3f;
ll M[N]; // 项目要求的数量
pair<int,ll> A[N];
unordered_map<int,ll> Com; // 公司有的人
vector<pair<int,ll>> C[N]; // i项目的要求
vector<pair<int,ll>> T[N]; // i项目的奖励
queue<int> P, q;
void Add(int t, int u) // t部门 u雇员
{
Com[t] += u;
int len = P.size();
while(len --){
int idx = P.front(); P.pop();
for(int i = 0; i < C[idx].size();i++){
int dep = C[idx][i].first, & num = C[idx][i].second;
if(num > 0 && Com[dep] >= num){
-- M[idx];
num = 0;
}
}
if(M[idx] == 0) {
q.push(idx);
}
else P.push(idx);
}
}
void solve()
{
int g ;cin >> g;
for(int i = 1;i <= g;i++){
int t, u; cin >> t >> u;
A[i] = {t, u};
}
int n ;cin >> n;
for(int i = 1;i <= n;i++){
cin >> M[i];
for(int k = 1;k <= M[i];k++){
int u, v; cin >>u >> v;
C[i].push_back({u,v});
}
int L; cin >> L;
for(int k = 1;k <= L;k++){
int u, v; cin >>u >> v;
T[i].push_back({u, v});
}
}
for(int i = 1;i <= n;i++){
if(M[i] == 0) q.push(i);
else P.push(i);
}
for(int i = 1;i <= g;i++){
Add(A[i].first ,A[i].second);
}
int ans = 0;
while(q.size()){
int idx = q.front() ;q.pop();
ans ++;
//cout << idx <<endl;
for(int i = 0;i < T[idx].size() ;i++){
Add(T[idx][i].first ,T[idx][i].second);
}
}
cout << ans <<endl;
return;
}
int main()
{
IOS;
int ___ = 1;
for (int i = 1; i <= ___; i++)
solve();
return 0;
}