N = int(eval(input()))

A = {}

ans = 0

for a in input().split():

    a = int(a)

    if a not in A:

        A[a] = 1

    else:

        A[a] += 1

    ans += a

    

Q = int(eval(input()))

for i in range(Q):

    B, C = list(map(int, input().split()))

    if B in A:

        ans += (C-B) * A[B]

        if C not in A:

            A[C] = A[B]

        else:

            A[C] += A[B]

        A[B] = 0

        print(ans)

    else:

        print(ans)
import sys

input = sys.stdin.readline



N = int(eval(input()))

A = [int(i) for i in input().split()]

count = [0] * 10**5

for i in range(N):

        count[A[i]-1] += 1



Q = int(eval(input()))

ans = sum(A)

for i in range(Q):

    B, C = list(map(int, input().split()))

    ans += (C - B) * count[B-1]



    print(ans)



    count[C-1] += count[B-1]

    count[B-1] = 0
N = int(eval(input()))
A = {}
ans = 0
for a in input().split():
    a = int(a)
    if a not in A:
        A[a] = 1
    else:
        A[a] += 1
    ans += a
Q = int(eval(input()))
for i in range(Q):
    B, C = list(map(int, input().split()))
    if B in A:
        ans += (C - B) * A[B]
        if C not in A:
            A[C] = A[B]
        else:
            A[C] += A[B]
        A[B] = 0
        print(ans)
    else:
        print(ans)

import sys

input = sys.stdin.readline
N = int(eval(input()))
A = [int(i) for i in input().split()]
count = [0] * 10**5
for i in range(N):
    count[A[i] - 1] += 1
Q = int(eval(input()))
ans = sum(A)
for i in range(Q):
    B, C = list(map(int, input().split()))
    ans += (C - B) * count[B - 1]
    print(ans)
    count[C - 1] += count[B - 1]
    count[B - 1] = 0

+import sys + +input = sys.stdin.readline -A = {} -ans = 0 -for a in input().split(): -    a = int(a) -    if a not in A: -        A[a] = 1 -    else: -        A[a] += 1 -    ans += a +A = [int(i) for i in input().split()] +count = [0] * 10**5 +for i in range(N): +    count[A[i] - 1] += 1 +ans = sum(A) -    if B in A: -        ans += (C - B) * A[B] -        if C not in A: -            A[C] = A[B] -        else: -            A[C] += A[B] -        A[B] = 0 -        print(ans) -    else: -        print(ans) +    ans += (C - B) * count[B - 1] +    print(ans) +    count[C - 1] += count[B - 1] +    count[B - 1] = 0