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