A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers p_{i}, a_{i} and b_{i}, where p_{i} is the price of the i-th t-shirt, a_{i} is front color of the i-th t-shirt and b_{i} is back color of the i-th t-shirt. All values p_{i} are distinct, and values a_{i} and b_{i} are integers from 1 to 3.

m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color c_{j}.

A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.

You are to compute the prices each buyer will pay for t-shirts.


-----Input-----

The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.

The following line contains sequence of integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ 1 000 000 000), where p_{i} equals to the price of the i-th t-shirt.

The following line contains sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 3), where a_{i} equals to the front color of the i-th t-shirt.

The following line contains sequence of integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 3), where b_{i} equals to the back color of the i-th t-shirt.

The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers. 

The following line contains sequence c_1, c_2, ..., c_{m} (1 ≤ c_{j} ≤ 3), where c_{j} equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.

 


-----Output-----

Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.


-----Examples-----
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1

Output
200 400 300 500 911 -1 

Input
2
1000000000 1
1 1
1 2
2
2 1

Output
1 1000000000

Source: codeforces | Difficulty: MEDIUM
Tags: Data structures

Implementation


Solutions:
input()
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
input()
c = list(map(int, input().split()))
d = [[[] for _ in range(3)] for _ in range(3)]
for (pi, ai, bi) in zip(p, a, b):
	d[ai - 1][bi - 1].append(pi)
for row in d:
	for l in row:
		l.sort(reverse=True)
r = []
for ci in c:
	pm = 1000000001
	im = -1
	for (j, l) in enumerate(d[ci - 1]):
		if len(l) > 0 and l[-1] < pm:
			pm = l[-1]
			im = ci - 1
			jm = j
	for (i, ll) in enumerate(d):
		l = ll[ci - 1]
		if len(l) > 0 and l[-1] < pm:
			pm = l[-1]
			im = i
			jm = ci - 1
	r.append(d[im][jm].pop() if im >= 0 else -1)
print(*r, sep=' ')


n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
m = int(input())
pos = map(int, input().split())
fut = zip(p, a, b)
fut = list(fut)

def sravni(elem):
	return elem[0]
fut.sort(key=sravni)
vz = []
for i in range(n):
	vz.append(False)
lastc = [0, 0, 0]
result = ''
for poset in pos:
	ctoim = -1
	for i in range(lastc[poset - 1], n):
		if vz[i] == False:
			if fut[i][1] == poset or fut[i][2] == poset:
				vz[i] = True
				ctoim = fut[i][0]
				lastc[poset - 1] = i + 1
				break
	if ctoim == -1:
		lastc[poset - 1] = n + 1
	result += str(ctoim) + ' '
print(result)


N = int(input())
p = list(map(int, input().strip().split(' ')))
a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
m = int(input())
CM = list(map(int, input().strip().split(' ')))
checkout = [0 for i in range(N)]
shirt = [[p[i], i] for i in range(N)]
shirt = sorted(shirt)
NUM = [[] for i in range(3)]
for i in range(N):
	temp = shirt[i][1]
	num1 = a[temp] - 1
	num2 = b[temp] - 1
	NUM[num1] += [temp]
	if num1 != num2:
		NUM[num2] += [temp]
for i in range(3):
	NUM[i] = NUM[i][::-1]
ANS = []
for i in range(len(CM)):
	color = CM[i] - 1
	NO = 0
	if NUM[color] != []:
		ans = NUM[color].pop()
	else:
		NO = 1
	if NO == 0:
		while checkout[ans] == 1:
			if NUM[color] != []:
				ans = NUM[color].pop()
			else:
				NO = 1
				break
	if NO == 1:
		ANS += [-1]
	else:
		ANS += [p[ans]]
		checkout[ans] = 1
print(*ANS)


def main():
	n = int(input())
	p = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	m = int(input())
	(r, g, bl) = ([], [], [])
	used = set()
	for i in range(n):
		(c1, c2) = (a[i], b[i])
		if c1 == 1 or c2 == 1:
			r.append([p[i], i])
		if c1 == 2 or c2 == 2:
			g.append([p[i], i])
		if c1 == 3 or c2 == 3:
			bl.append([p[i], i])
	r.sort(reverse=True)
	g.sort(reverse=True)
	bl.sort(reverse=True)
	answer = []
	for i in list(map(int, input().split())):
		if i == 1:
			try:
				while True:
					pop = r.pop()[0]
					if pop not in used:
						answer.append(str(pop))
						used.add(pop)
						break
			except:
				answer.append('-1')
		elif i == 2:
			try:
				while True:
					pop = g.pop()[0]
					if pop not in used:
						answer.append(str(pop))
						used.add(pop)
						break
			except:
				answer.append('-1')
		elif i == 3:
			try:
				while True:
					pop = bl.pop()[0]
					if pop not in used:
						answer.append(str(pop))
						used.add(pop)
						break
			except:
				answer.append('-1')
	print(' '.join(answer))
main()


import operator
n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
input()
c = map(int, input().split())
t = sorted(zip(p, a, b), key=operator.itemgetter(0))
t = [list(x) for x in t]
p = [None, 0, 0, 0]
r = []
for ci in c:
	i = p[ci]
	while i < n and ci not in (t[i][1], t[i][2]):
		i += 1
	if i == n:
		r.append(-1)
	else:
		r.append(t[i][0])
		(t[i][1], t[i][2]) = (0, 0)
		i += 1
	p[ci] = i
print(*r, sep=' ')


n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
m = input()
c = map(int, input().split())
t = sorted(zip(p, a, b), key=lambda x: x[0])
t = [list(x) for x in t]
p = [None, 0, 0, 0]
r = []
for ci in c:
	i = p[ci]
	while i < n and ci not in (t[i][1], t[i][2]):
		i += 1
	if i == n:
		r.append('-1')
	else:
		r.append(str(t[i][0]))
		(t[i][1], t[i][2]) = (0, 0)
		i += 1
	p[ci] = i
print(*r)


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tog = [(p[i], a[i], b[i]) for i in range(0, n)]
tog.sort()
arr = [None, [], [], []]
us = [None, [], [], []]
link = []
for i in range(0, n):
	if tog[i][1] == tog[i][2]:
		arr[tog[i][1]].append(i)
		us[tog[i][1]].append(True)
		link.append([(tog[i][1], len(arr[tog[i][1]]) - 1)])
	else:
		arr[tog[i][1]].append(i)
		us[tog[i][1]].append(True)
		arr[tog[i][2]].append(i)
		us[tog[i][2]].append(True)
		link.append([(tog[i][1], len(arr[tog[i][1]]) - 1), (tog[i][2], len(arr[tog[i][2]]) - 1)])
