def resolve():
    import sys
    input = sys.stdin.readline
    from collections import Counter
    N = int(input())
    A = [int(i) for i in input().split()]
    Q = int(input())
    BC = []
    for i in range(Q):
        BC.append(list(map(int, input().split())))
    dic = Counter(A)
    for i in BC:
        B, C = i[0], i[1]
        dic[C] += dic[B]
        dic[B] = 0
        ans = 0
        for k, v in dic.items():
            ans += int(k)*v
        print(ans)

resolve()