{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString.Char8       as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import           Data.List
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive.MutVar
import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           Unsafe.Coerce

main :: IO ()
main = do
    !n <- readLn :: IO Int
    cs <- getLine
    q <- readLn :: IO Int
    ks <- map read.words <$> getLine
    mapM_ (print.solve n cs) ks

solve :: Int -> String -> Int -> Int
solve n cs k = countDMC0 k cs

countDMC0 :: Int -> String -> Int
countDMC0 k = go 0 0 0 0 0 []
  where
    go !i !freqD !freqM !freqDM !freqDMC buf rest
        | i == k = countDMC freqD freqM freqDM freqDMC buf rest
        | otherwise = case rest of
            ('D':rest) -> go (i+1) (freqD+1) freqM freqDM freqDMC ('D':buf) rest
            ('M':rest) -> go (i+1) freqD (freqM+1) (freqDM+freqD) freqDMC ('M':buf) rest
            ('C':rest) -> go (i+1) freqD freqM freqDM (freqDMC+freqDM) ('C':buf) rest
            (c:rest) -> go (i+1) freqD freqM freqDM freqDMC (c:buf) rest
            _ -> undefined

countDMC :: Int -> Int -> Int -> Int -> String -> String -> Int
countDMC freqD0 freqM0 freqDM0 freqDMC0 = go freqDMC0 freqD0 freqM0 freqDM0 []
  where
    go !res !freqD !freqM !freqDM (f:fs) rs (r:rest) = go res' freqD' freqM' freqDM' fs (r:rs) rest
      where
        !freqD' = freqD - fromEnum (f == 'D') + fromEnum (r == 'D')
        !freqM' = freqM - fromEnum (f == 'M') + fromEnum (r == 'M')
        !freqDM' = freqDM - fromEnum (f == 'D') * freqM + (freqD - fromEnum (f == 'D')) * fromEnum (r == 'M')
        !res' = res + freqDM' * fromEnum (r == 'C')
    go !res !freqD !freqM !freqDM _ _ [] = res
    go !res !freqD !freqM !freqDM [] rs rest = go res freqD freqM freqDM (reverse rs) [] rest
