a=int(input()) # N
b=input().split(" ") # A
c=int(input()) # Q

final=[]
for i in range(c):
    t=0 # total
    d=input().split(" ")
    for i in range(len(b)):
        if int(d[0]) == int(b[i]): # replace
            b[i]=d[1]
        t+=int(b[i]) #sum
    final.append(t)
for j in range(c): # print result
    print (final[j])