n = int(input())
list_str = input()
for a in range(int(input())):
    b, c = input().split()
    list_str = list_str.replace(b, c)
    list = list_str.split()
    list_int = [int(i) for i in list]
    print(sum(list_int))
    list_int = []