N = STDIN.gets.chomp.to_i
S = STDIN.gets.chomp.split('')
Q = STDIN.gets.chomp.to_i
k = STDIN.gets.chomp.split(' ').map(&:to_i)

$chache = []


def process(k, index, selected = :n)
  if index == N
    return [false]
  end

  return $cache[index][selected] if $cache[index][selected]

  rets = []
  if selected == :b
    char = S[index]
    if char == 'C'
      rets.push(index)
    end
    rets.push(process(k,index+1, selected))
  elsif selected == :a
    char = S[index]
    if char == 'M'
      rets.push(process(k,index+1, :b))
    end
    rets.push(process(k,index+1, selected))
  else
    char = S[index]
    if char == 'D'
      cind = process(k,index+1, :a)
      ret = cind.map do |v|
        if v == false
          false
        elsif v - index < k
          true
        else
          false
        end
      end
      rets.push(ret)
      # rets.push(process(index+1, :a))
    end
    rets.push(process(k,index+1, selected))
  end

  r = rets.flatten
  $cache[index][selected] = r
  return r
end

k.each do |ki|
  $cache = Array.new(N) { { n: nil, a: nil, b: nil, c: nil } }
  ret = process(ki, 0).count(true)
  puts ret
end
