n = gets.to_i
s = gets.chomp!
q = gets.to_i
k = gets.split.map(&:to_i)

cntD = [0]
cntM = [0]
cntDM = [0]
for i in 1..n
  c = s[i-1]
  v1 = c=="D" ? 1 : 0
  v2 = c=="M" ? 1 : 0
  dm = c == "M" ? cntD[i-1] : 0
  cntD[i] = cntD[i-1] + v1
  cntM[i] = cntM[i-1] + v2
  cntDM[i] = cntDM[i-1] + dm
end

for itv in k
  ans = 0
  for i in 1..n
    if s[i-1] == "C"
      left = (i-itv < 0) ? 1 : i-itv+1
      ans += cntDM[i-1] - cntDM[left] - cntD[left-1] * (cntM[i-1] - cntM[left])
    end
  end
  p ans
end
