#include <bits/stdc++.h>
using namespace std;
int n, f[200005], c[200005], re[200005], ren;
vector<int> a[200005];
bool p[200005];
priority_queue<pair<int, int> > b;
int read() {
  int x = 0;
  bool f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= ch == '-';
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return (x ^ -f) + f;
}
void write(int x) {
  char a[20], s = 0;
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  while (x) {
    a[s++] = x % 10 + '0';
    x = x / 10;
  }
  while (s--) putchar(a[s]);
}
void writeln(int x) {
  write(x);
  putchar('\n');
}
void writeb(int x) {
  write(x);
  putchar(' ');
}
void init() {
  int i, x;
  n = read();
  for ((i) = (1); (i) <= (n); ++i) {
    x = read();
    f[x] = read();
    a[x].push_back(f[x]);
  }
}
void work() {
  int q, i, j, s, t;
  for ((i) = (1); (i) <= (n); ++i)
    if (f[i]) b.push(make_pair(f[i], i));
  q = read();
  for ((i) = (1); (i) <= (q); ++i) {
    s = read();
    for ((j) = (1); (j) <= (s); ++j) p[c[j] = read()] = 1;
    while (!b.empty()) {
      if (!p[b.top().second]) break;
      re[++ren] = b.top().second;
      b.pop();
    }
    if (b.empty())
      printf("0 0\n");
    else {
      writeb(t = re[++ren] = b.top().second);
      b.pop();
      while (!b.empty()) {
        if (!p[b.top().second]) break;
        re[++ren] = b.top().second;
        b.pop();
      }
      if (!b.empty())
        writeln(*lower_bound(a[t].begin(), a[t].end(), b.top().first));
      else
        writeln(a[t].front());
    }
    for ((j) = (1); (j) <= (ren); ++j) b.push(make_pair(f[re[j]], re[j]));
    ren = 0;
    for ((j) = (1); (j) <= (s); ++j) p[c[j]] = 0;
  }
}
int main() {
  init();
  work();
  return 0;
}