ptr = [None, 0, 0, 0]
m = int(input())
buy = list(map(int, input().split()))
out = []
for c in buy:
	while ptr[c] < len(us[c]) and (not us[c][ptr[c]]):
		ptr[c] += 1
	if ptr[c] >= len(arr[c]):
		out.append(-1)
	else:
		v = arr[c][ptr[c]]
		us[c][ptr[c]] = False
		out.append(tog[v][0])
		for (cc, pp) in link[v]:
			us[cc][pp] = False
print(' '.join(map(str, out)))


n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
m = input()
c = map(int, input().split())
tmp = sorted(zip(p, a, b), key=lambda l: l[0])
tmp = [list(i) for i in tmp]
indxs = [None, 0, 0, 0]
res = []
for c_i in c:
	indx = indxs[c_i]
	while indx < n:
		if c_i in (tmp[indx][1], tmp[indx][2]):
			break
		indx += 1
	if indx == n:
		res.append('-1')
	else:
		res.append(str(tmp[indx][0]))
		tmp[indx][1] = 0
		tmp[indx][2] = 0
		indx += 1
	indxs[c_i] = indx
print(*res)


import sys
input = sys.stdin.buffer.readline

def solution():
	n = int(input())
	p = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	f = [0] * n
	m = int(input())
	c = list(map(int, input().split()))
	l = [[], [], [], []]
	for i in range(n):
		l[a[i]] += [[p[i], i]]
		if b[i] != a[i]:
			l[b[i]] += [[p[i], i]]
	for i in range(1, 4):
		l[i] = sorted(l[i])
	i = [0, 0, 0, 0]
	ans = []
	for x in c:
		while i[x] < len(l[x]) and f[l[x][i[x]][1]]:
			i[x] += 1
		if i[x] >= len(l[x]):
			ans += [-1]
		else:
			ans += [l[x][i[x]][0]]
			f[l[x][i[x]][1]] = 1
	print(*ans)
solution()


from collections import deque, defaultdict
n = int(input())
p = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
m = int(input())
c = list(map(int, input().split(' ')))
result = list()
colors_1 = defaultdict(list)
for i in range(n):
	colors_1[a[i]].append(p[i])
	colors_1[b[i]].append(p[i])
for i in range(1, 4):
	colors_1[i].sort()
	colors_1[i] = deque(colors_1[i])
buyed = defaultdict(lambda : 0)
for q in c:
	while True:
		try:
			pr = colors_1[q].popleft()
		except IndexError:
			pr = -1
			break
		else:
			if buyed[pr] == 0:
				break
	result.append(pr)
	buyed[pr] = 1
result = list(map(str, result))
print(' '.join(result))


n = int(input())
ps = [int(a) for a in input().split()]
fronts = [int(a) for a in input().split()]
backs = [int(a) for a in input().split()]
shirts = list(zip(ps, fronts, backs))
shirts.sort()
bought = [False] * n
ptrs = [0] * 4

def mvptrs():
	for i in range(1, len(ptrs)):
		while ptrs[i] < n and (shirts[ptrs[i]][1] != i and shirts[ptrs[i]][2] != i or bought[ptrs[i]]):
			ptrs[i] += 1
mvptrs()
m = int(input())
colors = [int(a) for a in input().split()]

def serve(i):
	if ptrs[colors[i]] >= n:
		print(-1, end='')
	else:
		print(shirts[ptrs[colors[i]]][0], end='')
		bought[ptrs[colors[i]]] = True
		mvptrs()
serve(0)
for i in range(1, m):
	print(' ', end='')
	serve(i)
print()


n = int(input())
price = list(map(int, input().split()))
fron = list(map(int, input().split()))
back = list(map(int, input().split()))
e = [0, 0, 0]
group = []
for i in range(n):
	group.append([price[i], fron[i], back[i]])
group.sort(key=lambda x: x[0])
m = int(input())
l = list(map(int, input().split()))
for i in range(m):
	a = l[i]
	j = e[a - 1]
	t = True
	while j < n:
		if group[j][1] == a or group[j][2] == a:
			print(group[j][0], end=' ')
			group[j][1] = 0
			group[j][2] = 0
			t = False
			break
		j += 1
	if t:
		print(-1, end=' ')
	e[a - 1] = j + 1


n = int(input())
p = list(zip(map(int, input().split()), map(int, input().split()), map(int, input().split())))
p.sort()
pointer = [-1, 0, 0, 0]
input()
for cj in map(int, input().split()):
	while pointer[cj] < n and (p[pointer[cj]] == None or cj not in p[pointer[cj]][1:]):
		pointer[cj] += 1
	if pointer[cj] == n:
		print(-1, end=' ')
	else:
		print(p[pointer[cj]][0], end=' ')
		p[pointer[cj]] = None


import sys
from collections import deque
read = lambda : sys.stdin.readline().rstrip()
readi = lambda : int(sys.stdin.readline())
writeln = lambda x: sys.stdout.write(str(x) + '\n')
write = lambda x: sys.stdout.write(x)
N = readi()
ps = list(map(int, read().split()))
cas = list(map(int, read().split()))
cbs = list(map(int, read().split()))
M = readi()
bs = list(map(int, read().split()))
results = []
ts = [[] for _ in range(7)]
tcnt = [0 for _ in range(7)]
for i in range(N):
	(price, a, b) = (ps[i], cas[i], cbs[i])
	color = 2 ** (a - 1) | 2 ** (b - 1)
	ts[color].append(price)
	tcnt[color] += 1
tss = []
for i in range(7):
	if tcnt[i]:
		tss.append(deque(list(sorted(ts[i]))))
	else:
		tss.append([])
spots = [[1, 3, 5], [3, 2, 6], [5, 6, 4]]
for i in range(M):
	(s1, s2, s3) = spots[bs[i] - 1]
	(c1, c2, c3) = (tcnt[s1], tcnt[s2], tcnt[s3])
	alts = []
	if c1:
		alts.append(s1)
	if c2:
		alts.append(s2)
	if c3:
		alts.append(s3)
	if not alts:
		results.append('-1')
		continue
	vals = []
	for alt in alts:
		vals.append((tss[alt][0], alt))
	vals.sort()
	(price, cidx) = vals[0]
	results.append(str(price))
	tcnt[cidx] -= 1
	tss[cidx].popleft()
writeln(' '.join(results))


from sys import stdin, stdout

def readint():
	return map(int, stdin.readline().split())
n = int(input())
p = list(readint())
a = list(readint())
b = list(readint())
m = int(input())
c = list(readint())
cc = [[] for i in range(3)]
for i in range(n):
	cc[a[i] - 1].append(i)
	cc[b[i] - 1].append(i)
for i in range(3):
	cc[i].sort(key=lambda e: p[e])
