import sys

readline = sys.stdin.readline

 

 

def st(line):

    tmp = ''

    for l in line:

        if l == '\n':

            yield tmp

        elif l == ' ':

            yield tmp

            tmp = ''

        else:

            tmp += l







N = int(eval(input()))

As = st(readline())



d = dict()

total = 0

for a in As:

    a = int(a)



    if a in d:

        d[a] += 1

    else:

        d[a] = 1

    total += a



Q = int(eval(input()))

queries = (list(map(int, readline().strip().split())) for _ in range(Q))



for que in queries:

    b, c = que



    if b not in d:

        print(total)

        continue



    n_b = d[b]

    d[b] = 0



    if c not in d:

        d[c] = 0



    d[c] += n_b

    total += (c - b) * n_b



    print(total)



import sys

readline = sys.stdin.readline





def st(line):

    tmp = ''

    for l in line:

        if l == '\n':

            yield tmp

        elif l == ' ':

            yield tmp

            tmp = ''

        else:

            tmp += l





def solve():

    N = int(eval(input()))

    As = st(readline())



    d = dict()

    total = 0

    for a in As:

        a = int(a)



        if a in d:

            d[a] += 1

        else:

            d[a] = 1

        total += a



    Q = int(eval(input()))

    queries = (list(map(int, readline().strip().split())) for _ in range(Q))



    for que in queries:

        b, c = que



        if b not in d:

            print(total)

            continue



        n_b = d[b]

        d[b] = 0



        if c not in d:

            d[c] = 0



        d[c] += n_b

        total += (c - b) * n_b



        print(total)





def main():

    solve()





if __name__ == "__main__":

    main()
import sys

readline = sys.stdin.readline


def st(line):
    tmp = ""
    for l in line:
        if l == "\n":
            yield tmp
        elif l == " ":
            yield tmp
            tmp = ""
        else:
            tmp += l


N = int(eval(input()))
As = st(readline())
d = dict()
total = 0
for a in As:
    a = int(a)
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
    total += a
Q = int(eval(input()))
queries = (list(map(int, readline().strip().split())) for _ in range(Q))
for que in queries:
    b, c = que
    if b not in d:
        print(total)
        continue
    n_b = d[b]
    d[b] = 0
    if c not in d:
        d[c] = 0
    d[c] += n_b
    total += (c - b) * n_b
    print(total)

import sys

readline = sys.stdin.readline


def st(line):
    tmp = ""
    for l in line:
        if l == "\n":
            yield tmp
        elif l == " ":
            yield tmp
            tmp = ""
        else:
            tmp += l


def solve():
    N = int(eval(input()))
    As = st(readline())
    d = dict()
    total = 0
    for a in As:
        a = int(a)
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
        total += a
    Q = int(eval(input()))
    queries = (list(map(int, readline().strip().split())) for _ in range(Q))
    for que in queries:
        b, c = que
        if b not in d:
            print(total)
            continue
        n_b = d[b]
        d[b] = 0
        if c not in d:
            d[c] = 0
        d[c] += n_b
        total += (c - b) * n_b
        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()

-N = int(eval(input())) -As = st(readline()) -d = dict() -total = 0 -for a in As: -    a = int(a) -    if a in d: -        d[a] += 1 -    else: -        d[a] = 1 -    total += a -Q = int(eval(input())) -queries = (list(map(int, readline().strip().split())) for _ in range(Q)) -for que in queries: -    b, c = que -    if b not in d: +def solve(): +    N = int(eval(input())) +    As = st(readline()) +    d = dict() +    total = 0 +    for a in As: +        a = int(a) +        if a in d: +            d[a] += 1 +        else: +            d[a] = 1 +        total += a +    Q = int(eval(input())) +    queries = (list(map(int, readline().strip().split())) for _ in range(Q)) +    for que in queries: +        b, c = que +        if b not in d: +            print(total) +            continue +        n_b = d[b] +        d[b] = 0 +        if c not in d: +            d[c] = 0 +        d[c] += n_b +        total += (c - b) * n_b -        continue -    n_b = d[b] -    d[b] = 0 -    if c not in d: -        d[c] = 0 -    d[c] += n_b -    total += (c - b) * n_b -    print(total) + + +def main(): +    solve() + + +if __name__ == "__main__": +    main()