n = int(input())
A = list(map(int,input().split()))
count = [0]*(10**5)
for a in A:
    count[a] += 1
    
ans = 0
for i in range(10**5):
    if count[i]==0:
        continue
    else:
        ans += i*count[i]

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