import sys

N = int(sys.stdin.readline())
S = list(sys.stdin.readline())
Q = int(sys.stdin.readline())
k = list(map(int, sys.stdin.readline().split(' ')))

# chache = []

oms = [(i, S[i]) for i in range(N) if S[i] == 'D' or S[i] == 'M' or S[i] == 'C']
oml = len(oms)

def process(ki, index, selected = 'n'):
    global cache

    if index == oml:
      return []

    if cache[index][selected] is not None:
        # print('use cache')
        return cache[index][selected]

    rets = []
    # char = oms[index][1]
    if selected == 'b':
        for ind in range(index, oml):
            char = oms[ind][1]
            if char == 'C':
                rets.append(oms[ind][0])
    elif selected == 'a':
        for ind in range(index, oml):
            char = oms[ind][1]
            if char == 'M':
                rets += process(ki,ind+1, 'b')
    else:
        for ind in range(index, oml):
            char = oms[ind][1]
            if char == 'D':
                cind = process(ki,ind+1, 'a')
                ret = []
                for v in cind:
                    if v != False and ((v - oms[ind][0]) < ki):
                      ret.append(True)
                rets += ret

    cache[index][selected] = rets

    return rets


for ki in k:
    cache = [{'a': None, 'b': None, 'c': None, 'n': None} for i in range(oml)]
    ret = process(ki, 0)
    print(len(ret))
