DBG = !true
n = gets.to_i
s = gets.chomp!
q = gets.to_i
ks = gets.split.map{|x| x.to_i}

for k in ks
  d = m = dm = dmc = 0
  for i in 0...n
    if i >= k
      if s[i-k] == 'D'
        d -= 1
        dm -= m
      elsif s[i-k] == 'M'
        m -= 1
      end
    end

    if s[i] == 'D'
      d += 1
    elsif s[i] == 'M'
      m += 1
      dm += d
    elsif s[i] == 'C'
      dmc += dm
    end

  end
  printf("%d\n", dmc)
end
