N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dic = dict()

for i in A:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for i in range(Q):
    b, c = list(map(int, input().split()))
    if b in dic:
        if c in dic:    
            dic[c] += dic.pop(b)
        else:
            dic[c] = dic.pop(b)
    ans = 0
    for num in dic:
        ans += num * dic[num]
    print(ans)