import numpy as np
N = int(input())
lsn = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    for j in range(len(lsn)):
        if b == lsn[j]:
            lsn[j]=c
    print(sum(np.where(lsn==b,c,lsn)))