from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())

# bc = [list(map(int,input().split())) for _ in range(q)]

c = Counter(a)

ans = []

for i in range(q):
    bi,ci = list(map(int,input().split()))
    c[ci] = c[bi]+c[ci]
    c[bi] = 0
    csum = 0
    for k,v in c.items():
        csum += k*v
    ans.append(csum)
    
for i in ans:
    print(i)