from collections import Counter
n = int(input())

L = list(map(int,input().split()))
L = Counter(L)

q = int(input())

for i in range(q):
    sum1 = 0
    a,b = map(int,input().split())
    
    if a not in L:
        L[b] = 0
    else:
        L[b] +=L[a] 
    L[a] =0
    #print(L)
    for k,v in L.items():
        sum1 +=k*v
    print(sum1)