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

d = {}
for i in range(q):
    _l = input().split()
    l = l.replace(_l[0], _l[1])
    d[i] = sum(map(int, l.split()))
    
for key in d.keys():
    print(d[key])