from collections import Counter

N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)

dic = Counter(A)

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    if b in dic.keys():
        # ans = sumA+(dic[b]*(c-b))

        B = dic[b]
        # print("B", B)
        if c in dic.keys():
            C = dic[c]
            dic[c] += dic[b]
            del dic[b]
        else:
            C = B
            dic[c] = dic[b]
            del dic[b]
        # print("C", C)
        
        sumA += (C*c-B*b)
        
        # print(dic)
    # else:
    #     ans = sumA
    print(sumA)