object Main extends App {
  import scala.io.StdIn._

  val n = readInt
  val str = readLine.trim
  val q = readInt
  for (k ← readLine.trim.split(' ').map(_.toInt)){
    println(count(str, k))
  }
  def count(str: String, k: Int): Long = {
    def sub(current: Int, d: Long = 0, dm: Long = 0, m: Long = 0, result: Long = 0): Long = {
      if (current == str.length) result
      else{
        if (current < k) {
          if (str(current) == 'D') sub(current + 1, d + 1, dm, m, result)
          else if (str(current) == 'M') sub(current + 1, d, dm + d, m + 1, result)
          else if (str(current) == 'C') sub(current + 1, d, dm, m, result + dm)
          else sub(current + 1, d, dm, m, result)
        }else {
          val (nd, ndm, nm) = if (str(current - k) == 'D') (d - 1, dm - m, m) else if (str(current - k) == 'M') (d, dm, m - 1) else (d, dm, m)
          if (str(current) == 'D') sub(current + 1, nd + 1, ndm, nm, result)
          else if (str(current) == 'M') sub(current + 1, nd, ndm + nd, nm + 1, result)
          else if (str(current) == 'C') sub(current + 1, nd, ndm, nm, result + ndm)
          else sub(current + 1, nd, ndm, nm, result)
        }
      }
    }
    sub(0)
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
  }
}