n = int(input())
a = list(map(int, input().split()))
q = int(input())
aa = [0 for i in range(10 ** 5)]
for j in a:
    aa[j] += 1
suma = sum(a)
for f in range(q):
    b, c = map(int, input().split())
    suma += (c-b) * aa[b]
    aa[c] += aa[b]
    aa[b] = 0
    print(suma)
    