This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems.

You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:

  * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); 
  * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. 



Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if:

  * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; 
  * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. 



Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:

  * [10, 20, 20] lexicographically less than [10, 21, 1], 
  * [7, 99, 99] is lexicographically less than [10, 21, 1], 
  * [10, 21, 0] is lexicographically less than [10, 21, 1]. 



You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.

For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.

Input

The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a.

The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).

The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests.

The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests.

Output

Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.

Examples

Input


3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3


Output


20
10
20
10
20
10


Input


7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4


Output


2
3
2
3
2
3
1
1
3

Note

In the first example, for a=[10,20,10] the optimal subsequences are: 

  * for k=1: [20], 
  * for k=2: [10,20], 
  * for k=3: [10,20,10].

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Data structures

Greedy algorithms


Solutions:
import copy
b = []
a = []
rezult = ''
n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(1, m + 1):
	(k, pos) = map(int, input().split())
	b = copy.deepcopy(a)
	b.reverse()
	for j in range(1, n - k + 1):
		b.remove(min(b))
	b.reverse()
	rezult = rezult + '\n' + str(b[pos - 1])
print(rezult)


n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
for i in range(m):
	(kj, posj) = map(int, input().split())
	a_copy = [x for x in a]
	for u in range(n - kj):
		mid = 0
		for uuu in range(len(a_copy)):
			if a_copy[uuu] <= a_copy[mid]:
				mid = uuu
		a_copy.pop(mid)
	print(a_copy[posj - 1])


import sys
input = sys.stdin.readline
from operator import itemgetter

def updatebit(BITTree, n, i, v):
	while i <= n:
		BITTree[i] += v
		i += i & -i

def bisect_left(val):
	sum_ = 0
	pos = 0
	bit_length = (n + 1).bit_length()
	for i in range(bit_length, -1, -1):
		k = pos + (1 << i)
		if k < n + 1 and sum_ + l[k] < val:
			sum_ += l[k]
			pos += 1 << i
	return pos
n = int(input())
l = [0] * (n + 1)
a = [int(i) for i in input().split() if i != '\n']
empty = []
for i in range(len(a)):
	empty.append([a[i], i + 1])
empty.sort(key=lambda x: x[1])
empty.sort(key=lambda x: x[0], reverse=True)
m = int(input())
info = [list(map(int, input().split())) + [i] for i in range(m) if i != '\n']
info = sorted(info, key=itemgetter(0))
(bit, ans, cnt) = ([0] * (n + 1), [0] * m, 0)
for i in range(m):
	(k, pos, ind) = info[i]
	while cnt < k:
		updatebit(l, n, empty[cnt][1], 1)
		cnt += 1
	ans[ind] = a[bisect_left(pos)]
for i in ans:
	sys.stdout.write(str(i) + '\n')


from collections import defaultdict
import sys as _sys

def main():
	(n,) = _read_ints()
	a = tuple(_read_ints())
	(m,) = _read_ints()
	queries = (tuple(_read_ints()) for i_query in range(m))
	result = process_queries(a, queries)
	print(*result, sep='\n')

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split())

def process_queries(sequence, queries):
	sequence = tuple(sequence)
	indices_by_values = defaultdict(list)
	for (i, x) in enumerate(sequence):
		indices_by_values[x].append(i)
	enumerated_queries = sorted(enumerate(queries), key=lambda iv: iv[1][0])[::-1]
	queries_responses = [None] * len(enumerated_queries)
	selections_tree = [0] * (len(sequence) + 1)
	k = 0
	for (value, indices) in sorted(indices_by_values.items(), reverse=True):
		for index_to_select in indices:
			_fenwick_tree_add(selections_tree, index_to_select, 1)
			k += 1
			while enumerated_queries and enumerated_queries[-1][1][0] == k:
				(query_index, (_k, subseq_index)) = enumerated_queries.pop()
				seq_index = _find_seq_index_by_subseq_index(selections_tree, subseq_index)
				queries_responses[query_index] = sequence[seq_index]
	return queries_responses

def _find_seq_index_by_subseq_index(tree, subseq_i):
	min_i = 0
	max_i = len(tree) - 1
	while min_i != max_i:
		mid_i = (min_i + max_i) // 2
		if _fenwick_tree_prefix_sum(tree, mid_i) < subseq_i:
			min_i = mid_i + 1
		else:
			max_i = mid_i
	return min_i

def _fenwick_tree_prefix_sum(tree, i):
	i += 1
	result = 0
	while i != 0:
		result += tree[i]
		i -= i & -i
	return result

