import collections



N = int(eval(input()))

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

S = sum(A)

Q = int(eval(input()))

T = collections.Counter(A)

for i in range(Q):

    b, c = list(map(int, input().split()))

    S += T[b]*(c-b)

    T[c] += T[b]

    T[b] = 0

    print(S)
def main():

    n, *i = list(map(int, open(0).read().split()))

    a = i[:n]

    _ = i[n]

    s = sum(a)



    m = [0] * (10 ** 5 + 1)

    for x in a:

        m[x] += 1



    ans = []

    for b, c in zip(*[iter(i[n + 1:])] * 2):

        m[c] += m[b]

        s += (c - b) * m[b]

        m[b] = 0

        ans.append(s)



    print(("\n".join(map(str, ans))))



if __name__ == '__main__':

    main()
import collections

N = int(eval(input()))
A = list(map(int, input().split()))
S = sum(A)
Q = int(eval(input()))
T = collections.Counter(A)
for i in range(Q):
    b, c = list(map(int, input().split()))
    S += T[b] * (c - b)
    T[c] += T[b]
    T[b] = 0
    print(S)

def main():
    n, *i = list(map(int, open(0).read().split()))
    a = i[:n]
    _ = i[n]
    s = sum(a)
    m = [0] * (10**5 + 1)
    for x in a:
        m[x] += 1
    ans = []
    for b, c in zip(*[iter(i[n + 1 :])] * 2):
        m[c] += m[b]
        s += (c - b) * m[b]
        m[b] = 0
        ans.append(s)
    print(("\n".join(map(str, ans))))


if __name__ == "__main__":
    main()

-import collections +def main(): +    n, *i = list(map(int, open(0).read().split())) +    a = i[:n] +    _ = i[n] +    s = sum(a) +    m = [0] * (10**5 + 1) +    for x in a: +        m[x] += 1 +    ans = [] +    for b, c in zip(*[iter(i[n + 1 :])] * 2): +        m[c] += m[b] +        s += (c - b) * m[b] +        m[b] = 0 +        ans.append(s) +    print(("\n".join(map(str, ans)))) -N = int(eval(input())) -A = list(map(int, input().split())) -S = sum(A) -Q = int(eval(input())) -T = collections.Counter(A) -for i in range(Q): -    b, c = list(map(int, input().split())) -    S += T[b] * (c - b) -    T[c] += T[b] -    T[b] = 0 -    print(S) + +if __name__ == "__main__": +    main()