dd = (0, 0, -1, 0)
dm = (1, -1, 0, 0)
da = (1, 0, 0, 0)
md = (-1, 1, 2, 0)
mm = (0, 0, 1, 0)
ma = (0, 1, 1, 0)
cd = (-1, 0, -1, 1)
cm = (0, -1, 0, 1)
ca = (0, 0, 0, 1)
ad = (-1, 0, -1, 0)
am = (0, -1, 0, 0)
aa = (0, 0, 0, 0)

convert :: (Char, Char) -> (Int, Int, Int, Int)
convert ('D', c) =
  case c of
    'D' -> dd
    'M' -> dm
    _   -> da
convert ('M', c) =
  case c of
    'D' -> md
    'M' -> mm
    _   -> ma
convert ('C', c) =
  case c of
    'D' -> cd
    'M' -> cm
    _   -> ca
convert (_, c) =
  case c of
    'D' -> ad
    'M' -> am
    _   -> aa

count :: (Int, Int, Int, Int) -> (Int, Int, Int, Int) -> (Int, Int, Int, Int)
count (d, m, dm, dmc) (v0, v1, v2, v3) = (u0, u1, u2, u3)
  where
    u0 = d + v0
    u1 = m + v1
    u2 = dm + du2
    u3 = dmc + v3 * dm
    du2 = case v2 of
            0 -> 0
            1 -> d
            2 -> d - u1
            _ -> - u1

solve :: String -> Int -> Int
solve s k = ans
  where
    (_, _, _, ans) = foldl count (0, 0, 0, 0) $ map convert $ zip s $ (replicate (k - 1) '_') ++ s

main :: IO ()
main = do
  n <- readLn :: IO Int
  s <- getLine
  q <- readLn :: IO Int
  ks <- (fmap (\s -> read s :: Int)) . words <$> getLine
  mapM_ (print . (solve s)) ks
