N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

for B,C in pala:
    res = 0
    for i in range(len(data)):
        if data[i] == B:
            data[i] = C
        res += data[i]
    print(res)