import sys
from collections import defaultdict, deque
import math

# import copy
# from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 998244353

def solve():
    n = getN()
    nums = getList()
    d = defaultdict(int)
    for num in nums:
        d[num] += 1
    ans = sum(nums)
    # print(d)
    Q = getN()
    for i in range(Q):
        b, c = getList()
        if b in d and d[b] > 0:
            ans -= b * d[b]
            ans += c * d[b]
            d[c] += d[b]

        print(ans)



def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    # main()
    solve()