def _fenwick_tree_add(tree, i, x):
	i += 1
	while i < len(tree):
		tree[i] += x
		i += i & -i
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class FenwickTree:

	def __init__(self, x):
		self.bit = x
		for i in range(len(x)):
			j = i | i + 1
			if j < len(x):
				x[j] += x[i]

	def update(self, idx, x):
		while idx < len(self.bit):
			self.bit[idx] += x
			idx |= idx + 1

	def query(self, end):
		x = 0
		while end:
			x += self.bit[end - 1]
			end &= end - 1
		return x

	def findkth(self, k):
		idx = -1
		for d in reversed(range(len(self.bit).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(self.bit) and k >= self.bit[right_idx]:
				idx = right_idx
				k -= self.bit[idx]
		return idx + 1
from collections import deque

def main():
	n = int(input())
	vals = list(map(int, input().split()))
	sorts = [(vals[s], s) for s in range(n)]
	sorts.sort(key=lambda x: [x[0], -x[1]])
	sorts = deque(sorts)
	BIT = FenwickTree([1] * n)
	m = int(input())
	quer = []
	for s in range(m):
		(k, pos) = map(int, input().split())
		quer.append((k, pos))
	dict1 = {}
	new = quer.copy()
	new.sort(key=lambda x: x[0])
	k = n
	while k > 0:
		if len(new) > 0:
			while len(new) > 0 and new[-1][0] == k:
				v0 = new.pop()
				shift = BIT.findkth(v0[1])
				ans = vals[shift - 1]
				dict1[v0] = ans
			k -= 1
			v1 = sorts.popleft()
			BIT.update(v1[1] + 1, -1)
		else:
			break
	for s in range(m):
		query = quer[s]
		print(dict1[query])
main()


n = int(input())
data = input().split()
data1 = []
for i in range(n):
	data1.append((int(data[i]), i))
data1.sort(key=lambda x: (x[0], -x[1]))
for i in range(int(input())):
	(k, pos) = map(int, input().split())
	temp = sorted(data1[len(data1) - k:], key=lambda x: x[1])
	print(temp[pos - 1][0])


import sys
input = sys.stdin.buffer.readline
mx = 2 * 10 ** 5 + 1
bit = [0] * mx

def add(idx):
	idx += 1
	while idx < mx:
		bit[idx] += 1
		idx += idx & -idx

def lower_bound(val):
	pos = 0
	tot = 0
	for i in range(20, -1, -1):
		if pos + (1 << i) < mx and tot + bit[pos + (1 << i)] < val:
			tot += bit[pos + (1 << i)]
			pos += 1 << i
	return pos
n = int(input())
a = list(map(int, input().split()))
new_el = sorted(list(range(n)), key=lambda i: (-a[i], i))
m = int(input())
queries = []
answers = [0] * m
for i in range(m):
	(k, p) = map(int, input().split())
	queries.append([k, p, i])
queries.sort()
curr_len = 0
for i in range(m):
	(k, pos, query_idx) = queries[i]
	while curr_len < k:
		add(new_el[curr_len])
		curr_len += 1
	answers[query_idx] = a[lower_bound(pos)]
print(*answers)


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
n = I()
d = {}
for i in sorted(enumerate(R()), reverse=True, key=lambda x: x[1]):
	if i[1] not in d:
		d[i[1]] = []
	d[i[1]] += (i,)
	d[i[1]].sort(key=lambda x: x[0])
a = []
for i in d:
	a.extend(d[i])
m = []
x = []
for i in range(n):
	x += (a[i],)
	x.sort(key=lambda x: x[0])
	m.append(x.copy())
for i in range(I()):
	(k, pos) = R()
	print(m[k - 1][pos - 1][1])


def test(a, k, pos):
	r = list(reversed(sorted(a.copy())))
	countLast = r[k - 1]
	s = 0
	for i in range(k, len(r)):
		if r[i] == countLast:
			s += 1
	ans = []
	a = list(reversed(a))
	for i in range(len(a)):
		if a[i] > countLast or (a[i] == countLast and s == 0):
			ans.append(a[i])
		elif a[i] == countLast:
			s -= 1
	print(list(reversed(ans))[pos - 1])
n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	test(a, k, pos)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
res = []
for i in range(m):
	(k, pos) = map(int, input().split())
	mas1 = []
	for j in range(n):
		mas1.append(a[j])
	mas2 = sorted(mas1)
	mas1.reverse()
	for j in range(n - k):
		mas1.remove(mas2[j])
	mas1.reverse()
	res.append(mas1[pos - 1])
for i in range(m):
	print(res[i])


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	b.append([a[i], -i])
b.sort(reverse=True)
for i in range(int(input())):
	(k, pos) = map(int, input().split())
	ans = 0
	z = []
	for i in range(k):
		ans += b[i][0]
		z.append([-b[i][1], b[i][0]])
	z.sort()
	print(z[pos - 1][1])


import sys as _sys

def main():
	t = 1
	for i_t in range(t):
		(n,) = _read_ints()
		a = tuple(_read_ints())
		(m,) = _read_ints()
		queries = (tuple(_read_ints()) for i_query in range(m))
		result = process_queries(a, queries)
		print(*result, sep='\n')

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split())

def process_queries(sequence, queries):
	sequence = tuple(sequence)
	sorted_sequence = sorted(sequence, reverse=True)
	for (k, pos) in queries:
		pos -= 1
		k_max_elems = sorted_sequence[:k][::-1]
		seq = sequence
		subseq = []
		while len(subseq) < k:
			for i_next_elem in range(len(k_max_elems)):
				next_elem = k_max_elems[i_next_elem]
				seq_after = seq[seq.index(next_elem) + 1:]
				elems_remain = k_max_elems[:i_next_elem] + k_max_elems[i_next_elem + 1:]
				if _contains_elems(seq_after, elems_remain):
					seq = seq_after
					k_max_elems = elems_remain
					subseq.append(next_elem)
					break
		yield subseq[pos]

def _contains_elems(seq, elems):
	seq = sorted(seq)
	subseq = sorted(elems)
	i_seq = 0
	i_subseq = 0
	while i_seq < len(seq) and i_subseq < len(subseq):
		if seq[i_seq] == subseq[i_subseq]:
			i_subseq += 1
		i_seq += 1
	return i_subseq == len(subseq)
main()


def get_index(lt, vec):
	max_el = 0
	pos = None
	for i in range(len(vec)):
		if i not in lt:
			if max_el < vec[i]:
				max_el = vec[i]
				pos = i
	return pos
n = int(input())
vec = [int(x) for x in input().split()]
lst = [[vec.index(max(vec))]]
for i in range(n - 1):
	lt = list(lst[i])
	ind = get_index(lt, vec)
	lt.append(ind)
	lst.append(lt)
for l in lst:
	l.sort()
m = int(input())
for i in range(m):
	(k, pos) = [int(x) for x in input().split()]
	print(vec[lst[k - 1][pos - 1]])


import sys

def main():
	n = int(input())
	al = [[int(x), n - i] for (i, x) in enumerate(input().split())]
	al.sort()
	al.reverse()
	arr = [[[] for i in range(n)] for i in range(n)]
	for i in range(1, n + 1):
		for j in range(i - 1, n):
			arr[j][n - al[i - 1][1]] = al[i - 1][0]
	for i in range(n):
		arr[i] = list(filter(None, arr[i]))
	q = int(input())
	for _ in range(q):
		(k, ind) = map(int, input().split())
		print(arr[k - 1][ind - 1])
main()


n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
c = []
c.append(a)
for i in range(1, n + 1):
	k = len(c[i - 1]) - 1 - c[i - 1][::-1].index(b[i - 1])
	c.append(c[i - 1][0:k] + c[i - 1][k + 1:])
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	print(c[len(c) - k - 1][pos - 1])


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a * a + b * b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		print(m, avl.get_at(m)[0])
		if avl.get_at(m)[0] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
l = list(map(int, input().split()))
d = dict()
for i in range(n):
	if l[i] in d:
		d[l[i]].append(i)
	else:
		d.update({l[i]: deque([i])})
rt = [] + l
l.sort(reverse=True)
ind1 = [0] * n
for i in range(n):
	ind1[i] = d[l[i]][0]
	d[l[i]].popleft()
q = int(input())
a = [0] * q
b = [0] * q
d = dict()
for i in range(q):
	(a[i], b[i]) = map(int, input().split())
	if a[i] in d:
		d[a[i]].append(b[i])
	else:
		d.update({a[i]: [b[i]]})
c = -1
ans = dict()
for i in sorted(d.keys()):
	for j in sorted(d[i]):
		ai = i
		bi = j
		while c < ai - 1:
			c += 1
			avl.add(ind1[c], ind1[c])
		ans.update({(ai, bi): rt[avl.get_at(bi - 1)[0]]})
for i in range(q):
	print(ans[a[i], b[i]])


import sys
import bisect
import heapq
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
n = int(ri())
ar = Ri()
arr = [(ar[i], i) for i in range(n)]
arr.sort(key=lambda x: -x[0])
for _ in range(int(ri())):
	(a, b) = Ri()
	temp = []
	for i in range(a):
		temp.append(arr[i][1])
	temp.sort()
	print(ar[temp[b - 1]])


def f(a):
	return -a[1]
import sys
fin = sys.stdin
n = int(input())
arr = list(map(int, fin.readline().split()))
arr_ind = list()
for i in range(n):
	arr_ind.append([arr[i], -i])
arr_ind.sort()
arr_ind.reverse()
ans_array = list()
for k in range(1, n + 1):
	ans = arr_ind[:k]
	ans.sort(key=f)
	ans_array.append(ans)
tests = int(input())
for test in range(tests):
	(k, pos) = map(int, input().split())
	print(ans_array[k - 1][pos - 1][0])


from math import *
import os, sys
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	b.append((-a[i], i))
b.sort()
for i in range(int(input())):
	(k, pos) = map(int, input().split())
	tmp = []
	for j in range(k):
		tmp.append(b[j][1])
	tmp.sort()
	print(a[tmp[pos - 1]])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	save = []
	for j in range(len(a)):
		save.append(a[j])
	ans = []
	for x in range(k):
		maximum = 0
		pr = -1
		for j in range(len(save)):
			if save[j] > maximum:
				maximum = save[j]
				pr = j
		ans.append(maximum)
		save.pop(pr)
	ans.sort()
	answer = []
	for i in range(len(a)):
		if a[i] in ans:
			answer.append(a[i])
			ans.pop(ans.index(a[i]))
	print(answer[pos - 1])


import sys

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
lines = sys.stdin.readlines()
n = int(lines[0])
aa = [(a, i) for (i, a) in enumerate(map(int, lines[1].split()))]
m = int(lines[2])
qs = [None] * m
ans = [None] * m
for i in range(m):
	(k, pos) = map(int, lines[i + 3].split())
	qs[i] = (pos, k, i)
qs.sort(key=lambda x: x[1])
aa.sort(key=lambda x: x[1])
aa.sort(key=lambda x: x[0], reverse=True)
avl = AvlTree()
s = 0
for (pos, k, i) in qs:
	for (a, j) in aa[s:k]:
		avl.add(j, a)
	ans[i] = str(avl.get_at(pos - 1)[1])
	s = k
print('\n'.join(ans))


import math

def getFirstSetBitPos(n):
	return math.log2(n & -n) + 1

def find_div(x):
	ls = []
	for i in range(2, int(x ** 0.5) + 1):
		if x % i == 0:
			ls.append(i)
			if i != x // i:
				ls.append(x // i)
	return sorted(ls)
from collections import Counter
n = int(input())
ar = [(int(x), i) for (i, x) in enumerate(input().split())]
arr = sorted(ar, key=lambda x: (x[0], -1 * x[1]))
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	ls = sorted(arr[-k:], key=lambda x: x[1])
	print(ls[pos - 1][0])


n = int(input())
a = list(map(int, input().split()))
a1 = sorted(a, key=lambda x: -x)
q = int(input())
for i in range(q):
	(k, pos) = map(int, input().split())
	pos -= 1
	notused = {}
	for j in a1[:k]:
		if j in notused:
			notused[j] += 1
		else:
			notused[j] = 1
	for j in a:
		if j in notused:
			if pos > 0:
				if notused[j] > 1:
					notused[j] -= 1
				else:
					notused.pop(j)
				pos -= 1
			else:
				print(j)
				break


def main():
	import os
	from io import BytesIO
	input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
	n = int(input())
	a = [(int(el), -ind) for (ind, el) in enumerate(input().split())]
	a.sort(reverse=True)
	for i in range(int(input())):
		(x, y) = map(int, input().split())
		t = sorted(a[:x], key=lambda x: -x[1])
		print(t[y - 1][0])
main()


def main():
	n = int(input())
	a = list(enumerate(map(int, input().split())))
	a.sort(key=lambda item: (item[1], -item[0]))
	m = int(input())
	for i in range(m):
		(k, pos) = map(int, input().split())
		s = a[-k:]
		s = sorted(s)
		print(s[pos - 1][1])
main()


n = int(input())
b = list(map(int, input().split()))
a = []
for i in range(n):
	a.append([b[i], n - i])
a.sort()
a.reverse()
p = []
t = []
for i in range(n):
	t.append(a[i])
	d = t.copy()
	d.sort(key=lambda x: -x[1])
	p.append(d)
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	d = p[k - 1].copy()
	print(d[pos - 1][0])


n = int(input())
import copy
mins = [0] * n
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
	f = a.copy()
	(k, pos) = map(int, input().split())
	for l in range(n - k):
		if mins[l] == 0:
			mins[l] = min(f)
		for j in range(len(f) - 1, -1, -1):
			if f[j] == mins[l]:
				f.pop(j)
				break
	print(f[pos - 1])


from bisect import bisect_left, bisect_right, insort_right

class SquareSkipList:

	def __init__(self, values=None, sorted_=False, square=1000, seed=42):
		inf = float('inf')
		self.square = square
		if values is None:
			self.rand_y = seed
			self.layer1 = [inf]
			self.layer0 = [[]]
		else:
			self.layer1 = layer1 = []
			self.layer0 = layer0 = []
			if not sorted_:
				values.sort()
			y = seed
			l0 = []
			for v in values:
				y ^= (y & 524287) << 13
				y ^= y >> 17
				y ^= (y & 134217727) << 5
				if y % square == 0:
					layer0.append(l0)
					l0 = []
					layer1.append(v)
				else:
					l0.append(v)
			layer1.append(inf)
			layer0.append(l0)
			self.rand_y = y

	def add(self, x):
		y = self.rand_y
		y ^= (y & 524287) << 13
		y ^= y >> 17
		y ^= (y & 134217727) << 5
		self.rand_y = y
		if y % self.square == 0:
			(layer1, layer0) = (self.layer1, self.layer0)
			idx1 = bisect_right(layer1, x)
			layer1.insert(idx1, x)
			layer0_idx1 = layer0[idx1]
			idx0 = bisect_right(layer0_idx1, x)
			layer0.insert(idx1 + 1, layer0_idx1[idx0:])
			del layer0_idx1[idx0:]
		else:
			idx1 = bisect_right(self.layer1, x)
			insort_right(self.layer0[idx1], x)

	def remove(self, x):
		idx1 = bisect_left(self.layer1, x)
		layer0_idx1 = self.layer0[idx1]
		idx0 = bisect_left(layer0_idx1, x)
		if idx0 == len(layer0_idx1):
			del self.layer1[idx1]
			self.layer0[idx1] += self.layer0.pop(idx1 + 1)
		else:
			del layer0_idx1[idx0]

	def search_higher_equal(self, x):
		idx1 = bisect_left(self.layer1, x)
		layer0_idx1 = self.layer0[idx1]
		idx0 = bisect_left(layer0_idx1, x)
		if idx0 == len(layer0_idx1):
			return self.layer1[idx1]
		return layer0_idx1[idx0]

	def search_higher(self, x):
		idx1 = bisect_right(self.layer1, x)
		layer0_idx1 = self.layer0[idx1]
		idx0 = bisect_right(layer0_idx1, x)
		if idx0 == len(layer0_idx1):
			return self.layer1[idx1]
		return layer0_idx1[idx0]

	def search_lower(self, x):
		idx1 = bisect_left(self.layer1, x)
		layer0_idx1 = self.layer0[idx1]
		idx0 = bisect_left(layer0_idx1, x)
		if idx0 == 0:
			return self.layer1[idx1 - 1]
		return layer0_idx1[idx0 - 1]

	def pop(self, idx):
		layer0 = self.layer0
		s = -1
		for (i, l0) in enumerate(layer0):
			s += len(l0) + 1
			if s >= idx:
				break
		if s == idx:
			layer0[i] += layer0.pop(i + 1)
			return self.layer1.pop(i)
		else:
			return layer0[i].pop(idx - s)

	def __getitem__(self, item):
		layer0 = self.layer0
		s = -1
		for (i, l0) in enumerate(layer0):
			s += len(l0) + 1
			if s >= item:
				break
		if s == item:
			return self.layer1[i]
		else:
			return layer0[i][item - s]

	def print(self):
		print(self.layer1)
		print(self.layer0)
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
M = int(input())
Ans = [-1] * M
KP = [list(map(int, input().split())) for _ in range(M)]
Idx_query = list(range(M))
Idx_query.sort(key=lambda x: KP[x][0])
Idx = list(range(N))
Idx.sort(key=lambda x: A[x], reverse=True)
n = 0
st = SquareSkipList()
for idx_query in Idx_query:
	(k, pos) = KP[idx_query]
	while n < k:
		idx = Idx[n]
		st.add(idx)
		n += 1
	Ans[idx_query] = A[st[pos - 1]]
print('\n'.join(map(str, Ans)))


import sys
INF = 10 ** 15

def solve():
	global dp
	dp = [(+INF, []) for _ in range(n + 1)]
	dp[0] = (0, [])
	for i in range(1, n + 1):
		last = a[i - 1]
		for k in range(n, 0, -1):
			if dp[k - 1][0] != +INF:
				new_sum = dp[k - 1][0] - last
				new_seq = dp[k - 1][1] + [last]
				if dp[k] > (new_sum, new_seq):
					dp[k] = (new_sum, new_seq)
(n,) = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
(t,) = map(int, sys.stdin.readline().split())
dp = []
solve()
for _ in range(t):
	(k, pos) = map(int, sys.stdin.readline().split())
	assert dp[k][0] != INF
	print(dp[k][1][pos - 1])


import os
import sys
from io import BytesIO, IOBase

class SortedList:

	def __init__(self, iterable=None, _load=200):
		if iterable is None:
			iterable = []
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def main():
	n = int(input())
	aa = list(map(int, input().split()))
	a = [(v, n - i - 1) for (i, v) in enumerate(aa)]
	a.sort()
	a.reverse()
	q = []
	for i in range(int(input())):
		(x, y) = map(int, input().split())
		q.append((x, y, i))
	q.sort(key=lambda x: x[0])
	b = SortedList()
	ans = [0] * len(q)
	j = 0
	for i in q:
		while j < i[0]:
			b.add(-(a[j][1] - n + 1))
			j += 1
		ans[i[2]] = aa[b[i[1] - 1]]
	for i in ans:
		print(i)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
arr = [int(i) for i in input().split()]
sor = [[arr[i], n - i] for i in range(n)]
sor.sort()
m = int(input())
for i in range(m):
	op = []
	[q, index] = [int(i) for i in input().split()]
	for j in range(q):
		op.append(n - sor[-1 - j][1])
	op.sort()
	print(arr[op[index - 1]])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
x = [[a[i], i] for i in range(n)]
x.sort(key=lambda x: x[0], reverse=True)
for _i in range(m):
	(k, p) = map(int, input().split())
	print(sorted(x[:k], key=lambda x: x[1])[p - 1][0])


n = int(input())
s = sorted([[v, -i] for (i, v) in enumerate(map(int, input().split()))])
for _ in range(int(input())):
	(k, i) = map(int, input().split())
	ans = sorted(s[-k:], key=lambda x: -x[1])
	print(ans[i - 1][0])


n = int(input())
aa = [int(i) for i in input().split()]
dic = {}
for i in range(0, n):
	if aa[i] not in dic:
		dic[aa[i]] = [i]
	else:
		dic[aa[i]].append(i)
ll = sorted(dic)[::-1]
m = int(input())
for _ in range(0, m):
	(k, pos) = map(int, input().split())
	ans = []
	for i in range(0, len(ll)):
		if len(dic[ll[i]]) < k:
			ans += dic[ll[i]]
			k -= len(dic[ll[i]])
		else:
			ans += dic[ll[i]][:k]
			break
	print(aa[sorted(ans)[pos - 1]])


n = int(input())
arr = list(map(int, input().split()))
m = int(input())
q = []
for i in range(m):
	(a, b) = map(int, input().split())
	q.append((a, b))

def f(arr, m):
	vis = [False] * len(arr)
	arr2 = []
	for i in range(len(arr)):
		arr2.append([arr[i], n - i])
	arr2.sort(reverse=True)
	arr2 = arr2[:m]
	for i in range(len(arr2)):
		arr2[i][1] = n - arr2[i][1]
	arr2.sort(key=lambda x: x[1])
	res = []
	for i in range(len(arr2)):
		res.append(arr2[i][0])
	return res
for i in q:
	x = f(arr, i[0])
	print(x[i[1] - 1])


from sys import stdin
n = int(stdin.readline())
alist = list(map(int, stdin.readline().split()))
aindlist = sorted([(elem, i) for (i, elem) in enumerate(alist)], key=lambda x: (-x[0], x[1]))
m = int(stdin.readline())
ans = []
for _ in range(m):
	(k, pos) = map(int, stdin.readline().split())
	temp = [x[1] for x in aindlist[:k]]
	temp.sort()
	ans.append(alist[temp[pos - 1]])
print(*ans, sep='\n')


import sys
input = sys.stdin.readline

def answer(a, k, pos):
	while len(a) > k:
		mn = min(a)
		for i in range(len(a) - 1, -1, -1):
			if a[i] == mn:
				del a[i]
				break
	return a[pos - 1]
n = int(input())
a = list(map(int, input().split()))
m = int(input())
for _ in range(m):
	(k, pos) = map(int, input().split())
	print(answer(list(a), k, pos))


def custom_key(pair):
	(value, index) = pair
	return (-value, index)
n = int(input())
List = [int(x) for x in input().split()]
Sorted = []
for i in range(1, n + 1):
	Sorted.append([List[i - 1], i])
Sorted.sort(key=custom_key)
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	X = []
	for i in range(k):
		X.append(Sorted[i])
	X.sort(key=lambda x: x[1])
	print(X[pos - 1][0])


n = int(input())
a = [int(x) for x in input().split()]
b = [[] for i in range(n + 3)]
for g in range(1, n + 1):
	k = g
	cnt = 0
	mm = 10 ** 10
	m = 0
	for i in range(n):
		if len(b[g]) != 0:
			mm = min(b[g])
		if cnt == k:
			if a[i] > mm:
				for j in range(k - 1, -1, -1):
					if b[g][j] == mm:
						b[g].pop(j)
						break
				b[g].append(a[i])
		else:
			b[g].append(a[i])
			cnt += 1
		m = max(b[g])
m = int(input())
for i in range(m):
	(k, pos) = [int(x) for x in input().split()]
	pos -= 1
	print(b[k][pos])


n = int(input())
line = list(map(int, input().split()))
m = int(input())
for _ in range(m):
	(k, pos) = map(int, input().split())
	lf = line[:]
	while len(lf) > k:
		j = -1
		x = min(lf)
		while j > -len(lf) - 1:
			if lf[j] == x:
				del lf[j]
				if len(lf) == k:
					break
			else:
				j -= 1
	print(lf[pos - 1])


n = int(input())
a = list(map(int, input().split()))
b = sorted(((a[i], -i) for i in range(n)), reverse=True)
m = int(input())
for _ in range(m):
	(k, p) = map(int, input().split())
	print(a[sorted((-i for (_, i) in b[:k]))[p - 1]])


a = int(input())
b = list(map(int, input().split()))
for i in range(a):
	b[i] = (b[i], -i)
b.sort()
k = int(input())
for q in range(k):
	a = list(map(int, input().split()))
	l = a[0]
	p = a[1]
	print(sorted(b[-l:], key=lambda x: -x[1])[p - 1][0])


n = int(input())
a = sorted(zip(list(map(int, input().split())), range(n)), key=lambda x: (x[0], -x[1]), reverse=True)
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	b = sorted(a[:k], key=lambda x: x[1])
	print(b[pos - 1][0])


n = int(input())
sequence = list(map(int, input().split()))
array = list(sequence)
array.sort(reverse=True)
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	D = dict()
	for elem in array[:k]:
		if elem in D:
			D[elem] += 1
		else:
			D[elem] = 1
	for elem in sequence:
		if elem in D and D[elem] != 0:
			D[elem] -= 1
			if pos == 1:
				print(elem)
				break
			else:
				pos -= 1


n = int(input())
a = [int(x) for x in input().split()]
z = a[:]
z.sort(reverse=True)
an = [[] for i in range(n + 1)]
an[0] = a[:]
for i in range(n):
	for j in range(len(a) - 1, -1, -1):
		if a[j] == z[-1]:
			del a[j]
			z.pop()
			break
	an[i + 1] = a[:]
an = an[::-1]
for j in range(int(input())):
	(x, y) = map(int, input().split())
	print(an[x][y - 1])


def mergesort(l, r, arr, pos):
	if r - l == 1:
		return (arr, pos)
	m = (l + r) // 2
	(arr, pos) = mergesort(l, m, arr, pos)
	(arr, pos) = mergesort(m, r, arr, pos)
	c = [0 for i in range(r)]
	d = [0 for i in range(r)]
	poi_a = l
	poi_b = m
	for i in range(l, r):
		if poi_a == m:
			c[i] = arr[poi_b]
			d[i] = pos[poi_b]
			poi_b += 1
		elif poi_b == r:
			c[i] = arr[poi_a]
			d[i] = pos[poi_a]
			poi_a += 1
		elif a[poi_a] > arr[poi_b]:
			c[i] = arr[poi_a]
			d[i] = pos[poi_a]
			poi_a += 1
		else:
			c[i] = arr[poi_b]
			d[i] = pos[poi_b]
			poi_b += 1
	for i in range(l, r):
		arr[i] = c[i]
		pos[i] = d[i]
	return (arr, pos)
n = int(input())
a = list(map(int, input().split()))
p = [i for i in range(n)]
temp = a[:]
(a, p) = mergesort(0, n, a, p)
for m in range(int(input())):
	(k, pos) = map(int, input().split())
	j = k
	while j < n and a[j - 1] == a[j]:
		j += 1
	i = k - 1
	l = 1
	while i > 0 and a[i - 1] == a[i]:
		i -= 1
		l += 1
	m = sorted(p[i:j])
	res = sorted(m[:l] + p[:i])
	print(temp[res[pos - 1]])


n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
b.reverse()
m = int(input())
for _ in range(m):
	(k, pos) = map(int, input().split())
	x = 0
	j = 0
	d = dict()
	ans = 0
	for i in b[:k]:
		d[i] = d.get(i, 0) + 1
	while x != pos:
		u = a[j]
		if u in d:
			d[u] -= 1
			x += 1
			if d[u] == 0:
				del d[u]
		j += 1
		ans = u
	print(ans)


n = int(input())
a = [int(i) for i in input().split()]
b = [(a[i], n - i) for i in range(n)]
b.sort(reverse=True)
b = [(b[i][0], n - b[i][1]) for i in range(n)]
m = int(input())
for qu in range(m):
	(k, p) = map(int, input().split())
	c = b[:k]
	c.sort(key=lambda x: x[1])
	print(c[p - 1][0])


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i])
a_sorted = sorted(a, reverse=True)
m = int(input())
r = []
for _ in range(m):
	b = []
	(k, pos) = input().split()
	k = int(k)
	pos = int(pos) - 1
	for i in range(n - 1, -1, -1):
		if a[i] > a_sorted[k - 1]:
			b.append([a[i], i])
	i = 0
	while len(b) < k:
		if a[i] == a_sorted[k - 1]:
			b.append([a[i], i])
		i += 1
	c = sorted(b, key=lambda x: x[1])
	r.append(c[pos][0])
