f=lambda:[*list(map(int,input().split()))]

f()

s,*l=[0]*100002

for i in f(): s+=i; l[i]+=1

q=f()[0]

for _ in range(q): b,c=f(); s+=(c-b)*l[b]; l[c]+=l[b]; l[b]=0; print(s)
f=lambda:list(map(int,input().split()))

f()

s,*l=[0]*100002

for i in f(): s+=i; l[i]+=1

q,=f()

for _ in range(q): b,c=f(); s+=(c-b)*l[b]; l[c]+=l[b]; l[b]=0; print(s)
f = lambda: [*list(map(int, input().split()))]
f()
s, *l = [0] * 100002
for i in f():
    s += i
    l[i] += 1
q = f()[0]
for _ in range(q):
    b, c = f()
    s += (c - b) * l[b]
    l[c] += l[b]
    l[b] = 0
    print(s)

f = lambda: list(map(int, input().split()))
f()
s, *l = [0] * 100002
for i in f():
    s += i
    l[i] += 1
(q,) = f()
for _ in range(q):
    b, c = f()
    s += (c - b) * l[b]
    l[c] += l[b]
    l[b] = 0
    print(s)

-f = lambda: [*list(map(int, input().split()))] +f = lambda: list(map(int, input().split())) -q = f()[0] +(q,) = f()