{-# LANGUAGE BangPatterns #-}

import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as BS
import Control.Monad

data Counter = Counter { cD :: {-# UNPACK #-} !Int,
                         cM :: {-# UNPACK #-} !Int,
                         cDM :: {-# UNPACK #-} !Int,
                         cAcc :: {-# UNPACK #-} !Int }

query :: BS.ByteString -> Int -> Int
query str k = cAcc $ VU.foldl' update cInit padded
  where
    cInit = Counter 0 0 0 0
    padded = VU.generate (BS.length str) $ \i ->
      (BS.index str i, if i < k then ' ' else BS.index str (i-k))
    update c0 (now,old) = c2
      where
        c2 = case now of
          'D' -> c1 { cD = cD c1 + 1 }
          'M' -> c1 { cM = cM c1 + 1, cDM = cDM c1 + cD c1 }
          'C' -> c1 { cAcc = cAcc c1 + cDM c1 }
          _   -> c1
        c1 = case old of
          'D' -> c0 { cD = cD c0 - 1, cDM = cDM c0 - cM c0 }
          'M' -> c0 { cM = cM c0 - 1 }
          _   -> c0

main :: IO ()
main = do
  getLine
  str <- BS.getLine
  q <- read <$> getLine
  ks <- VU.unfoldrN q (BS.readInt . BS.dropWhile (==' ')) <$> BS.getLine
  VU.mapM_ (putStrLn . show . query str) ks
