from collections import Counter
n = int(input())
a = list(map(int,input().split()))
x = 0
count = Counter(a)
q = int(input())
b,c = [],[]
for i in range(q):
    bi,ci = map(int,input().split())
    b.append(bi),c.append(ci)
for i in range(q):
    bi,ci = b[i],c[i]
    count[ci]+=count[bi]
    count[bi] = 0
    x = 0
    for k,v in count.items():
        x+=k*v
    print(x)
