There are n people taking part in auction today. The rules of auction are classical. There were n bids made, though it's not guaranteed they were from different people. It might happen that some people made no bids at all.

Each bid is define by two integers (ai, bi), where ai is the index of the person, who made this bid and bi is its size. Bids are given in chronological order, meaning bi < bi + 1 for all i < n. Moreover, participant never makes two bids in a row (no one updates his own bid), i.e. ai ≠ ai + 1 for all i < n.

Now you are curious with the following question: who (and which bid) will win the auction if some participants were absent? Consider that if someone was absent, all his bids are just removed and no new bids are added.

Note, that if during this imaginary exclusion of some participants it happens that some of the remaining participants makes a bid twice (or more times) in a row, only first of these bids is counted. For better understanding take a look at the samples.

You have several questions in your mind, compute the answer for each of them.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 200 000) — the number of participants and bids.

Each of the following n lines contains two integers ai and bi (1 ≤ ai ≤ n, 1 ≤ bi ≤ 109, bi < bi + 1) — the number of participant who made the i-th bid and the size of this bid.

Next line contains an integer q (1 ≤ q ≤ 200 000) — the number of question you have in mind.

Each of next q lines contains an integer k (1 ≤ k ≤ n), followed by k integers lj (1 ≤ lj ≤ n) — the number of people who are not coming in this question and their indices. It is guarenteed that lj values are different for a single question.

It's guaranteed that the sum of k over all question won't exceed 200 000.

Output

For each question print two integer — the index of the winner and the size of the winning bid. If there is no winner (there are no remaining bids at all), print two zeroes.

Examples

Input

6
1 10
2 100
3 1000
1 10000
2 100000
3 1000000
3
1 3
2 2 3
2 1 2


Output

2 100000
1 10
3 1000


Input

3
1 10
2 100
1 1000
2
2 1 2
2 2 3


Output

0 0
1 10

Note

Consider the first sample: 

  * In the first question participant number 3 is absent so the sequence of bids looks as follows: 
    1. 1 10
    2. 2 100
    3. 1 10 000
    4. 2 100 000
Participant number 2 wins with the bid 100 000.
  * In the second question participants 2 and 3 are absent, so the sequence of bids looks: 
    1. 1 10
    2. 1 10 000
The winner is, of course, participant number 1 but the winning bid is 10 instead of 10 000 as no one will ever increase his own bid (in this problem). 
  * In the third question participants 1 and 2 are absent and the sequence is: 
    1. 3 1 000
    2. 3 1 000 000
The winner is participant 3 with the bid 1 000. 
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.ObjectInputStream.GetField;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

