def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from collections import Counter

    N = int(input())
    A = list(map(int, input().split()))
    CA = Counter(A)
    sumA = 0
    for key, value in CA.items():
        sumA += key*value

    Q = int(input())
    for i in range(Q):
        b, c = map(int, input().split())
        sumA = (c-b)*CA[b]
        CA[c] += CA[b]
        CA[b] = 0
        print(sumA)

main()