n1 = gets.to_i
wo = gets.chomp.split("")
n2 = gets.to_i
n3 = gets.chomp.split(" ").map(&:to_i)
d = Array.new(n1, 0)
m = Array.new(n1, 0)
c = Array.new(n1, 0)

for i in 0..n1 - 1
  if wo[i] == "D"
    d[i] += 1
  elsif wo[i] == "M"
    m[i] += 1
  elsif wo[i] == "C"
    c[i] += 1
  end
end

for i in 0..n2 - 1
  ans = 0
  for j in 0..n1 - 1
    if wo[j] == "D"
      inst = 0
      for k in 1..n3[i] - 1
        if wo[j + k] == "M"
          inst += 1
        elsif wo[j + k] == "C"
          ans += inst
        end
      end
    end
  end
  puts ans
end