for i in r:
	print(i)


from collections import defaultdict
n = int(input())
a = [int(x) for x in input().split()]
b = sorted(a, reverse=True)
for _ in range(int(input())):
	(k, pos) = map(int, input().split())
	d = defaultdict(int)
	for i in range(k):
		d[b[i]] += 1
	count = 0
	num = -1
	for x in a:
		if d[x]:
			d[x] -= 1
			count += 1
		if count == pos:
			num = x
			break
	print(num)


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] = (a[i], -i)
a.sort()
m = int(input())
for j in range(m):
	X = list(map(int, input().split()))
	k = X[0]
	pos = X[1]
	print(sorted(a[-k:], key=lambda x: -x[1])[pos - 1][0])


class BIT:

	def __init__(self, n):
		self.n = n
		self.bit = [0] * (n + 1)

	def _sum(self, i):
		s = 0
		while i > 0:
			s += self.bit[i]
			i -= i & -i
		return s

	def add(self, i, val):
		i = i + 1
		while i <= self.n:
			self.bit[i] += val
			i += i & -i

	def get_sum(self, l, r):
		return self._sum(r) - self._sum(l)

	def bisect_left(self, val):
		sum_ = 0
		pos = 0
		bit_length = (n + 1).bit_length()
		for i in range(bit_length, -1, -1):
			k = pos + (1 << i)
			if k < self.n + 1 and sum_ + self.bit[k] < val:
				sum_ += self.bit[k]
				pos += 1 << i
		return pos + 1
