import collections
a = input()
A = list(map(int,input().split()))
N = int(input())
dic = collections.Counter(A)

for i in range(N):
    ans = 0
    b,c = map(int,input().split())
    p = dic[b]
    dic[c] += p
    dic[b] = 0
    k = list(dic.keys())
    v = list(dic.values())
    for i in range(len(k)):
        if v[i]==0:
            break
        ans += k[i]*v[i]
    print(ans)