n = int(input())
a = map(str, input().split())
a = " ".join(a)

m = int(input())
for i in range(m):
               b, c = map(str,input().split())
               #ans = sum([int(i) for i in a])
               a=a.replace(b,c)
               a=a.replace(" ","+")
               
               print(eval(a))