from math import floor,ceil,sqrt
import fractions
from collections import deque
import itertools
from collections import deque
import collections
import heapq
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
q=[list(map(int,input().split())) for _ in range(Q)]
nums={}
su=sum(A)
for i in range(N):
    if nums.get(A[i])==None:
        nums[A[i]]=1
    else:
        nums[A[i]]=nums[A[i]]+1
#print(nums)
for i in range(Q):
    a,b=q[i]
    anum=nums.pop(a,None)
    bnum=nums.pop(b,None)
    #print(anum,bnum)
    if anum!=None:
        if bnum!=None:
            su=su+(b-a)*anum
            nums[b]=bnum+anum
            print(su)
        else:
            su = su + (b - a) * anum
            nums[b]=anum
            print(su)
    else:
        print(su)