from collections import defaultdict

n = int(input())
arr = list(map(int,input().split()))

dd = defaultdict(int)

for a in arr:
    dd[a]+=1

q = int(input())
for _ in range(q):
    b,c = map(int,input().split())
    if dd[b]>0:
        dd[c]+=dd[b]
        dd[b]=0
    sum = 0
    for key in dd.keys():
        sum+=dd[key]*key
    print(sum)