import numpy as np

from numba import njit



N = int(eval(input()))

S = eval(input())

Q = int(eval(input()))

ks = list(map(int, input().split()))



def func(ch):

    if ch == 'D':

        return 1

    if ch == 'M':

        return 2

    if ch == 'C':

        return 3

    return 0

    

S_arr = np.array([func(ch) for ch in S], dtype = np.int32)



@njit("i8(i8, i4[:], i8)", cache = True)

def solve(N, S_arr, k):

    ans = 0

    D = 0

    M = 0

    DM = 0

    for i in range(k):

        if S_arr[i] == 1:

            D += 1

        elif S_arr[i] == 2:

            DM += D

            M += 1

        elif S_arr[i] == 3:

            ans += DM

    for i in range(k, N):

        if S_arr[i-k] == 1:

            D -= 1

            DM -= M

        elif S_arr[i-k] == 2:

            M -= 1



        if S_arr[i] == 1:

            D += 1

        elif S_arr[i] == 2:

            DM += D

            M += 1

        elif S_arr[i] == 3:

            ans += DM



    return ans



for k in ks:

    print((solve(N, S_arr, k)))

    
import numpy as np

from numba import njit



def solve(N, S_arr, k):

    ans = 0

    D = 0

    M = 0

    DM = 0

    for i in range(k):

        if S_arr[i] == 1:

            D += 1

        elif S_arr[i] == 2:

            DM += D

            M += 1

        elif S_arr[i] == 3:

            ans += DM

    for i in range(k, N):

        if S_arr[i-k] == 1:

            D -= 1

            DM -= M

        elif S_arr[i-k] == 2:

            M -= 1



        if S_arr[i] == 1:

            D += 1

        elif S_arr[i] == 2:

            DM += D

            M += 1

        elif S_arr[i] == 3:

            ans += DM



    return ans



def main():

    N = int(eval(input()))

    S = eval(input())

    Q = int(eval(input()))

    ks = list(map(int, input().split()))



    def func(ch):

        if ch == 'D':

            return 1

        if ch == 'M':

            return 2

        if ch == 'C':

            return 3

        return 0

        

    S_arr = np.array([func(ch) for ch in S], dtype = np.int32)

    for k in ks:

        print((solve(N, S_arr, k)))



def cc_export():

    from numba.pycc import CC

    cc = CC('my_module')

    cc.export('solve', "i8(i8, i4[:], i8)")(solve)

    cc.compile()



if __name__ == '__main__':

    import sys

    if sys.argv[-1] == 'ONLINE_JUDGE':

        cc_export()

        exit(0)

    from my_module import solve

    main()

    
import numpy as np
from numba import njit

N = int(eval(input()))
S = eval(input())
Q = int(eval(input()))
ks = list(map(int, input().split()))


def func(ch):
    if ch == "D":
        return 1
    if ch == "M":
        return 2
    if ch == "C":
        return 3
    return 0


S_arr = np.array([func(ch) for ch in S], dtype=np.int32)


@njit("i8(i8, i4[:], i8)", cache=True)
def solve(N, S_arr, k):
    ans = 0
    D = 0
    M = 0
    DM = 0
    for i in range(k):
        if S_arr[i] == 1:
            D += 1
        elif S_arr[i] == 2:
            DM += D
            M += 1
        elif S_arr[i] == 3:
            ans += DM
    for i in range(k, N):
        if S_arr[i - k] == 1:
            D -= 1
            DM -= M
        elif S_arr[i - k] == 2:
            M -= 1
        if S_arr[i] == 1:
            D += 1
        elif S_arr[i] == 2:
            DM += D
            M += 1
        elif S_arr[i] == 3:
            ans += DM
    return ans


for k in ks:
    print((solve(N, S_arr, k)))

import numpy as np
from numba import njit


def solve(N, S_arr, k):
    ans = 0
    D = 0
    M = 0
    DM = 0
    for i in range(k):
        if S_arr[i] == 1:
            D += 1
        elif S_arr[i] == 2:
            DM += D
            M += 1
        elif S_arr[i] == 3:
            ans += DM
    for i in range(k, N):
        if S_arr[i - k] == 1:
            D -= 1
            DM -= M
        elif S_arr[i - k] == 2:
            M -= 1
        if S_arr[i] == 1:
            D += 1
        elif S_arr[i] == 2:
            DM += D
            M += 1
        elif S_arr[i] == 3:
            ans += DM
    return ans


def main():
    N = int(eval(input()))
    S = eval(input())
    Q = int(eval(input()))
    ks = list(map(int, input().split()))

    def func(ch):
        if ch == "D":
            return 1
        if ch == "M":
            return 2
        if ch == "C":
            return 3
        return 0

    S_arr = np.array([func(ch) for ch in S], dtype=np.int32)
    for k in ks:
        print((solve(N, S_arr, k)))


def cc_export():
    from numba.pycc import CC

    cc = CC("my_module")
    cc.export("solve", "i8(i8, i4[:], i8)")(solve)
    cc.compile()


if __name__ == "__main__":
    import sys

    if sys.argv[-1] == "ONLINE_JUDGE":
        cc_export()
        exit(0)
    from my_module import solve

    main()

-N = int(eval(input())) -S = eval(input()) -Q = int(eval(input())) -ks = list(map(int, input().split())) - -def func(ch): -    if ch == "D": -        return 1 -    if ch == "M": -        return 2 -    if ch == "C": -        return 3 -    return 0 - - -S_arr = np.array([func(ch) for ch in S], dtype=np.int32) - - -@njit("i8(i8, i4[:], i8)", cache=True) -for k in ks: -    print((solve(N, S_arr, k))) +def main(): +    N = int(eval(input())) +    S = eval(input()) +    Q = int(eval(input())) +    ks = list(map(int, input().split())) + +    def func(ch): +        if ch == "D": +            return 1 +        if ch == "M": +            return 2 +        if ch == "C": +            return 3 +        return 0 + +    S_arr = np.array([func(ch) for ch in S], dtype=np.int32) +    for k in ks: +        print((solve(N, S_arr, k))) + + +def cc_export(): +    from numba.pycc import CC + +    cc = CC("my_module") +    cc.export("solve", "i8(i8, i4[:], i8)")(solve) +    cc.compile() + + +if __name__ == "__main__": +    import sys + +    if sys.argv[-1] == "ONLINE_JUDGE": +        cc_export() +        exit(0) +    from my_module import solve + +    main()