n = int(input())

freq = {}
A = list(map(int,input().split()))
for i in range(len(A)):
    freq[A[i]] = freq.get(A[i],0) + 1

q = int(input())

for i in range(q):
    b,c = map(int,input().split())
    ans = 0
    if c not in freq.keys():
        freq[c] = freq.get(c,0) + 0
    if b in freq.keys():
        freq[c] += freq[b]
        freq[b] = 0
    for keys,values in freq.items():
        ans += keys*values
    print(ans)