n = int(input())

a = list(map(int, input().split()))

q = int(input())

#初期化

a_dic ={}


for i in a:
    if i not in a_dic:
        a_dic[i] = i
    else:
        a_dic[i] += i

#a_sorted = sorted(a)
for i in range(q):
    ans = 0
    b,c = map(int, input().split())
    #print(a_dic)
    
    if b in a_dic:
        if c not in a_dic:
            a_dic[c] = a_dic[b]/b * c 

        else:
            tmp_c = a_dic[c]
            tmp_c += a_dic[b]/b*c
            a_dic[c] = tmp_c
        a_dic.pop(b) 
    print(int(sum(a_dic.values())))