N= int(input())
A = list(map(int, input().split()))
Q = int(input())

baseline = sum(A)
seq = ''.join(str(n) for n in A)

for i in range(Q):
    b, c = input().split()
    n = seq.count(b)
    diff = int(c) - int(b)
    baseline += n * diff
    seq = seq.replace(b, c)

    print(baseline)