N = int(input())
A = list(map(int, input().split()))
Q = int(input())
Query = [tuple(map(int, input().split())) for _ in range(Q)]
from collections import Counter
cntr = Counter(A)
keys = list(cntr.keys())
for b, c in Query:
    cntr[c] += cntr[b]
    cntr[b] = 0
    ans = 0
    for k, v in cntr.items():
        ans += k * v
    print(ans)