from operator import itemgetter
n = int(input())
a = list(map(int, input().split()))
m = int(input())
info = [list(map(int, input().split())) + [i] for i in range(m)]
b = list(zip(a, range(len(a))))
b = sorted(b, key=itemgetter(0), reverse=True)
info = sorted(info, key=itemgetter(0))
bit = BIT(n)
ans = [0] * m
cnt = 0
for i in range(m):
	(k, pos, ind) = info[i]
	while cnt < k:
		bit.add(b[cnt][1], 1)
		cnt += 1
	ans[ind] = a[bit.bisect_left(pos) - 1]
for i in ans:
	print(i)


n = int(input())
A = list(map(int, input().split()))
L = A.copy()
L2 = L.copy()
A.sort()
c = int(input())
r = []

def S(x, L):
	idx = L.index(x)
	L[idx] = -1
	return [idx, x]
for i in range(c):
	(k, pos) = map(int, input().split())
	B = A[:0 - k - 1:-1]
	H = list(map(lambda x: S(x, L), B))
	H.sort()
	N = list(map(lambda x: x[1], H))
	r += [N[pos - 1]]
	L = L2.copy()
print('\n'.join(map(str, r)))


n = int(input())
data = list(map(int, input().split()))
m = int(input())
for _ in range(m):
	(k, pos) = map(int, input().split())
	s = data[:]
	ans = []
	for i in range(k):
		x = s.index(max(s))
		ans.append(x)
		s[x] = -1
	ans.sort()
	print(data[ans[pos - 1]])


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a1 = list(map(int, input().split()))
a = list()
for i in range(n):
	a.append((a1[i], n - i))