ans = []
ii = [0, 0, 0]
for i in range(m):
	ci = c[i] - 1
	while True:
		if ii[ci] >= len(cc[ci]):
			break
		if p[cc[ci][ii[ci]]] == -1:
			ii[ci] += 1
		else:
			break
	if ii[ci] >= len(cc[ci]):
		ans.append(-1)
	else:
		ans.append(p[cc[ci][ii[ci]]])
		p[cc[ci][ii[ci]]] = -1
print(*ans)


n = int(input())
p = list(map(int, input().split()))
b = list(map(int, input().split()))
a = list(map(int, input().split()))
m = int(input())
c = list(map(int, input().split()))
tp = {(i, j): [] for i in range(1, 4) for j in range(i, 4)}
for i in range(n):
	k = [a[i], b[i]]
	k.sort()
	tp[tuple(k)].append(p[i])
for (k, v) in tp.items():
	v.sort(reverse=True)
ans = []
for i in range(m):
	mm = -1
	for (k, v) in tp.items():
		if c[i] in k:
			if len(v) != 0:
				if mm == -1 or tp[mm][-1] > v[-1]:
					mm = k
	if mm == -1:
		ans.append('-1')
	else:
		z = tp[mm].pop()
		ans.append(str(z))
print(' '.join(ans))


n = int(input())
ps = [int(i) for i in input().split()]
front = [int(i) for i in input().split()]
back = [int(i) for i in input().split()]
colors = [[], [], []]
lens = [0] * 3
inds = [0] * 3
for (p, f, b, i) in zip(ps, front, back, [i for i in range(n)]):
	colors[f - 1].append((p, i))
	lens[f - 1] += 1
	if b != f:
		colors[b - 1].append((p, i))
		lens[b - 1] += 1
for i in range(3):
	colors[i].sort()
m = int(input())
favs = [int(i) for i in input().split()]
bought = set()
res = ''
for fav in favs:
	while True:
		if inds[fav - 1] == lens[fav - 1]:
			res += '-1 '
			break
		if colors[fav - 1][inds[fav - 1]][1] in bought:
			inds[fav - 1] += 1
		else:
			res += str(colors[fav - 1][inds[fav - 1]][0]) + ' '
			bought.add(colors[fav - 1][inds[fav - 1]][1])
			break
print(res[:-1])


import sys

def solve():
	n = int(sys.stdin.readline())
	p = [int(pi) for pi in sys.stdin.readline().split()]
	a = [int(ai) - 1 for ai in sys.stdin.readline().split()]
	b = [int(bi) - 1 for bi in sys.stdin.readline().split()]
	m = int(sys.stdin.readline())
	c = [int(ci) - 1 for ci in sys.stdin.readline().split()]
	p_col = [[] for i in range(3)]
	for i in range(n):
		p_col[a[i]].append(p[i])
		if a[i] != b[i]:
			p_col[b[i]].append(p[i])
	for i in range(3):
		p_col[i].sort()
	used = set()
	l = [0] * 3
	ans = [-1] * m
	for (k, cj) in enumerate(c):
		for i in range(l[cj], len(p_col[cj])):
			if p_col[cj][i] not in used:
				ans[k] = p_col[cj][i]
				used.add(p_col[cj][i])
				l[cj] = i + 1
				break
	print(*ans)

def __starting_point():
	solve()
__starting_point()


import collections

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

def lt():
	return list(map(int, input().split()))

def pt(x):
	print(x)

def ip():
	return input()

def it():
	return int(input())

def sl(x):
	return [t for t in x]

def spl(x):
	return x.split()

def aj(liste, item):
	liste.append(item)

def bin(x):
	return '{0:b}'.format(x)

def listring(l):
	return ' '.join([str(x) for x in l])

def ptlist(l):
	print(' '.join([str(x) for x in l]))
n = it()
p = lt()
a = lt()
b = lt()
m = it()
c = lt()
shirt = list(zip(p, a, b))
shirt.sort()
pointer = [-1, 0, 0, 0]
l = []
for i in range(m):
	cl = c[i]
	while pointer[cl] < n and (shirt[pointer[cl]] == None or cl not in shirt[pointer[cl]][1:]):
		pointer[cl] += 1
	if pointer[cl] == n:
		l.append(-1)
	else:
		l.append(shirt[pointer[cl]][0])
		shirt[pointer[cl]] = None
ptlist(l)


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = int(input())
queue = list(map(int, input().split()))
pointers = [[0 for i in range(3)] for j in range(3)]
t = [[list() for i in range(3)] for j in range(3)]
for i in range(n):
	t[a[i] - 1][b[i] - 1].append(p[i])
for i in range(3):
	for j in range(3):
		t[i][j].sort()
for i in range(m):
	m1 = 10 ** 10
	m2 = 10 ** 10
	for j in range(3):
		if len(t[queue[i] - 1][j]) and pointers[queue[i] - 1][j] < len(t[queue[i] - 1][j]) and (t[queue[i] - 1][j][pointers[queue[i] - 1][j]] < m1):
			m1 = t[queue[i] - 1][j][pointers[queue[i] - 1][j]]
			pos_j = j
	for j in range(3):
		if len(t[j][queue[i] - 1]) and pointers[j][queue[i] - 1] < len(t[j][queue[i] - 1]) and (t[j][queue[i] - 1][pointers[j][queue[i] - 1]] < m2):
			m2 = t[j][queue[i] - 1][pointers[j][queue[i] - 1]]
			pos_i = j
	if m1 == 10 ** 10 and m2 == 10 ** 10:
		print(-1, end=' ')
	elif m1 < m2:
		pointers[queue[i] - 1][pos_j] += 1
		print(m1, end=' ')
	else:
		pointers[pos_i][queue[i] - 1] += 1
		print(m2, end=' ')


from collections import deque

class CodeforcesTask799BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.prices = []
		self.front_colors = []
		self.back_colors = []
		self.m = 0
		self.customers = []

	def read_input(self):
		self.n = int(input())
		self.prices = [int(x) for x in input().split(' ')]
		self.front_colors = [int(x) for x in input().split(' ')]
		self.back_colors = [int(x) for x in input().split(' ')]
		self.m = int(input())
		self.customers = [int(x) for x in input().split(' ')]

	def process_task(self):
		shop = [[[] for _ in range(3)] for __ in range(3)]
		for t in range(self.n):
			shop[self.front_colors[t] - 1][self.back_colors[t] - 1].append(self.prices[t])
		for x in range(3):
			for y in range(3):
				shop[x][y].sort()
				shop[x][y] = deque(shop[x][y])
		res = []
		for customer in self.customers:
			mn_p = 10 ** 10
			selected = None
			for back in range(3):
				if shop[customer - 1][back]:
					if shop[customer - 1][back][0] < mn_p:
						selected = (customer - 1, back)
						mn_p = shop[customer - 1][back][0]
			for front in range(3):
				if shop[front][customer - 1]:
					if shop[front][customer - 1][0] < mn_p:
						selected = (front, customer - 1)
						mn_p = shop[front][customer - 1][0]
			if selected:
				res.append(shop[selected[0]][selected[1]].popleft())
			else:
				res.append(-1)
		self.result = ' '.join([str(x) for x in res])

	def get_result(self):
		return self.result
