def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    answer = sum(A)
    numNumberDict = {}
    Aset = set(A)
    
    for Ai in Aset:
        numNumberDict[Ai] = A.count(Ai)
        
    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberDict[B]

        numNumberDict[C] = numNumberDict[C] + numNumberDict[B]
        numNumberDict[B] = 0

        print(answer)

if __name__ == "__main__":
    main()