n = int(input())
l = input()
q = int(input())

l2= []
for i in range(q):
    _l = input().split()
    l = l.replace(_l[0], _l[1])
    l2.append(sum(map(int, l.split())))
    
for e in l2:
    print(e)