public class Q2 {
	static long MOD = 1000000007;
	static boolean b[];
	static ArrayList<Integer>[] amp;
	static long sum[];
	static long ans = 0;
	static int p = 0;
	static FasterScanner sc = new FasterScanner();
	static Queue<String> q = new LinkedList<>();
	static ArrayList<String>[] arr;
	static ArrayList<Integer> parent = new ArrayList<>();
	public static void main(String[] args) throws IOException {
		BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
		TreeMap<Integer,Integer> hm = new TreeMap<>();
		HashMap<Integer,Integer> hm2 = new HashMap<>();
		HashMap<Integer,TreeSet<Integer>> tm = new HashMap<>();
		int n = sc.nextInt();
		for(int i = 0;i<n;i++){
			int x = sc.nextInt(), y = sc.nextInt();
			hm2.put(x, y);
			if(tm.containsKey(x)){
				tm.get(x).add(y);
			}
			else{
				TreeSet<Integer> hs = new TreeSet<>();
				hs.add(y);
				tm.put(x, hs);
			}
		}
		for(int x:hm2.keySet()){
			int temp = hm2.get(x);
			hm.put(temp, x);
		}
		int q = sc.nextInt();
		while(q-->0){
			int k = sc.nextInt();
			int arr[] = sc.nextIntArray(k);
			Pair prr[] = new Pair[k];
			for(int i = 0;i<k;i++) {
				if(tm.containsKey(arr[i])){
				prr[i] = new Pair(arr[i],tm.get(arr[i]).last());
				hm.remove(prr[i].v);
				}
				else prr[i] = new Pair(0,0);
			}
			//System.out.println(hm);
			if(hm.size()==0) {
				log.write("0 0\n");
			}
			else if(hm.size()==1){
				int key = hm.firstKey();
				//log.write("*");
				log.write(hm.get(key)+" "+tm.get(hm.get(key)).first()+"\n");
			}
			else{
				int key = hm.lastKey();
				int value = hm.get(key);
				hm.remove(key);
				log.write(value+" "+tm.get(value).ceiling(hm.lastKey())+"\n");
				hm.put(key, value);
			}
			for(int i = 0;i<k;i++) {
				if(prr[i].v!=0 && prr[i].u!=0)
				hm.put(prr[i].v,prr[i].u);	
			}
		}
		log.close();
	}
	static class SegmentTree{
		int st[][];
		SegmentTree(char arr[], int n){
			int size = 4*n+1;
			st = new int[size][3];
			build(arr,0,n-1,0);
		}
		int[] build(char arr[], int ss, int se, int si){
			if(ss==se){
				st[si][0] = 0;
				if(arr[ss]=='(')
				st[si][1] = 1;
				else st[si][2] = 1;
				return st[si];
			}
			int mid = (ss+se)/2;
			int amp[] = build(arr,ss,mid,si*2+1);
			int bmp[] = build(arr,mid+1,se,si*2+2);
			int t = Math.min(amp[1], bmp[2]);
			st[si][0] = amp[0]+bmp[0]+2*t;
			st[si][1] = amp[1]+bmp[1]-t;
			st[si][2] = amp[2]+bmp[2]-t;
			return st[si];
		}
		int[] getXor(int qs, int qe, int ss, int se, int si){
			if(qe<ss || qs>se){
				return null;
			}
			if(qs<=ss && qe>=se) return st[si];
			int mid = (ss+se)/2;
			int amp[]= getXor(qs,qe,ss,mid,2*si+1);
			int bmp[] = getXor(qs,qe, mid+1, se, 2*si+2);
			if(amp==null) return bmp;
			else if(bmp == null) return amp;
			int[] a=new int[3];
            int t=Math.min(amp[1],bmp[2]);
            a[0]=amp[0]+bmp[0]+2*t;
            a[1]=amp[1]+bmp[1]-t;
            a[2]=amp[2]+bmp[2]-t;
            return a;
		}
		void print(){
			for(int i = 0;i<st.length;i++){
				for(int j = 0;j<3;j++)
				System.out.print(st[i][j]+" ");
				System.out.println("----------------------");
			}
		}
	}
	static int convert(int x){
		int cnt = 0;
		String str = Integer.toBinaryString(x);
		//System.out.println(str);
		for(int i = 0;i<str.length();i++){
			if(str.charAt(i)=='1'){
				cnt++;
			}
		}
		int ans = (int) Math.pow(3, 6-cnt);
		return ans;
	}
	static class Node2{
		Node2 left = null;
		Node2 right = null;
		Node2 parent = null;
		int data;
	}
	static class BinarySearchTree{
		Node2 root = null;
		int height = 0;
		int max = 0;
		int cnt = 1;
		ArrayList<Integer> parent = new ArrayList<>();
		HashMap<Integer, Integer> hm = new HashMap<>();
		public void insert(int x){
			Node2 n = new Node2();
			n.data = x;
			if(root==null){
				root = n;
			}
			else{
				Node2 temp = root,temp2 = null;
				while(temp!=null){
					temp2 = temp;
					if(x>temp.data) temp = temp.right;
					else temp = temp.left;
				}
				if(x>temp2.data) temp2.right = n;
				else temp2.left = n;
				n.parent = temp2;
				parent.add(temp2.data);
			}
		}
		public Node2 getSomething(int x, int y, Node2 n){
			if(n.data==x || n.data==y) return n;
			else if(n.data>x && n.data<y) return n;
			else if(n.data<x && n.data<y) return getSomething(x,y,n.right);
			else return getSomething(x,y,n.left);
		}
		public Node2 search(int x,Node2 n){
			if(x==n.data){
				max = Math.max(max, n.data);
				return n;
			}
			if(x>n.data){
				max = Math.max(max, n.data);
				return search(x,n.right);
			}
			else{
				max = Math.max(max, n.data);
				return search(x,n.left);
			}
		}
		public int getHeight(Node2 n){
			if(n==null) return 0;
			height = 1+ Math.max(getHeight(n.left), getHeight(n.right));
			return height;
		}
	}
	static long findDiff(long[] arr, long[] brr, int m){
		int i = 0, j = 0;
		long fa = 1000000000000L;
		while(i<m && j<m){
			long x = arr[i]-brr[j];
			if(x>=0){
				if(x<fa) fa = x;
				j++;
			}
			else{
				if((-x)<fa) fa = -x;
				i++;
			}
		}
		return fa;
	}
	public static long max(long x, long y, long z){
		if(x>=y && x>=z) return x;
		if(y>=x && y>=z) return y;
		return z;
	}
	public static void bfs(){
		if(!q.isEmpty()){
			String x = q.poll();
			int y = Integer.parseInt(q.poll());
			arr[p].add(x);
			//System.out.println(x+" "+y+" "+p);
			for(int i= 0;i<y;i++){
				p++;
				bfs();
				p--;
			}
		}
	}
	public static void buildGraph(int n){
		for(int i =0;i<n;i++){
			int x = sc.nextInt(), y = sc.nextInt();
			amp[--x].add(--y);
			amp[y].add(x);
		}
	}
	public static void dfs(int x){
		ans++;
		b[x] = true;
		for(int i =0 ;i<amp[x].size();i++){
			if(!b[amp[x].get(i)]){
				dfs(amp[x].get(i));
			}
		}
	}
	public static void seive(long n){
		b = new boolean[(int) (n+1)];
		Arrays.fill(b, true);
		for(int i = 2;i<=n;i++){
			if(b[i]){
				for(int p = 2*i;p<=n;p++){
					b[p] = false;
				}
			}
		}
	}
	static long modInverse(long a, long mOD2){
	            return  power(a, mOD2-2, mOD2);
	}
	static long power(long x, long y, long m)
	{
	    if (y == 0)
	        return 1;
	    long p = power(x, y/2, m) % m;
	    p = (p * p) % m;
	 
	    return (y%2 == 0)? p : (x * p) % m;
	}
	static long power2(long x,BigInteger y,long m){
		long ans = 1;
		BigInteger two = new BigInteger("2");
		while(y.compareTo(BigInteger.ZERO)>0){
			if(y.getLowestSetBit()==y.bitCount()){
				x = (x*x)%MOD;
				y = y.divide(two);
			}
			else{
				ans = (ans*x)%MOD;
				y = y.subtract(BigInteger.ONE);
			}
		}
		return ans;
	}
	static BigInteger power2(BigInteger x, BigInteger y, BigInteger m){
		BigInteger ans = new BigInteger("1");
		BigInteger one  = new BigInteger("1");
		BigInteger two  = new BigInteger("2");
		BigInteger zero  = new BigInteger("0");
		while(y.compareTo(zero)>0){
			//System.out.println(y);
			if(y.mod(two).equals(one)){
				ans = ans.multiply(x).mod(m);
				y = y.subtract(one);
			}
			else{
				x = x.multiply(x).mod(m);
				y = y.divide(two);
			}
		}
		return ans;
	}
	static BigInteger power(BigInteger x, BigInteger y, BigInteger m)
	{
	    if (y.equals(0))
	        return (new BigInteger("1"));
	    BigInteger p = power(x, y.divide(new BigInteger("2")), m).mod(m);
	    p = (p.multiply(p)).mod(m);
	 
	    return (y.mod(new BigInteger("2")).equals(0))? p : (p.multiply(x)).mod(m);
	}
	static long d,x,y;
	public static void extendedEuclidian(long a, long b){
		if(b == 0) {
	        d = a;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclidian(b, a%b);
	        int temp = (int) x;
	        x = y;
	        y = temp - (a/b)*y;
	    }
	}
	
