n = int(input())
a = input().split()
alist = set()
adic = {}
for i in a:
    if int(i) in alist:
        adic[int(i)] = adic[int(i)] + 1
    else:
        alist.add(int(i))
        adic[int(i)] = 1

q = int(input())
ans = []

for i in range(q):
    s = input().split()
    b = int(s[0])
    c = int(s[1])
    su = 0
    if b in alist:
        if not(c in alist):
            adic[c] = 0
            alist.add(c)
        adic[c] = adic[c] + adic[b]
        adic[b] = 0
    for j in adic:
        su += j * adic[j]
    ans.append(su)
for i in range(len(ans)):
    print(ans[i])