# -*- coding: utf-8 -*-

from sys import stdin
from functools import reduce

def load():
    n = int(stdin.readline())
    s = stdin.readline()
    q = int(stdin.readline())
    k = [int(x) for x in stdin.readline().split()]
    
    return (s, k)

def calc(s, ks):
    dmcmap = dmc(s, max(ks))
    for k in ks:
        print(sum(dmcmap[:k]))

def dmc(src, kmax):
    ret = [0 for _ in range(kmax)]

    for ind in range(len(src)):
        if src[ind] == 'D':
            for inm in range(ind+1, len(src)):
                if src[inm] == 'M':
                    for inc in range(inm+1, len(src)):
                        if src[inc] == 'C':
                            ret[inc-ind] += 1
    return ret

s, ks = load()
calc(s,ks)