def solve():
    N = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    q = int(input())
    n = [0 for _ in range(100000)]
    for i in a:
        n[i] += 1
    for _ in range(q):
        b, c = map(int, input().split())
        s += (c - b) * n[b]
        print(s)
        n[c] += n[b]
        n[b] = 0


if __name__ == '__main__':
    solve()
