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

f(); l,q=f(),f()[0]

from collections import *

C,s=Counter(l),sum(l)

for _ in range(q): b,c=f(); s+=(c-b)*C[b]; C[c]+=C[b]; C[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()
l, q = f(), f()[0]
from collections import *

C, s = Counter(l), sum(l)
for _ in range(q):
    b, c = f()
    s += (c - b) * C[b]
    C[c] += C[b]
    C[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)

-l, q = f(), f()[0] -from collections import * - -C, s = Counter(l), sum(l) +s, *l = [0] * 100002 +for i in f(): +    s += i +    l[i] += 1 +q = f()[0] -    s += (c - b) * C[b] -    C[c] += C[b] -    C[b] = 0 +    s += (c - b) * l[b] +    l[c] += l[b] +    l[b] = 0