def main():

    n = int(eval(input()))



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

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

    ans = 0

    for a in As:

        ans += a

        nums[a] += 1



    q = int(eval(input()))

    for i in range(q):

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

        ans += (c-b) * nums[b]

        nums[c] += nums[b]

        nums[b] = 0

        print(ans)





if __name__ == "__main__":

    main()

def main():

    n = int(eval(input()))

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

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

    q = int(eval(input()))

    bcs = [list(map(int, input().split())) for _ in range(q)]

    for A in As:

        As_Count[A] += 1

    ans = sum(As)

    for b, c in bcs:

        ans -= As_Count[b] * b

        ans += c * As_Count[b]

        As_Count[c] += As_Count[b]

        As_Count[b] = 0

        print(ans)





if __name__ == "__main__":

    main()

def main():
    n = int(eval(input()))
    As = list(map(int, input().split()))
    nums = [0] * (10**5 + 1)
    ans = 0
    for a in As:
        ans += a
        nums[a] += 1
    q = int(eval(input()))
    for i in range(q):
        b, c = list(map(int, input().split()))
        ans += (c - b) * nums[b]
        nums[c] += nums[b]
        nums[b] = 0
        print(ans)


if __name__ == "__main__":
    main()

def main():
    n = int(eval(input()))
    As = list(map(int, input().split()))
    As_Count = [0] * (10**5 + 1)
    q = int(eval(input()))
    bcs = [list(map(int, input().split())) for _ in range(q)]
    for A in As:
        As_Count[A] += 1
    ans = sum(As)
    for b, c in bcs:
        ans -= As_Count[b] * b
        ans += c * As_Count[b]
        As_Count[c] += As_Count[b]
        As_Count[b] = 0
        print(ans)


if __name__ == "__main__":
    main()

-    nums = [0] * (10**5 + 1) -    ans = 0 -    for a in As: -        ans += a -        nums[a] += 1 +    As_Count = [0] * (10**5 + 1) -    for i in range(q): -        b, c = list(map(int, input().split())) -        ans += (c - b) * nums[b] -        nums[c] += nums[b] -        nums[b] = 0 +    bcs = [list(map(int, input().split())) for _ in range(q)] +    for A in As: +        As_Count[A] += 1 +    ans = sum(As) +    for b, c in bcs: +        ans -= As_Count[b] * b +        ans += c * As_Count[b] +        As_Count[c] += As_Count[b] +        As_Count[b] = 0