Solution = CodeforcesTask799BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, c, v) = (int(input()), [0] * 4, [])

def adv():
	for i in range(1, 4):
		while c[i] < n and (a[c[i]][0] == -1 or i not in a[c[i]][1:]):
			c[i] += 1
a = sorted(([x, y, z] for (x, y, z) in zip(map(int, input().split()), map(int, input().split()), map(int, input().split()))))
adv()
m = int(input())
for x in map(int, input().split()):
	if c[x] == n:
		v.append(-1)
	else:
		v.append(a[c[x]][0])
		a[c[x]][0] = -1
		c[x] += 1
		adv()
print(' '.join(map(str, v)))


n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
input()
c = map(int, input().split())
t = sorted(zip(p, a, b), key=lambda x: x[0])
p = [None, 0, 0, 0]
r = []
for ci in c:
	i = p[ci]
	while i < n and (t[i] is None or ci not in t[i][1:]):
		i += 1
	if i == n:
		r.append('-1')
	else:
		r.append(str(t[i][0]))
		t[i] = None
		i += 1
	p[ci] = i
print(' '.join(r))


import os
import sys, math
from io import BytesIO, IOBase
from os import path

def yes():
	print('YES')

def no():
	print('NO')

def I():
	return int(input())

def In():
	return map(int, input().split())
from heapq import heappush, heappop

def main():
	try:
		n = I()
		p = list(In())
		a = list(In())
		b = list(In())
		(p12, p11, p13, p22, p23, p33) = ([], [], [], [], [], [])
		for i in range(n):
			(a[i], b[i]) = (min(a[i], b[i]), max(a[i], b[i]))
			if a[i] == 1 and b[i] == 1:
				heappush(p11, p[i])
			elif a[i] == 1 and b[i] == 2:
				heappush(p12, p[i])
			elif a[i] == 1 and b[i] == 3:
				heappush(p13, p[i])
			elif a[i] == 2 and b[i] == 2:
				heappush(p22, p[i])
			elif a[i] == 2 and b[i] == 3:
				heappush(p23, p[i])
			else:
				heappush(p33, p[i])
		nq = I()
		c = list(In())
		ans = []
		for i in range(nq):
			q = c[i]
			cost = float('inf')
			co = -1
			if q == 1:
				if len(p11) and cost > p11[0]:
					co = 'p11'
					cost = p11[0]
				if len(p12) and cost > p12[0]:
					co = 'p12'
					cost = p12[0]
				if len(p13) and cost > p13[0]:
					co = 'p13'
					cost = p13[0]
				if co == 'p11':
					ans.append(heappop(p11))
				elif co == 'p12':
					ans.append(heappop(p12))
				elif co == 'p13':
					ans.append(heappop(p13))
				else:
					ans.append(-1)
			elif q == 2:
				if len(p22) and cost > p22[0]:
					cost = p22[0]
					co = 'p22'
				if len(p12) and cost > p12[0]:
					co = 'p12'
					cost = p12[0]
				if len(p23) and cost > p23[0]:
					co = 'p23'
					cost = p23[0]
				if co == 'p22':
					ans.append(heappop(p22))
				elif co == 'p12':
					ans.append(heappop(p12))
				elif co == 'p23':
					ans.append(heappop(p23))
				else:
					ans.append(-1)
			elif q == 3:
				if len(p33) and cost > p33[0]:
					cost = p33[0]
					co = 'p33'
				if len(p13) and cost > p13[0]:
					co = 'p13'
					cost = p13[0]
				if len(p23) and cost > p23[0]:
					co = 'p23'
					cost = p23[0]
				if co == 'p33':
					ans.append(heappop(p33))
				elif co == 'p13':
					ans.append(heappop(p13))
				elif co == 'p23':
					ans.append(heappop(p23))
				else:
					ans.append(-1)
		print(*ans)
	except:
		pass
M = 998244353
P = 1000000007
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(1):
	main()


n = int(input())
prices = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
m = int(input())
prefers = [int(x) for x in input().split(' ')]
color2shirt = {}
for i in range(n):
	if a[i] not in color2shirt:
		color2shirt[a[i]] = []
	color2shirt[a[i]].append(i)
	if b[i] not in color2shirt:
		color2shirt[b[i]] = []
	color2shirt[b[i]].append(i)
for c in color2shirt:
	color2shirt[c].sort(key=lambda x: prices[x], reverse=True)
out = []
sold = set([])
for i in range(m):
	while prefers[i] in color2shirt and len(color2shirt[prefers[i]]) > 0:
		c = color2shirt[prefers[i]].pop()
		if c in sold:
			continue
		sold.add(c)
		out.append(prices[c])
		break
	else:
		out.append(-1)
print(*out)


import itertools

def minl(l):
	return l[-1] if len(l) > 0 else 10 ** 9 + 1
input()
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
input()
c = list(map(int, input().split()))
d = [[[] for _ in range(3)] for _ in range(3)]
for (pi, ai, bi) in zip(p, a, b):
	d[ai - 1][bi - 1].append(pi)
for row in d:
	for l in row:
		l.sort(reverse=True)
r = []
for ci in c:
	ci -= 1
	row = ((ci, j) for j in range(3))
	col = ((i, ci) for i in range(3))
	(i, j) = min(itertools.chain(row, col), key=lambda p: minl(d[p[0]][p[1]]))
	l = d[i][j]
	r.append(l.pop() if len(l) > 0 else -1)
print(*r, sep=' ')


n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = int(input())
c = [int(i) for i in input().split()]
c1 = {}
c2 = {}
c3 = {}
for i in range(n):
	if a[i] == 1 or b[i] == 1:
		c1[p[i]] = 1
	if a[i] == 2 or b[i] == 2:
		c2[p[i]] = 1
	if a[i] == 3 or b[i] == 3:
		c3[p[i]] = 1
k1 = sorted(c1)
k2 = sorted(c2)
k3 = sorted(c3)
k1s = 0
k2s = 0
k3s = 0
for i in range(m):
	ch = 0
	if c[i] == 1:
		for j in range(k1s, len(k1)):
			if c1[k1[j]] != 0:
				c1[k1[j]] = 0
				c2[k1[j]] = 0
				c3[k1[j]] = 0
				ch = 1
				k1s = j + 1
				print(k1[j], end=' ')
				break
	elif c[i] == 2:
		for j in range(k2s, len(k2)):
			if c2[k2[j]] != 0:
				c1[k2[j]] = 0
				c2[k2[j]] = 0
				c3[k2[j]] = 0
				ch = 1
				k2s = j + 1
				print(k2[j], end=' ')
				break
	elif c[i] == 3:
		for j in range(k3s, len(k3)):
			if c3[k3[j]] != 0:
				c1[k3[j]] = 0
				c2[k3[j]] = 0
				c3[k3[j]] = 0
				ch = 1
				k3s = j + 1
				print(k3[j], end=' ')
				break
	if ch == 0:
		print(-1, end=' ')


