n = int(input())
a = list(map(int, input().split()))
mp = {}
for i in range(n):
    if(a[i] in mp.keys()):
        mp[a[i]] += 1
    else:
        mp[a[i]] = 1
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    if(b not in mp.keys()):
        mp[b] = 0
    if(c in mp.keys()):
        mp[c] += mp[b]
    else:
        mp[c] = mp[b]
    mp[b] = ans = 0
    for k in mp.keys():
        ans += k * mp[k]
    print(ans)