from collections import Counter 
N = int(input())
A = Counter(list(map(int, input().split())))
Q = int(input())

for _ in range(Q):
    B, C = map(int, input().split())
    if B in A.keys():
        if C in A.keys():
            A[C] += A[B]
        else:
            A[C] = A[B]
        A[B] = 0
    ans = 0
    for k, v in A.items():
        ans += k*v
    print(ans)