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)

for B,C in pala:
    res = 0
    if B in count:
      count[C] += count.pop(B)

    for k , v in count.items():
        res += (k * v)
    
    print(res)

