N = input()
P = input()
Q = int(input())

sumP = sum(map(int, P.split()))
for i in range(Q):
    B, C = map(str, input().split())
    sumP -= (int(B)-int(C)) * P.count(B)
    P = P.replace(B, C)
    print(sumP)