a.sort(reverse=True)
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	c = list()
	for j in range(k):
		c.append(n - a[j][1])
	c.sort()
	res = a1[c[pos - 1]]
	print(res)


a = int(input())
Ans = []
A = list(map(int, input().split()))
for i in range(a):
	A[i] = [A[i], -i]
A.sort()
for i in range(a):
	A[i][1] = -A[i][1]
for i in range(int(input())):
	(n, m) = map(int, input().split())
	B = list(A[a - n:])
	B.sort(key=lambda n: n[1])
	Ans.append(B[m - 1][0])
for an in Ans:
	print(an)


test_case = '7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4'
length = int(input())
a = [int(x) for x in input().split()]
l = []
for x in range(length):
	l.append([a[x], x + 1])
ll = sorted(l, key=lambda y: (y[0], -y[1]))
t = int(input())
for _ in range(t):
	(k, p) = [int(x) for x in input().split()]
	ans = ll[length - k:]
	final = sorted(ans, key=lambda y: y[1])
	print(final[p - 1][0])


m = int(input())
line = [int(i) for i in input().split()]
for i in range(int(input())):
	false_line = list(line)
	(k, pos) = map(int, input().split())
	while len(false_line) > k:
		x = min(false_line)
		rang = iter(range(-1, -len(false_line) - 1, -1))
		j = next(rang)
		while j > -len(false_line) - 1:
			if false_line[j] == x:
				del false_line[j]
				if len(false_line) == k:
					break
			else:
				try:
					j = next(rang)
				except StopIteration:
					break
	print(false_line[pos - 1])


