n = int(input())
a = list(map(int,input().split()))
l = [0]*(10**5+1)
s = 0
for i in a:
    l[i]+=1
    s+=i

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