	public static long gcd(long n, long m){
		if(m!=0) return gcd(m,n%m);
		else return n;
	}
	
	static class Pair implements Comparable<Pair> {
		int u;
		int v;
		public Pair(){
			
		}
		public Pair(int u, int v) {
			this.u = u;
			this.v = v;
		}
		
		public int hashCode() {
			int hu = (int) (u ^ (u >>> 32));
			int hv = (int) (v ^ (v >>> 32));
			return 31*hu + hv;
		}

		public boolean equals(Object o) {
			Pair other = (Pair) o;
			return (u == other.u && v == other.v);
		}

		public int compareTo(Pair other) {
			return Integer.compare(u, other.u) != 0 ? (Integer.compare(u, other.u)) : (Integer.compare(v, other.v));
		}

		public String toString() {
			return "[u=" + u + ", v=" + v + "]";
		}
	}
	public static class FasterScanner {
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
 
		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = System.in.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}
 
		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}
 
		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}
 
		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
 
		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
	        
	    public int[] nextIntArray(int n) {
	        return nextIntArray(n, 0);
	    }
	    
	    public int[] nextIntArray(int n, int off) {
	    	int[] arr = new int[n + off];
	    	for (int i = 0; i < n; i++) {
	    		arr[i + off] = nextInt();
	    	}
	    	return arr;
	    }
	    
	    public long[] nextLongArray(int n) {
	    	return nextLongArray(n, 0);
	    }
        
		public long[] nextLongArray(int n, int off) {
		    long[] arr = new long[n + off];
		    for (int i = 0; i < n; i++) {
		        arr[i + off] = nextLong();
		    }
		    return arr;
		}
 
	    private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
 
		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
	}
}