n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
s = []
for i in range(n):
	s.append([p[i], a[i], b[i]])
s = sorted(s)
m = int(input())
c = [int(i) for i in input().split()]
idx = [0] * 4
ans = []
for i in range(m):
	ci = c[i]
	while idx[ci] < n:
		if s[idx[ci]][1] == ci or s[idx[ci]][2] == ci:
			s[idx[ci]][1] = 0
			s[idx[ci]][2] = 0
			ans.append(s[idx[ci]][0])
			break
		idx[ci] += 1
	if idx[ci] == n:
		ans.append(-1)
print(*ans)


import heapq
n = int(input())
color = [[] for x in range(4)]
p = list(map(int, input().split()))
f = list(map(int, input().split()))
b = list(map(int, input().split()))
dic = {}
cost = [0 for _ in range(10)]
buy = int(input())
ans = []
buyColor = list(map(int, input().split()))
for i in range(n):
	front = f[i]
	back = b[i]
	if front != back:
		heapq.heappush(color[front], p[i])
		heapq.heappush(color[back], p[i])
	else:
		heapq.heappush(color[front], p[i])
	dic[str(p[i])] = 1
for need in buyColor:
	while 1:
		if len(color[need]) > 0:
			pay = heapq.heappop(color[need])
			text = str(pay)
			if dic.get(text) == 1:
				ans.append(pay)
				dic[text] = 0
				break
		else:
			ans.append(-1)
			break
print(*ans)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
col = {(i, j): [] for i in range(1, 4) for j in range(1, 4)}
for i in range(n):
	(x, y) = (a[i], b[i])
	if x > y:
		(y, x) = (x, y)
	col[x, y].append(p[i])
for i in col.values():
	i.sort(reverse=True)
m = int(input())
c = list(map(int, input().split()))
ans = []
for i in c:
	cost = -1
	for (j, k) in col.items():
		if i in j and k and (cost == -1 or col[cost][-1] > k[-1]):
			cost = j
	if cost == -1:
		ans.append(-1)
	else:
		ans.append(col[cost].pop())
print(*ans)


n = int(input())
p = list(map(int, input().split()))
arr = [[], [], []]
used = set()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	arr[a[i] - 1].append(p[i])
	arr[b[i] - 1].append(p[i])
arr[0].sort(reverse=True)
arr[1].sort(reverse=True)
arr[2].sort(reverse=True)
q = int(input())
c = list(map(int, input().split()))
for i in c:
	while arr[i - 1] and arr[i - 1][-1] in used:
		arr[i - 1].pop()
	if arr[i - 1]:
		t = arr[i - 1].pop()
		used.add(t)
	else:
		t = -1
	print(t, end=' ')


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = [0] * n
m = int(input())
c = list(map(int, input().split()))
l = [[], [], [], []]
for i in range(n):
	l[a[i]] += [[p[i], i]]
	if b[i] != a[i]:
		l[b[i]] += [[p[i], i]]
for i in range(1, 4):
	l[i] = sorted(l[i])
i = [0, 0, 0, 0]
ans = []
for x in c:
	while i[x] < len(l[x]) and f[l[x][i[x]][1]]:
		i[x] += 1
	if i[x] >= len(l[x]):
		ans += [-1]
	else:
		ans += [l[x][i[x]][0]]
		f[l[x][i[x]][1]] = 1
print(*ans)


import heapq
n = int(input())
p = list(map(int, input().split()))
f = list(map(int, input().split()))
b = list(map(int, input().split()))
m = int(input())
choice = list(map(int, input().split()))
dic = {}
dic[1] = []
dic[2] = []
dic[3] = []
pdic = {}
for i in range(n):
	pdic[p[i]] = 0
	dic[f[i]].append(p[i])
	dic[b[i]].append(p[i])
heapq.heapify(dic[1])
heapq.heapify(dic[2])
heapq.heapify(dic[3])
for i in range(m):
	try:
		z = heapq.heappop(dic[choice[i]])
		while pdic[z] != 0:
			z = heapq.heappop(dic[choice[i]])
		pdic[z] = 1
		print(z, end=' ')
	except:
		print(-1, end=' ')


n = int(input())
p = list(map(int, input().strip().split(' ')))
a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
m = int(input())
lst = list(map(int, input().strip().split(' ')))
l11 = []
l12 = []
l13 = []
l22 = []
l23 = []
l33 = []
for j in range(n):
	if a[j] == b[j]:
		if a[j] == 1:
			l11.append(p[j])
		elif a[j] == 2:
			l22.append(p[j])
		else:
			l33.append(p[j])
	elif a[j] == 1 and b[j] == 2 or (a[j] == 2 and b[j] == 1):
		l12.append(p[j])
	elif a[j] == 1 and b[j] == 3 or (a[j] == 3 and b[j] == 1):
		l13.append(p[j])
	else:
		l23.append(p[j])
