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

from sys import stdin

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)
    ans = [sum(dmcmap[:k]) for k in ks]
    print(*ans, sep='\n', flush=True)

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

    dmap = [i for i, x in enumerate(src) if x == 'D']
    mmap = [i for i, x in enumerate(src) if x == 'M']
    cmap = [i for i, x in enumerate(src) if x == 'C']

    for d in dmap:
        for m in filter(lambda x: x > d, mmap):
            for c in filter(lambda x: x > m, cmap):
                ret[c-d] += 1
    return ret

stdin = open('data.txt')
s, ks = load()
calc(s,ks)