#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
//typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
//typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 200003, M = N;
//=======================
vi g[N];
vi a;
int n;
const int rtn = 142;
int cnt[N][rtn];
int tot[rtn];
int pl[N], bet[N];
int id(int x){
return x/rtn;
}
int solve(int x){
// cout<<x<<endl;
if(x<0) return -1;
int sab = 0;
for(int k: a)
{
sab += cnt[x][k];
// cout<<k<<" ";
}
//cout<<endl<<sab<<" "<<tot[x]<<endl;
if(sab == tot[x]) return solve(x-1);
return x;
}
int mark[N], P, LA;
int sol(int x, int w){
if(x<0) return LA;
int sab = cnt[x][w];
if(sab==0) return sol(x-1, w);
LA = x;
for(int k: a)
sab += cnt[x][k];
if(sab==tot[x]) return sol(x-1, w);
return x;
}
int win(int x){
int i = x*rtn, j = min(n-1, i+rtn-1);
int w;
Fo(w, j, i-1){
if(!mark[pl[w]]){
P = pl[w];
return sol(x, P);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i,k,j,q,x;
cin>>n;
int B;
fo(i, n){
int a, b;
cin>>a>>b;
pl[i] = a;
bet[i] = b;
//b[id(i)][a]
cnt[id(i)][a]++;
tot[id(i)]++;
B = id(i);
}
cin>>q;
while(q--){
cin>>k;
a.clear();
fo(i, k) cin>>x, a.pb(x), mark[x] = 1;
x = solve(B);
if(x==-1){
cout<<0<<" "<<0<<endl;
for(int x: a) mark[x] = 0;
continue;
}
x = win(x);
//x bucket mei P ki last bet
int lo = x*rtn, hi = lo+rtn-1;
hi = min(n-1, hi);
int ans = 0;
Fo(i, hi, lo-1){
if(mark[pl[i]]) continue;
if(pl[i]==P) ans = bet[i];
else break;
}
cout << P<<" "<<ans << endl;
for(int x: a) mark[x] = 0;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZ2MgZ2V0Y2hhcl91bmxvY2tlZAojZGVmaW5lIGZvKGksbikgZm9yKGk9MDtpPG47aSsrKQojZGVmaW5lIEZvKGksayxuKSBmb3IoaT1rO2s8bj9pPG46aT5uO2s8bj9pKz0xOmktPTEpCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgc2koeCkJc2NhbmYoIiVkIiwmeCkKI2RlZmluZSBzbCh4KQlzY2FuZigiJWxsZCIsJngpCiNkZWZpbmUgc3MocykJc2NhbmYoIiVzIixzKQojZGVmaW5lIHBpKHgpCXByaW50ZigiJWRcbiIseCkKI2RlZmluZSBwbCh4KQlwcmludGYoIiVsbGRcbiIseCkKI2RlZmluZSBwcyhzKQlwcmludGYoIiVzXG4iLHMpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgRiBmaXJzdAojZGVmaW5lIFMgc2Vjb25kCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSwgeC5lbmQoKQojZGVmaW5lIGNscih4KSBtZW1zZXQoeCwgMCwgc2l6ZW9mKHgpKQojZGVmaW5lIHNvcnRhbGwoeCkgc29ydChhbGw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