l11.sort()
l12.sort()
l13.sort()
l22.sort()
l23.sort()
l33.sort()
for j in range(m):
	if lst[j] == 1:
		if len(l11) == 0 and len(l12) == 0 and (len(l13) == 0):
			print(-1, end=' ')
		elif len(l11) == 0 and len(l12) == 0:
			print(l13[0], end=' ')
			del l13[0]
		elif len(l12) == 0 and len(l13) == 0:
			print(l11[0], end=' ')
			del l11[0]
		elif len(l11) == 0 and len(l13) == 0:
			print(l12[0], end=' ')
			del l12[0]
		elif len(l11) == 0:
			if l12[0] <= l13[0]:
				print(l12[0], end=' ')
				del l12[0]
			else:
				print(l13[0], end=' ')
				del l13[0]
		elif len(l12) == 0:
			if l11[0] <= l13[0]:
				print(l11[0], end=' ')
				del l11[0]
			else:
				print(l13[0], end=' ')
				del l13[0]
		elif len(l13) == 0:
			if l11[0] <= l12[0]:
				print(l11[0], end=' ')
				del l11[0]
			else:
				print(l12[0], end=' ')
				del l12[0]
		elif l11[0] <= l12[0] and l11[0] <= l13[0]:
			print(l11[0], end=' ')
			del l11[0]
		elif l12[0] <= l11[0] and l12[0] <= l13[0]:
			print(l12[0], end=' ')
			del l12[0]
		else:
			print(l13[0], end=' ')
			del l13[0]
	elif lst[j] == 2:
		if len(l12) == 0 and len(l22) == 0 and (len(l23) == 0):
			print(-1, end=' ')
		elif len(l12) == 0 and len(l22) == 0:
			print(l23[0], end=' ')
			del l23[0]
		elif len(l12) == 0 and len(l23) == 0:
			print(l22[0], end=' ')
			del l22[0]
		elif len(l22) == 0 and len(l23) == 0:
			print(l12[0], end=' ')
			del l12[0]
		elif len(l12) == 0:
			if l22[0] <= l23[0]:
				print(l22[0], end=' ')
				del l22[0]
			else:
				print(l23[0], end=' ')
				del l23[0]
		elif len(l22) == 0:
			if l12[0] <= l23[0]:
				print(l12[0], end=' ')
				del l12[0]
			else:
				print(l23[0], end=' ')
				del l23[0]
		elif len(l23) == 0:
			if l12[0] <= l22[0]:
				print(l12[0], end=' ')
				del l12[0]
			else:
				print(l22[0], end=' ')
				del l22[0]
		elif l12[0] <= l22[0] and l12[0] <= l23[0]:
			print(l12[0], end=' ')
			del l12[0]
		elif l22[0] <= l12[0] and l22[0] <= l23[0]:
			print(l22[0], end=' ')
			del l22[0]
		else:
			print(l23[0], end=' ')
			del l23[0]
	elif lst[j] == 3:
		if len(l13) == 0 and len(l23) == 0 and (len(l33) == 0):
			print(-1, end=' ')
		elif len(l13) == 0 and len(l23) == 0:
			print(l33[0], end=' ')
			del l33[0]
		elif len(l13) == 0 and len(l33) == 0:
			print(l23[0], end=' ')
			del l23[0]
		elif len(l23) == 0 and len(l33) == 0:
			print(l13[0], end=' ')
			del l13[0]
		elif len(l13) == 0:
			if l23[0] <= l33[0]:
				print(l23[0], end=' ')
				del l23[0]
			else:
				print(l33[0], end=' ')
				del l33[0]
		elif len(l23) == 0:
			if l13[0] <= l33[0]:
				print(l13[0], end=' ')
				del l13[0]
			else:
				print(l33[0], end=' ')
				del l33[0]
		elif len(l33) == 0:
			if l13[0] <= l23[0]:
				print(l13[0], end=' ')
				del l13[0]
			else:
				print(l23[0], end=' ')
				del l23[0]
		elif l13[0] <= l23[0] and l13[0] <= l33[0]:
			print(l13[0], end=' ')
			del l13[0]
		elif l23[0] <= l33[0] and l23[0] <= l13[0]:
			print(l23[0], end=' ')
			del l23[0]
		else:
			print(l33[0], end=' ')
			del l33[0]


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = int(input())
c = list(map(int, input().split()))
res_cost = ''
first_color_arr = []
second_color_arr = []
third_color_arr = []
buy_flag = []
for i in range(n):
	if a[i] == 1 or b[i] == 1:
		first_color_arr.append([p[i], i])
	if a[i] == 2 or b[i] == 2:
		second_color_arr.append([p[i], i])
	if a[i] == 3 or b[i] == 3:
		third_color_arr.append([p[i], i])
	buy_flag.append(False)
first_color_index = 0
second_color_index = 0
third_color_index = 0
first_color_arr = sorted(first_color_arr, key=lambda first_color_arr: first_color_arr[0])
second_color_arr = sorted(second_color_arr, key=lambda second_color_arr: second_color_arr[0])
third_color_arr = sorted(third_color_arr, key=lambda third_color_arr: third_color_arr[0])
first_color_len = len(first_color_arr)
second_color_len = len(second_color_arr)
third_color_len = len(third_color_arr)
for i in range(m):
	if c[i] == 1:
		while first_color_index < first_color_len and buy_flag[first_color_arr[first_color_index][1]]:
			first_color_index += 1
		try:
			res_cost += str(first_color_arr[first_color_index][0]) + ' '
			buy_flag[first_color_arr[first_color_index][1]] = True
		except:
			res_cost += '-1' + ' '
	if c[i] == 2:
		while second_color_index < second_color_len and buy_flag[second_color_arr[second_color_index][1]]:
			second_color_index += 1
		try:
			res_cost += str(second_color_arr[second_color_index][0]) + ' '
			buy_flag[second_color_arr[second_color_index][1]] = True
		except:
			res_cost += '-1' + ' '
	if c[i] == 3:
		while third_color_index < third_color_len and buy_flag[third_color_arr[third_color_index][1]]:
			third_color_index += 1
		try:
			res_cost += str(third_color_arr[third_color_index][0]) + ' '
			buy_flag[third_color_arr[third_color_index][1]] = True
		except:
			res_cost += '-1' + ' '
print(res_cost)


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
P = []
for i in range(n):
	P.append([p[i], a[i], b[i], 0])
P = sorted(P)
last = [0, 0, 0, 0]
m = int(input())
c = list(map(int, input().split()))
R = []
for i in c:
	if last[i] == -1:
		R.append(-1)
	else:
		for j in range(last[i], n):
			if P[j][3] == 0:
				if P[j][1] == i or P[j][2] == i:
					R.append(P[j][0])
					P[j][3] = 1
					if j + 1 < n:
						last[i] = j + 1
					else:
						last[i] = -1
					break
		else:
			R.append(-1)
			last[i] = -1
print(' '.join(map(str, R)))


def preferences(p, a, b, c):
	s = []
	for i in range(len(p)):
		s.append([p[i], a[i], b[i]])
	s.sort()
	idx = [0] * 4
	ans = []
	for i in range(len(c)):
		ci = c[i]
		while idx[ci] < len(p):
			if s[idx[ci]][1] == ci or s[idx[ci]][2] == ci:
				s[idx[ci]][1] = 0
				s[idx[ci]][2] = 0
				ans.append(s[idx[ci]][0])
				break
			idx[ci] += 1
		if idx[ci] == len(p):
			ans.append(-1)
	return ans
n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = int(input())
c = [int(i) for i in input().split()]
out = preferences(p, a, b, c)
print(' '.join((str(x) for x in out)))


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	p = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [[] for i in range(4)]
	for i in range(n):
		c[a[i]].append((p[i], i))
		c[b[i]].append((p[i], i))
	for i in range(4):
		c[i].sort()
	w = [False] * n
	m = int(input())
	i = [0] * 4
	for v in map(int, input().split()):
		while i[v] < len(c[v]) and w[c[v][i[v]][1]]:
			i[v] += 1
		if i[v] < len(c[v]) and (not w[c[v][i[v]][1]]):
			print(c[v][i[v]][0], end=' ')
			w[c[v][i[v]][1]] = True
			i[v] += 1
		else:
			print(-1, end=' ')
solve()


import sys
input = sys.stdin.readline
from heapq import *
n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pqs = [[] for _ in range(3)]
for i in range(n):
	for j in range(3):
		if a[i] == j + 1 or b[i] == j + 1:
			heappush(pqs[j], p[i])
