N = int(input())

CNT = {}
S = 0

for a in map(int, input().split()):
    if not(a in CNT):
        CNT[a] = 0

    CNT[a] += 1
    S += a

Q = int(input())
for _ in range(Q):
    B,C = map(int, input().split())

    if B in CNT:
        cnt = CNT[B]
        S -= B*cnt
        S += C*cnt
        
        if not(C in CNT):
            CNT[C] = 0

        CNT[C] += cnt
        
    print(S)