n = int(input())
p = list(map(int, input().split()))
p = sorted(list(enumerate(p)), key=lambda x: -x[1])
a = [[]]
for i in range(n):
	a.append(sorted(a[-1] + [p[i]], key=lambda x: x[0]))
m = int(input())
for _ in range(m):
	(x, y) = map(int, input().split())
	print(a[x][y - 1][1])


n = int(input())
arr = list(map(int, input().split()))
arr_sorted = [(arr[i], i) for i in range(n)]
arr_sorted.sort(key=lambda el: (el[0], -el[1]), reverse=True)
m = int(input())
for req_i in range(m):
	(k, pos) = map(int, input().split())
	pos -= 1
	cur_arr = arr_sorted[:k]
	cur_arr.sort(key=lambda el: el[1])
	print(cur_arr[pos][0])


n = int(input())
nar = list(map(int, input().split()))
m = int(input())
for q in range(m):
	(k, p) = list(map(int, input().split()))
	mar = []
	for i in range(k):
		mar.append(nar[i])
	for i in range(k, n):
		minItem = 0
		for j in range(1, k):
			if mar[j] <= mar[minItem]:
				minItem = j
		if nar[i] > mar[minItem]:
			mar.pop(minItem)
			mar.append(nar[i])
	print(mar[p - 1])


from math import *
from copy import copy

def maximum(list_ticket):
	for i in range(len(list_ticket)):
		if i == 0:
			ma = list_ticket[i]
		else:
			ma = max(ma, list_ticket[i])
	ma = list_ticket.index(ma)
	list_ticket.insert(ma, 0)
	list_ticket.pop(ma + 1)
	return ma
n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
	b = copy(a)
	q = []
	array = list(map(int, input().split()))
	for i in range(array[0]):
		q.append(maximum(b))
	q.sort()
	q1 = []
	for i in range(len(q)):
		q1.append(a[q[i]])
	q1 = list(map(str, q1))
	print(q1[array[1] - 1])


n = int(input())
arr = list(map(int, input().split()))
arr = [(i, num) for (num, i) in enumerate(arr)]
arr.sort(key=lambda x: (-x[0], x[1]))
m = int(input())
for q in range(m):
	(k, pos) = tuple(map(int, input().split()))
	now = []
	for i in arr:
		if len(now) == k:
			break
		now.append(i)
	now.sort(key=lambda x: x[1])
	print(now[pos - 1][0])


import os, sys, math
import collections

def solve(sorted_seq, seq, k, pos):
	lowest = sorted_seq[-k]
	count_selected = sorted_seq[-k:].count(lowest)
	used_lowest = 0
	used_digits = 0
	for x in seq:
		if x < lowest:
			continue
		if x == lowest:
			if used_lowest == count_selected:
				continue
			used_lowest += 1
		used_digits += 1
		if used_digits == pos:
			return x
	assert False
	return -1
if os.path.exists('testing'):
	name = os.path.basename(__file__)
	if name.endswith('.py'):
		name = name[:-3]
	src = open(name + '.in.txt', encoding='utf8')
	input = src.readline
num = int(input().strip())
seq = list(map(int, input().strip().split()))
sorted_seq = list(sorted(seq))
num = int(input().strip())
for x in range(num):
	(k, pos) = map(int, input().strip().split())
	res = solve(sorted_seq, seq, k, pos)
	print(res)


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] = (a[i], -i)
a.sort()
m = int(input())
for j in range(m):
	t = list(map(int, input().split()))
	k = t[0]
	p = t[1]
	print(sorted(a[-k:], key=lambda x: -x[1])[p - 1][0])


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt, log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