served = set()
m = int(input())
c = list(map(int, input().split()))
ans = [-1] * m
for i in range(m):
	while pqs[c[i] - 1]:
		pr = heappop(pqs[c[i] - 1])
		if pr not in served:
			ans[i] = pr
			served.add(pr)
			break
print(*ans)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
t = I()
p = L()
a = L()
b = L()
m = I()
c = L()
one = []
two = []
three = []
for i in range(t):
	if a[i] == 1 or b[i] == 1:
		one.append((p[i], i))
	if a[i] == 2 or b[i] == 2:
		two.append((p[i], i))
	if a[i] == 3 or b[i] == 3:
		three.append((p[i], i))
if len(one) > 0:
	one.sort(key=lambda x: x[0])
if len(two) > 0:
	two.sort(key=lambda x: x[0])
if len(three) > 0:
	three.sort(key=lambda x: x[0])
i = 0
j = 0
k = 0
v = [0] * t
for _ in c:
	if _ == 1:
		while i < len(one) and v[one[i][1]]:
			i += 1
		if i < len(one):
			v[one[i][1]] = 1
			print(one[i][0], end=' ')
			i += 1
		else:
			print(-1, end=' ')
	elif _ == 2:
		while j < len(two) and v[two[j][1]]:
			j += 1
		if j < len(two):
			v[two[j][1]] = 1
			print(two[j][0], end=' ')
			j += 1
		else:
			print(-1, end=' ')
	else:
		while k < len(three) and v[three[k][1]]:
			k += 1
		if k < len(three):
			v[three[k][1]] = 1
			print(three[k][0], end=' ')
			k += 1
		else:
			print(-1, end=' ')


import math, sys
from collections import defaultdict, deque
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')

def I():
	return int(sys.stdin.readline())

def In():
	return map(int, sys.stdin.readline().split())

def Sn():
	return sys.stdin.readline().strip()
