from collections import Counter

n = int(input())
A = list(map(int, input().split()))
q = int(input())
cnt_A = dict(Counter(A))
for i in range(q):
    ans = 0
    b, c = map(int, input().split())
    if b in cnt_A and cnt_A[b] > 0:
        m = cnt_A[b]
        cnt_A[b] = 0
        if c not in cnt_A:
            cnt_A[c] = m
        else:
            cnt_A[c] += m
    ans = sum([k * v for k, v in cnt_A.items()])
    print(ans)