count =int(input())
nums=[]
nums=input().split(" ")
count2=int(input())

nums2=[]
for i in range(count):
    nums2.append(int(nums[i]))
print(nums2)

totals=[]
for i in range(count2):
    nums3=[]
    nums3=input().split(" ")
    num1=nums3[0]
    num2=nums3[1]
    
    total=0
    for k in range(count):
        if nums2[k]==num1:
            nums2[k]=num2
        total=total+nums2[k]
        if k == count-1:
            totals.append(total)
            

print(totals)
for i in range(1,len(totals)-1,+1):
    print(totals[i])