n = int(input())
a = list(map(int,input().split()))
q = int(input())
l = [0]*10**6
ans = sum(a)
for i in a:
    l[i]+=1
for i in range(q):
    b,c = map(int,input().split())
    l[c] += l[b]
    ans -= b*l[b]
    ans += c*l[b]
    l[b] = 0
    print(ans)