from collections import Counter


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(A)
    q = int(input())
    s = sum(a)
    for _ in range(q):
        b, c = map(int, input().split())
        s -= C[b]*b
        s += C[b]*c
        C[c] += c[b]
        c[b] = 0
        print(s)

if __name__ == "__main__":
    main()
