n=int(input())
a=[int(i) for i in input().split()]
a.sort()
q=int(input())
bc=[]
for i in range(q):
    bc.append([int(i) for i in input().split()])

import collections
collect = collections.Counter(a)
#print(collect)

for i in bc:
    #if i[0] in collect.keys():
    if i[1] in collect.keys():
        collect[i[1]] += collect.pop(i[0],0)
    else:
        collect[i[1]] = collect.pop(i[0],0)
    ans=0
    for k in collect.keys():
        ans += (k * collect[k])
    print(ans)