import sys

input=sys.stdin.readline

n=int(eval(input()))

l=eval(input())

q=int(eval(input()))

kl=tuple(map(int,input().split()))

for k in kl:

    d=0

    m=0

    dm=0

    ans=0

    for j in range(n):

        if l[j]=='D':

            d+=1

        elif l[j]=='M':

            m+=1

            dm+=d

        elif l[j]=='C':

            ans+=dm

        if j>=k-1:

            if l[j-k+1]=='D':

                dm-=m

                d-=1

            elif l[j-k+1]=='M':

                m-=1

    print(ans)

import sys

input=sys.stdin.readline



def main():

    n=int(eval(input()))

    l=eval(input())

    q=int(eval(input()))

    kl=tuple(map(int,input().split()))

    for k in kl:

        d=0

        m=0

        dm=0

        ans=0

        for j in range(n):

            if l[j]=='D':

                d+=1

            elif l[j]=='M':

                m+=1

                dm+=d

            elif l[j]=='C':

                ans+=dm

            if j>=k-1:

                if l[j-k+1]=='D':

                    dm-=m

                    d-=1

                elif l[j-k+1]=='M':

                    m-=1

        print(ans)

if __name__=='__main__':

    main()
import sys

input = sys.stdin.readline
n = int(eval(input()))
l = eval(input())
q = int(eval(input()))
kl = tuple(map(int, input().split()))
for k in kl:
    d = 0
    m = 0
    dm = 0
    ans = 0
    for j in range(n):
        if l[j] == "D":
            d += 1
        elif l[j] == "M":
            m += 1
            dm += d
        elif l[j] == "C":
            ans += dm
        if j >= k - 1:
            if l[j - k + 1] == "D":
                dm -= m
                d -= 1
            elif l[j - k + 1] == "M":
                m -= 1
    print(ans)

import sys

input = sys.stdin.readline


def main():
    n = int(eval(input()))
    l = eval(input())
    q = int(eval(input()))
    kl = tuple(map(int, input().split()))
    for k in kl:
        d = 0
        m = 0
        dm = 0
        ans = 0
        for j in range(n):
            if l[j] == "D":
                d += 1
            elif l[j] == "M":
                m += 1
                dm += d
            elif l[j] == "C":
                ans += dm
            if j >= k - 1:
                if l[j - k + 1] == "D":
                    dm -= m
                    d -= 1
                elif l[j - k + 1] == "M":
                    m -= 1
        print(ans)


if __name__ == "__main__":
    main()

-n = int(eval(input())) -l = eval(input()) -q = int(eval(input())) -kl = tuple(map(int, input().split())) -for k in kl: -    d = 0 -    m = 0 -    dm = 0 -    ans = 0 -    for j in range(n): -        if l[j] == "D": -            d += 1 -        elif l[j] == "M": -            m += 1 -            dm += d -        elif l[j] == "C": -            ans += dm -        if j >= k - 1: -            if l[j - k + 1] == "D": -                dm -= m -                d -= 1 -            elif l[j - k + 1] == "M": -                m -= 1 -    print(ans) + + +def main(): +    n = int(eval(input())) +    l = eval(input()) +    q = int(eval(input())) +    kl = tuple(map(int, input().split())) +    for k in kl: +        d = 0 +        m = 0 +        dm = 0 +        ans = 0 +        for j in range(n): +            if l[j] == "D": +                d += 1 +            elif l[j] == "M": +                m += 1 +                dm += d +            elif l[j] == "C": +                ans += dm +            if j >= k - 1: +                if l[j - k + 1] == "D": +                    dm -= m +                    d -= 1 +                elif l[j - k + 1] == "M": +                    m -= 1 +        print(ans) + + +if __name__ == "__main__": +    main()