N = int(input())
As = input().split()
Q = int(input())
BCs = []
for a in range(Q):
    BCs.append(input().split())

for b in range(Q):
    for c in range(N):
        if As[c] == BCs[b][0]:
            As[c] = BCs[b][1]
    R = 0
    for d in As:
        R += int(d)
    print(R)