from heapq import heappush, heappop

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		p = list(In())
		a = list(In())
		b = list(In())
		(p12, p11, p13, p22, p23, p33) = ([], [], [], [], [], [])
		for i in range(n):
			(a[i], b[i]) = (min(a[i], b[i]), max(a[i], b[i]))
			if a[i] == 1 and b[i] == 1:
				heappush(p11, p[i])
			elif a[i] == 1 and b[i] == 2:
				heappush(p12, p[i])
			elif a[i] == 1 and b[i] == 3:
				heappush(p13, p[i])
			elif a[i] == 2 and b[i] == 2:
				heappush(p22, p[i])
			elif a[i] == 2 and b[i] == 3:
				heappush(p23, p[i])
			else:
				heappush(p33, p[i])
		nq = I()
		c = list(In())
		ans = []
		for i in range(nq):
			q = c[i]
			cost = float('inf')
			co = -1
			if q == 1:
				if len(p11) and cost > p11[0]:
					co = 'p11'
					cost = p11[0]
				if len(p12) and cost > p12[0]:
					co = 'p12'
					cost = p12[0]
				if len(p13) and cost > p13[0]:
					co = 'p13'
					cost = p13[0]
				if co == 'p11':
					ans.append(heappop(p11))
				elif co == 'p12':
					ans.append(heappop(p12))
				elif co == 'p13':
					ans.append(heappop(p13))
				else:
					ans.append(-1)
			elif q == 2:
				if len(p22) and cost > p22[0]:
					cost = p22[0]
					co = 'p22'
				if len(p12) and cost > p12[0]:
					co = 'p12'
					cost = p12[0]
				if len(p23) and cost > p23[0]:
					co = 'p23'
					cost = p23[0]
				if co == 'p22':
					ans.append(heappop(p22))
				elif co == 'p12':
					ans.append(heappop(p12))
				elif co == 'p23':
					ans.append(heappop(p23))
				else:
					ans.append(-1)
			elif q == 3:
				if len(p33) and cost > p33[0]:
					cost = p33[0]
					co = 'p33'
				if len(p13) and cost > p13[0]:
					co = 'p13'
					cost = p13[0]
				if len(p23) and cost > p23[0]:
					co = 'p23'
					cost = p23[0]
				if co == 'p33':
					ans.append(heappop(p33))
				elif co == 'p13':
					ans.append(heappop(p13))
				elif co == 'p23':
					ans.append(heappop(p23))
				else:
					ans.append(-1)
		print(*ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = int(input())
c = list(map(int, input().split()))
l = [list() for i in range(1, 4)]
for i in range(n):
	l[a[i] - 1].append((p[i], i))
	if a[i] != b[i]:
		l[b[i] - 1].append((p[i], i))
import operator
l = list(map(lambda l_: sorted(l_, key=operator.itemgetter(0)), l))
invalid = set()
ys = [0] * 4
for i in range(m):
	y = ys[c[i] - 1]
	while y < len(l[c[i] - 1]) and l[c[i] - 1][y][1] in invalid:
		y += 1
	if y == len(l[c[i] - 1]):
		print(-1, end=' ')
	else:
		print(l[c[i] - 1][y][0], end=' ')
		invalid.add(l[c[i] - 1][y][1])
	ys[c[i] - 1] = y


n = int(input())
p = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
used = [False] * n
ma = [[], [], []]
mb = [[], [], []]
for i in range(n):
	ma[a[i] - 1].append(i)
	mb[b[i] - 1].append(i)
for i in range(3):
	ma[i].sort(key=lambda x: -p[x])
	mb[i].sort(key=lambda x: -p[x])
m = int(input())
c = list(map(int, input().split(' ')))
ans = [0] * m
for i in range(m):
	x = c[i] - 1

	def tryone(a):
		while len(a[x]) > 0 and used[a[x][len(a[x]) - 1]]:
			a[x].pop()
		if len(a[x]) == 0:
			return -1
		else:
			return a[x][len(a[x]) - 1]
	c1 = tryone(ma)
	c2 = tryone(mb)
	if c1 == -1 and c2 == -1:
		ans[i] = -1
	elif c1 == -1 or (c2 != -1 and p[c2] < p[c1]):
		used[c2] = True
		ans[i] = p[c2]
	else:
		used[c1] = True
		ans[i] = p[c1]
print(' '.join(list(map(str, ans))))


from sys import stdin
inp = stdin.readline
n = int(inp())
p = list(map(int, inp().split()))
a = list(map(int, inp().split()))
b = list(map(int, inp().split()))
ans = []
ones = []
twos = []
threes = []
for i in range(n):
	if a[i] == 1 or b[i] == 1:
		ones.append((p[i], i))
	if a[i] == 2 or b[i] == 2:
		twos.append((p[i], i))
	if a[i] == 3 or b[i] == 3:
		threes.append((p[i], i))
ones.sort()
twos.sort()
threes.sort()
index_ones = 0
index_tows = 0
index_threes = 0
bought = set()
m = int(inp())
c = list(map(int, inp().split()))
for i in range(m):
	if c[i] == 1:
		while index_ones < len(ones) and ones[index_ones][1] in bought:
			index_ones += 1
		if index_ones == len(ones):
			ans.append(-1)
		else:
			ans.append(ones[index_ones][0])
			bought.add(ones[index_ones][1])
	elif c[i] == 2:
		while index_tows < len(twos) and twos[index_tows][1] in bought:
			index_tows += 1
		if index_tows == len(twos):
			ans.append(-1)
		else:
			ans.append(twos[index_tows][0])
			bought.add(twos[index_tows][1])
	else:
		while index_threes < len(threes) and threes[index_threes][1] in bought:
			index_threes += 1
		if index_threes == len(threes):
			ans.append(-1)
		else:
			ans.append(threes[index_threes][0])
			bought.add(threes[index_threes][1])
print(*ans)


n = int(input())
p = list(zip(map(int, input().split()), map(int, input().split()), map(int, input().split())))
m = int(input())
p.sort()
(t, ans) = ([0, 0, 0, 0], [])
for c in map(int, input().split()):
	while t[c] < n and (p[t[c]] == None or c not in p[t[c]][1:]):
		t[c] += 1
	if t[c] == n:
		ans.append(-1)
	else:
		ans.append(p[t[c]][0])
		p[t[c]] = None
print(*ans)


import sys

def main(in_stream=sys.stdin):
	max_col = 3
	in_stream.readline()
	plist = [int(tok) for tok in in_stream.readline().split()]
	alist = [int(tok) for tok in in_stream.readline().split()]
	blist = [int(tok) for tok in in_stream.readline().split()]
	in_stream.readline()
	clist = [int(tok) for tok in in_stream.readline().split()]
	color_to_tshirts = {i: [] for i in range(1, max_col + 1)}
	color_to_avail_index = {}
	for triple in zip(plist, alist, blist):
		tshirt = list(triple + (False,))
		color_to_tshirts[tshirt[1]].append(tshirt)
		color_to_tshirts[tshirt[2]].append(tshirt)
	for (c, tlist) in color_to_tshirts.items():
		tlist.sort(key=lambda ts: ts[0])
		color_to_avail_index[c] = 0
	result = []
	for ci in clist:
		the_list = color_to_tshirts[ci]
		avail_index = color_to_avail_index[ci]
		while avail_index < len(the_list) and the_list[avail_index][-1]:
			avail_index += 1
		color_to_avail_index[ci] = avail_index
		if len(the_list) == avail_index:
			result.append(-1)
			continue
		tsel = the_list[avail_index]
		tsel[-1] = True
		result.append(tsel[0])
	return result
print(*main())


n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = int(input())
c = [int(i) for i in input().split()]
s = []
for i in range(len(p)):
	s.append([p[i], a[i], b[i]])
s = sorted(s, key=lambda x: x[0])
idx = [0] * 4
l = []
for i in range(len(c)):
	x = c[i]
	while idx[x] < len(p):
		if s[idx[x]][1] == x or s[idx[x]][2] == x:
			s[idx[x]][1] = 0
			s[idx[x]][2] = 0
			l.append(s[idx[x]][0])
			break
		idx[x] += 1
	if idx[x] == len(p):
		l.append(-1)
for i in l:
	print(i, end=' ')


n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
m = input()
c = map(int, input().split())
tmp = sorted(zip(p, a, b), key=lambda x: x[0])
tmp = [list(x) for x in tmp]
indxs = [None, 0, 0, 0]
res = []
for c_i in c:
	indx = indxs[c_i]
	while indx < n and c_i not in (tmp[indx][1], tmp[indx][2]):
		indx += 1
	if indx == n:
		res.append('-1')
	else:
		res.append(str(tmp[indx][0]))
		(tmp[indx][1], tmp[indx][2]) = (0, 0)
		indx += 1
	indxs[c_i] = indx
print(*res)


from bisect import insort

class t_shirt:

	def __init__(self, p, a, b, id):
		self.p = p
		self.a = a
		self.b = b
		self.id = id

def helper(n1, n2, n3):
	try:
		if arr[n1][index[n1]].p < arr[n2][index[n2]].p and arr[n1][index[n1]].p < arr[n3][index[n3]].p:
			print(arr[n1][index[n1]].p)
			index[n1] += 1
			return
	except:
		pass
	try:
		if arr[n2][index[n2]].p < arr[n1][index[n1]].p and arr[n2][index[n2]].p < arr[n3][index[n3]].p:
			print(arr[n2][index[n2]].p)
			index[n2] += 1
			return
	except:
		pass
	try:
		if arr[n3][index[n3]].p < arr[n1][index[n1]].p and arr[n3][index[n3]].p < arr[n2][index[n2]].p:
			print(arr[n3][index[n3]].p)
			index[n3] += 1
			return
	except:
		pass
	print(-1)
n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = int(input())
c = list(map(int, input().split()))
arr = []
index = [0 for i in range(0, 7)]
for i in range(0, 7, 1):
	arr.append([])
for i in range(0, n, 1):
	t = t_shirt(p[i], a[i], b[i], i)
	if a[i] == 1 and b[i] == 1:
		arr[1].append(t)
	elif a[i] == 2 and b[i] == 2:
		arr[2].append(t)
	elif a[i] == 3 and b[i] == 3:
		arr[3].append(t)
	elif a[i] == 1 and b[i] == 2 or (a[i] == 2 and b[i] == 1):
		arr[4].append(t)
	elif a[i] == 1 and b[i] == 3 or (a[i] == 3 and b[i] == 1):
		arr[5].append(t)
	elif a[i] == 2 and b[i] == 3 or (a[i] == 3 and b[i] == 2):
		arr[6].append(t)
for i in range(1, 7, 1):
	arr[i] = sorted(arr[i], key=lambda t_shirt: t_shirt.p)
	arr[i].append(t_shirt(1000000001, 0, 0, 200001))
for i in c:
	if i == 1:
		helper(1, 4, 5)
	elif i == 2:
		helper(2, 4, 6)
	else:
		helper(3, 5, 6)


n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = int(input())
c = list(map(int, input().split()))
t = [[p[i], a[i], b[i], 1] for i in range(n)]
t = sorted(t, key=lambda x: x[0])
p = [0, 0, 0, 0]
ans = []
for i in range(m):
	clr = c[i]
	while p[clr] < n and (t[p[clr]][3] == 0 or (t[p[clr]][1] != clr and t[p[clr]][2] != clr)):
		p[clr] += 1
	if p[clr] == n:
		ans.append(-1)
	else:
		t[p[clr]][3] = 0
		ans.append(t[p[clr]][0])
print(' '.join([str(a) for a in ans]))