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

