from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)
flag = 0

for B,C in pala:
    res = 0
    for k , v in count.items():
        if k == B:
            k = C
            flag = 1
        res += (k * v)
    if flag == 1:
  #    print(count)
      count[C] += count.pop(B)
 #     print(count)
      flag = 0
    
    print(res)

