#include <bits/stdc++.h>   using namespace std;   const int N = 200001;   int MAX[N];   vector<int> A[N];   int main() {     ios::sync_with_stdio(false);     set<pair<int, int> > S;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int u, v;       cin >> u >> v;       MAX[u] = v;       A[u].push_back(v);     }     for (int i = 1; i <= n; i++) {       if (MAX[i] > 0) S.insert({MAX[i], i});     }     int q;     cin >> q;     for (int i = 0; i < q; i++) {       int c;       cin >> c;       vector<int> temp;       for (int j = 0; j < c; j++) {         int u;         cin >> u;         temp.push_back(u);         S.erase({MAX[u], u});       }       if (S.size() > 1) {         pair<int, int> p = *S.rbegin(), p2;         S.erase(p);         temp.push_back(p.second);         p2 = *S.rbegin();         int l_lim = 0, u_lim = A[p.second].size();         while (l_lim < u_lim - 1) {           int cur = (l_lim + u_lim) / 2;           if (A[p.second][cur] > p2.first) {             u_lim = cur;           } else {             l_lim = cur;           }         }         if (A[p.second][l_lim] > p2.first) {           cout << p.second <<     << A[p.second][l_lim] <<   n ;         } else {           cout << p.second <<     << A[p.second][l_lim + 1] <<   n ;         }       } else if (S.size() == 1) {         pair<int, int> p = *S.rbegin();         cout << p.second <<     << A[p.second][0] <<   n ;       } else {         cout <<  0 0 n ;       }       while (!temp.empty()) {         if (MAX[temp.back()] > 0) S.insert({MAX[temp.back()], temp.back()});         temp.pop_back();       }     }     return 0;   }   