


import sys
import collections
import itertools
import math
import re
from collections import Counter
from collections import deque
from collections import defaultdict

N = int(input())
S = input()
Q = int(input())
K = [int(x) for x in input().split()]


for k in K:
    ret = 0
    d = 0
    m = 0
    dm = 0

    for i in range(N):
        s = S[i]

        if s == "C":
            ret += dm

        if s == "D":
            d += 1
        elif s == "M":
            m += 1
            dm += d

        j = i - k + 1
        if j >= 0:
            s2 = S[j]
            if s2 == "D":
                d -= 1
                dm -= m
            elif s2 == "M":
                m -= 1
    print(ret)



sys.exit(0)