n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for i in range(q)]
a_dict = {}
for i in a:
    if i not in a_dict:
        a_dict[i] = 1
    else:
        a_dict[i] += 1
        
for b, c in bc:
    sumA = 0
    if b in a_dict:
        bn = a_dict[b]
        a_dict[b] = 0
        if c in a_dict:
            a_dict[c] += bn
        else:
            a_dict[c] = bn
    for k, v in a_dict.items():
        sumA += k*v
    print(sumA)
