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

data = {}

for i in range(len(A)):
    a = A[i]
    if a in data.keys():
        data[a].append(i)
    else:
        data[a] = [i]


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

    temp = data.get(B)

    if temp is None:
        print(sum(A))
        continue

    for j in temp:
        A[j] = C

    if C in data.keys():
        data[C].extend(temp)
        del data[B]
    else:
        data[C] = data[B]

        del data[B]

    print(sum(A))