public static void main (String args[]) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int round = Integer.parseInt(reader.readLine());

        for (int i = 0; i < round; i++) {
            int operation_count = Integer.parseInt(reader.readLine());
            Heap heap = new Heap();

            for (int j = 0; j < operation_count; j++) {
                StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
                if ("1".equals(tokenizer.nextToken())) {
                    int number = Integer.parseInt(tokenizer.nextToken());
                    heap.add(number);
                } else {
                    System.out.println(heap.pop());
                }
            }
        }
    }