solve1 :: String -> String -> Int -> Int -> Int -> Int -> Int -> (Int, Int, Int, Int)
solve1 [] orig k d m dm dmc = (d, m, dm, dmc)
solve1 s orig 1 d m dm dmc = solve2 s orig d m dm dmc
solve1 (c:rs) orig k d m dm dmc =
  case c of
    'D' -> solve1 rs orig (k - 1) (d + 1) m dm dmc
    'M' -> solve1 rs orig (k - 1) d (m + 1) (dm + d) dmc
    'C' -> solve1 rs orig (k - 1) d m dm (dmc + dm)
    _ -> solve1 rs orig (k - 1) d m dm dmc

solve2 :: String -> String -> Int -> Int -> Int -> Int -> (Int, Int, Int, Int)
solve2 "" _ d m dm dmc = (d, m, dm, dmc)
solve2 (c:rs) (c1:bs) d m dm dmc =
  case (c, c1) of
    ('D', 'D') -> solve2 rs bs d m (dm - m) dmc
    ('D', 'M') -> solve2 rs bs (d + 1) (m - 1) dm dmc
    ('D', _) -> solve2 rs bs (d + 1) m dm dmc
    ('M', 'D') -> solve2 rs bs (d - 1) (m + 1) (dm + d - 1 - m) dmc
    ('M', 'M') -> solve2 rs bs d m (dm + d) dmc
    ('M', _) -> solve2 rs bs d (m + 1) (dm + d) dmc
    ('C', 'D') -> solve2 rs bs (d - 1) m (dm - m) (dmc + dm)
    ('C', 'M') -> solve2 rs bs d (m - 1) dm (dmc + dm)
    ('C', _) -> solve2 rs bs d m dm (dmc + dm)
    (_, 'D') -> solve2 rs bs (d - 1) m (dm - m) dmc
    (_, 'M') -> solve2 rs bs d (m - 1) dm dmc
    _ -> solve2 rs bs d m dm dmc

solve :: String -> Int -> Int
solve s k = ans
  where
    (_, _, _, ans) = solve1 s s k 0 0 0 0

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