class FenwickTree:

	def __init__(self, x):
		self.bit = x
		for i in range(len(x)):
			j = i | i + 1
			if j < len(x):
				x[j] += x[i]

	def update(self, idx, x):
		while idx < len(self.bit):
			self.bit[idx] += x
			idx |= idx + 1

	def query(self, end):
		x = 0
		while end:
			x += self.bit[end - 1]
			end &= end - 1
		return x

	def findkth(self, k):
		idx = -1
		for d in reversed(range(len(self.bit).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(self.bit) and k >= self.bit[right_idx]:
				idx = right_idx
				k -= self.bit[idx]
		return idx + 1

	def printpref(self):
		out = []
		for i in range(1, len(self.bit) + 1):
			out.append(self.query(i))
		print(out)

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def solve(case):
	n = int(inp())
	a = lis()
	queries = []
	m = int(inp())
	for i in range(m):
		(k, pos) = sep()
		queries.append((k, pos, i))
	queries.sort()
	b = sorted(a)[::-1]
	ind = {}
	for i in range(n):
		if a[i] not in ind:
			ind[a[i]] = deque([i])
		else:
			ind[a[i]].append(i)
	sl = SortedList()
	currind = 0
	ans = [-1] * m
	for (k, pos, where) in queries:
		while currind < k:
			sl.add(ind[b[currind]].popleft())
			currind += 1
		ans[where] = a[sl[pos - 1]]
	for i in ans:
		print(i)
testcase(1)


def min_s(a, k):
	res = a.copy()
	for i in range(len(a) - k):
		m = min(res)
		res.reverse()
		res.remove(m)
		res.reverse()
	return res
n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
for _ in range(m):
	(k, pos) = [int(x) for x in input().split()]
	l = min_s(a, k)
	print(l[pos - 1])


n = int(input())
s = list(map(int, input().split()))
for i in range(n):
	s[i] = (s[i], i)
mx = sorted(s, key=lambda y: y[0], reverse=True)
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	pos -= 1
	a = 0
	last = mx[0][0]
	res = []
	d = dict()
	d2 = dict()
	for x in range(k):
		if mx[x][0] not in d:
			d[mx[x][0]] = 1
		else:
			d[mx[x][0]] += 1
	for x in range(n):
		if mx[x][0] != last:
			a += 1
			last = mx[x][0]
		if a == k:
			break
		if mx[x][0] not in d2:
			d2[mx[x][0]] = [mx[x][1]]
		else:
			d2[mx[x][0]].append(mx[x][1])
	for a in list(d.keys()):
		d2[a].sort()
		for j in range(d[a]):
			res.append((a, d2[a][j]))
	res = sorted(res, key=lambda y: y[1])
	print(res[pos][0])


n = int(input())
a = list(map(int, input().split()))
d = []
for i in range(n):
	d.append([a[i], -i])
d.sort(reverse=True)
e = []
for i in range(n):
	e.append([-d[i][1], d[i][0]])
m = int(input())
f = []
for i in range(m):
	(b, c) = map(int, input().split())
	f = e[:b]
	f.sort()
	print(f[c - 1][1])


n = int(input())
b = list(map(int, input().split()))
a = [[0] * 2 for i in range(n)]
for i in range(n):
	a[i][0] = b[i]
	a[i][1] = i
for i in range(n - 1):
	for j in range(n - i - 1):
		if a[j][0] > a[j + 1][0]:
			(a[j], a[j + 1]) = (a[j + 1], a[j])
		elif a[j][0] == a[j + 1][0] and a[j][1] < a[j + 1][1]:
			(a[j], a[j + 1]) = (a[j + 1], a[j])
m = int(input())
for k in range(m):
	(k, ind) = map(int, input().split())
	ans = [[0] * 2 for i in range(k)]
	for i in range(k):
		ans[i][0] = a[n - i - 1][1]
		ans[i][1] = a[n - i - 1][0]
	ans.sort()
	print(ans[ind - 1][1])


n = int(input())
a = list(map(int, input().split()))
a1 = sorted(a, key=lambda x: -x)
m = int(input())
for i in range(m):
	(k, p) = list(map(int, input().split()))
	c = {}
	c1 = {}
	s = []
	for j in range(n + 1):
		s.append({})
	pos = {}
	for j in range(n):
		c[a1[j]] = 0
		c1[a1[j]] = 0
		s[0][a[j]] = 0
		pos[a1[j]] = []
	for j in range(n):
		s[0][a[j]] += 1
	for j in range(n):
		for t in range(n):
			s[j + 1][a[t]] = s[j][a[t]]
		s[j + 1][a[j]] -= 1
	b = []
	for j in range(k):
		c[a1[j]] += 1
	for j in range(k, n):
		c1[a1[j]] += 1
	ns = []
	us = [0] * n
	ans = []
	for j in range(n - 1, -1, -1):
		if c1[a[j]] ^ 0:
			c1[a[j]] -= 1
		else:
			ans.append(a[j])
	for j in range(len(ans) // 2):
		(ans[j], ans[len(ans) - 1 - j]) = (ans[len(ans) - 1 - j], ans[j])
	print(ans[p - 1])


import math
import copy
t = int(input())

def remove_values_from_list(the_list, val):
	return [value for value in the_list if value != val]
list1 = [int(x) for x in input().split()]
indexDict = dict()
for i in range(0, len(list1)):
	a = list1[i]
	if a in indexDict.keys():
		indexDict[a].append(i)
	else:
		indexDict[a] = [i]
n = int(input())
for i in range(0, n):
	indexDict1 = copy.deepcopy(indexDict)
	inp = input().split()
	k = int(inp[0])
	pos = int(inp[1])
	list2 = sorted(list1)[-k:]
	listF = [-1] * t
	for j in list2:
		index = indexDict1[j].pop(0)
		listF[index] = j
	listF = remove_values_from_list(listF, -1)
	print(listF[pos - 1])


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] = [a[i], n - i]
a.sort()
c = list(range(n))
for i in range(n):
	c[n - i - 1] = [n - a[i][1] + 1, a[i][0]]
m = int(input())
for g in range(m):
	(k, pos) = map(int, input().split())
	b = c[:k]
	b.sort()
	print(b[pos - 1][1])


n = int(input())
a = [int(x) for x in input().split()]
a_sorted = sorted(a, reverse=True)
m = int(input())
for i in range(m):
	(k, pos) = [int(x) for x in input().split()]
	vals = a_sorted[0:k]
	result = []
	for val in a:
		if val in vals:
			result.append(val)
			vals.remove(val)
		if len(vals) == 0:
			break
	print(result[pos - 1])


n = int(input())
arr = list(map(int, input().split()))
q = int(input())
qs = []
for i in range(q):
	(a, b) = map(int, input().split())
	qs.append([a, b - 1, i])
qs.sort()
ret = []
used = [0 for i in range(n)]
ans = [-1 for i in range(q)]
for z in range(q):
	(l, pos, o) = qs[z]
	while len(ret) < l:
		mx = 0
		ind = -1
		for i in range(n):
			if not used[i]:
				if mx < arr[i]:
					mx = arr[i]
					ind = i
		used[ind] = 1
		ret.append(mx)
	c = []
	for i in range(n):
		if used[i]:
			c.append(arr[i])
	ans[qs[z][2]] = c[pos]
for i in range(q):
	print(ans[i])


n = int(input())
c = list(map(lambda x: (int(x[1]), -x[0]), enumerate(input().split())))
so = sorted(c)
for i in range(int(input())):
	(k, r) = map(int, input().split())
	now = so[-k:]
	now.sort(key=lambda x: -x[1])
	print(now[r - 1][0])


n = int(input())
a = list(map(int, input().split()))
b = [a]
for x in range(n - 1):
	b.append([])
	m = min(a)
	y = len(a) - 1
	while y >= 0 and a[y] != m:
		b[-1].append(a[y])
		y -= 1
	y -= 1
	while y >= 0:
		b[-1].append(a[y])
		y -= 1
	a = b[-1][::-1]
m = int(input())
for x in range(m):
	(k, l) = map(int, input().split())
	if k < n:
		k = n - (k - 1) - 1
		l = len(b[k]) - (l - 1) - 1
		print(b[k][l])
	else:
		print(b[0][l - 1])


import itertools
import functools
from operator import itemgetter

def cmp(a, b):
	if a[0] > b[0]:
		return -1
	elif a[0] < b[0]:
		return 1
	elif a[1] < b[1]:
		return -1
	elif a[1] > b[1]:
		return 1
	else:
		return 0
n = int(input())
a = list(map(int, input().split()))
c = a[:]
c = list(zip(a, itertools.count(0)))
c = sorted(c, key=functools.cmp_to_key(cmp))
h = c[:]
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	h = sorted(c[:k], key=itemgetter(1))
	print(h[pos - 1][0])


WIDTH = 10

def index_tree(n):
	levels = [[1] * n]
	size = WIDTH
	while size < n:
		(m, r) = (n // size, n % size)
		levels.append([size] * m + ([r] if r > 0 else []))
		size *= WIDTH
	return levels

def dec_index(levels, i):
	for level in levels:
		level[i] -= 1
		i //= WIDTH

def find_pos(levels, pos):
	(i, l) = (0, len(levels) - 1)
	total = 0
	while True:
		level = levels[l]
		while total + level[i] < pos:
			total += level[i]
			i += 1
		if l == 0:
			return i
		i *= WIDTH
		l -= 1
import sys

def main():
	numbers = [int(x) for x in sys.stdin.read().split()]
	n = numbers[0]
	sequence = numbers[1:n + 1]
	m = numbers[n + 1]
	queries = {}
	for i in range(n + 2, n + 2 + 2 * m, 2):
		(k, pos) = (numbers[i], numbers[i + 1])
		if k in queries:
			queries[k][pos] = None
		else:
			queries[k] = {pos: None}
	sequence1 = sorted([(s, -i) for (i, s) in enumerate(sequence)])
	tree = index_tree(n)
	size = n
	for (_, neg_i) in sequence1:
		if size in queries:
			for pos in queries[size]:
				queries[size][pos] = find_pos(tree, pos)
		dec_index(tree, -neg_i)
		size -= 1
	for i in range(n + 2, n + 2 + 2 * m, 2):
		(k, pos) = (numbers[i], numbers[i + 1])
		print(sequence[queries[k][pos]])
main()


n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
	d = dict()
	(k, pos) = map(int, input().split())
	b = a + []
	for i in range(k):
		d[b.index(max(b))] = max(b)
		b[b.index(max(b))] = 0
	print(d[sorted(d.keys())[pos - 1]])


n = int(input())
a = list(map(int, input().split()))
s_a = sorted(a)
m = int(input())
req = []
for _ in range(m):
	req.append(list(map(int, input().split())))
d = dict()
for r in req:
	if r[0] in d.keys():
		print(d[r[0]][r[1] - 1])
	else:
		del_indx = []
		for e in s_a[:n - r[0]]:
			for i in reversed(range(n)):
				if a[i] == e:
					if i not in del_indx:
						del_indx.append(i)
						break
		new_a = []
		for i in range(n):
			if i not in del_indx:
				new_a.append(a[i])
		d[r[0]] = new_a
		print(d[r[0]][r[1] - 1])


n = int(input())
l = [int(j) for j in input().split()]
m = int(input())
d = dict()
for i in range(n):
	if l[i] in d:
		d[l[i]].append(i)
	else:
		d[l[i]] = [i]
d = sorted(d.items(), reverse=True)
for que in range(m):
	(k, pos) = [int(j) for j in input().split()]
	min_ = []
	i = 0
	j = 0
	while k > 0:
		k -= 1
		min_.append(d[i][1][j])
		j += 1
		if j == len(d[i][1]):
			i += 1
			j = 0
	min_.sort()
	print(l[min_[pos - 1]])


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
M = int(input())
Ans = []
Idx = list(range(N))
Idx.sort(key=lambda x: A[x], reverse=True)
for _ in range(M):
	(k, pos) = map(int, input().split())
	print(A[sorted(Idx[:k])[pos - 1]])


from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
a_copy = a.copy()
a_copy.sort()
for _ in range(int(input())):
	(k, pos) = map(int, input().split())
	part_of_a_copy = Counter(a_copy[len(a_copy) - k:])
	accepted_elements = []
	for num in a:
		if num in part_of_a_copy and part_of_a_copy[num] != 0:
			part_of_a_copy[num] -= 1
			accepted_elements.append(num)
	print(accepted_elements[pos - 1])


n = int(input())
a = [int(i) for i in input().split()]
copy1 = a[:]
a.sort()
m = int(input())
for i in range(m):
	(k, pos) = map(int, input().split())
	ans = [-1]
	copy = a[-k:]
	for i in copy1:
		if i in copy:
			copy.pop(copy.index(i))
			ans.append(i)
	print(ans[pos])


n = int(input())
a = list(map(int, input().split()))
u = sorted(a)
m = int(input())
ans = []
for _ in range(m):
	(k, pos) = map(int, input().split())
	d = []
	p = []
	s = u[n - k:]
	for i in range(n):
		if len(d) == k:
			break
		for j in range(len(s)):
			if s[j] == a[i]:
				s[j] = -1
				d.append(i)
				break
	d.sort()
	ans.append(a[d[pos - 1]])
print('\n'.join(map(str, ans)))


n = int(input())
a = list(map(int, input().split()))
m = int(input())
info = [list(map(int, input().split())) for i in range(m)]
sorted_a = sorted(a, reverse=True)
ruiseki = [0] * (n + 1)
for i in range(n):
	ruiseki[i + 1] = ruiseki[i] + a[i]
for i in range(m):
	(k, pos) = info[i]
	max_num = ruiseki[k]
	use = [False] * n
	num = sorted_a[k - 1]
	cnt = 0
	for i in range(n):
		if a[i] > num:
			use[i] = True
			cnt += 1
	nokori = k - cnt
	for i in range(n):
		if nokori == 0:
			break
		if a[i] == num:
			use[i] = True
			nokori -= 1
	ans = []
	for i in range(n):
		if use[i]:
			ans.append(i)
	print(a[ans[pos - 1]])


import sys
from collections import defaultdict
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
l = [i for i in arr]
for i in range(n):
	arr[i] = [arr[i], -i]
arr.sort()
dic = defaultdict(list)
z = len(arr)
m = int(sys.stdin.readline())
for _ in range(m):
	(n, pos) = map(int, sys.stdin.readline().split())
	if dic[n] != []:
		print(l[dic[n][pos - 1]])
	else:
		dic[n] = [0 for _ in range(n)]
		ind = n - 1
		for i in range(z - 1, z - n - 1, -1):
			dic[n][ind] = -arr[i][1]
			ind -= 1
		dic[n].sort()
		print(l[dic[n][pos - 1]])


import sys as _sys

def main():
	(n,) = _read_ints()
	a = tuple(_read_ints())
	(m,) = _read_ints()
	queries = (tuple(_read_ints()) for i_query in range(m))
	result = process_queries(a, queries)
	print(*result, sep='\n')

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split())

def process_queries(sequence, queries):
	sequence = tuple(sequence)
	indices_to_select = sorted(range(len(sequence)), key=lambda index: (-sequence[index], index))
	enumerated_queries = sorted(enumerate(queries), key=lambda iv: iv[1][0])[::-1]
	queries_responses = [None] * len(enumerated_queries)
	selections_tree = [0] * (len(sequence) + 1)
	selected_n = 0
	for index_to_select in indices_to_select:
		_fenwick_tree_add(selections_tree, index_to_select, 1)
		selected_n += 1
		while enumerated_queries and enumerated_queries[-1][1][0] == selected_n:
			(query_index, (_k, subseq_index)) = enumerated_queries.pop()
			seq_index = _find_seq_index_by_subseq_index(selections_tree, subseq_index)
			queries_responses[query_index] = sequence[seq_index]
	return queries_responses

def _find_seq_index_by_subseq_index(tree, subseq_i):
	seq_length = len(tree) - 1
	min_i = 0
	max_i = seq_length - 1
	while min_i != max_i:
		mid_i = (min_i + max_i) // 2
		if _fenwick_tree_prefix_sum(tree, mid_i) < subseq_i:
			min_i = mid_i + 1
		else:
			max_i = mid_i
	return min_i

def _fenwick_tree_prefix_sum(tree, i):
	i += 1
	result = 0
	while i != 0:
		result += tree[i]
		i -= i & -i
	return result

def _fenwick_tree_add(tree, i, x):
	i += 1
	while i < len(tree):
		tree[i] += x
		i += i & -i
main()