import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    numbers = {}
    for ai in a:
        if not ai in numbers:
            numbers[ai] = 0
        numbers[ai] += 1
    s = sum(a)
    for b, c in bc:
        if b in numbers:
            s = s + (-b + c) * numbers[b]
            if not c in numbers:
                numbers[c] = 0
            numbers[c] += numbers[b]
            del numbers[b]
        print s
    return




n = RN()
a = RA()
q = RN()
bc = [RA() for _ in range(q)]
solve()
