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

Difficulty: competition

Starter code:

Solutions:
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)


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

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()

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]))

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))

def __starting_point():
    main()
__starting_point()

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))))


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)

#!/usr/bin/env python3
from sys import stdin,stdout

def readint():
    return list(map(int, stdin.readline().split()))
#lines = stdin.readlines()

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()));
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)])


#print("tog arr us link")
##print( tog)
#print( arr)
#print( us)
#print(link)

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("out:", out[-1]);
    #print("ptr:", ptr)
    #print("us:", us)

print(" ".join(map(str, out)))
        
        






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

fut = list(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().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 = ' ')


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
	else:
		if 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
		else:
			if 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 = 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


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))  


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=' ')


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)

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 ((i, ll) for i, ll in enumerate(d) if i != ci - 1):
        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=' ')


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
    p[ci] = i
    if i < n:
        t[i][1], t[i][2] = 0, 0
    r.append(t[i][0] if i < n else - 1)
print(*r, sep=' ')


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=' ')


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=' ')


import operator

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=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=' ')


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 = list(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(str(t[i][0]))
        t[i][1], t[i][2] = 0, 0
        i += 1
    p[ci] = i
print(' '.join(r))


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 = list(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(' '.join(map(str, r)))


n = int(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()))

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(' '.join(r))


n = int(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()))

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)