local mfl = math.floor

local function comp(a, b)
  return a < b
end

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if not comp(ary[1], x) then return 1 end
  if comp(ary[num], x) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local n = io.read("*n", "*l")
local s = io.read()
local m_cnt = {}
local d_pos = {}
local c_pos = {}
local ss = ""
local mc = 0LL
for i = 1, n do
  ss = s:sub(i, i)
  if ss == "D" then
    table.insert(d_pos, i)
  elseif ss == "C" then
    table.insert(c_pos, i)
  elseif ss == "M" then
    mc = mc + 1LL
  end
  m_cnt[i] = mc
end
local m_sum_per_c = {}
for i = 1, #c_pos do
  local ic = c_pos[i]
  if i == 1 then
    m_sum_per_c[i] = m_cnt[ic]
  else
    m_sum_per_c[i] = m_sum_per_c[i - 1] + m_cnt[ic]
  end
end

local function llprint(llnumber)
  local str = tostring(llnumber):gsub("LL", "")
  print(str)
end

local c_froms = {}
for id = 1, #d_pos do
  c_froms[id] = lower_bound(c_pos, d_pos[id])
end
local q = io.read("*n")
for i = 1, q do
  local k = io.read("*n")
  local ret = 0LL
  for id = 1, #d_pos do
    local dp = d_pos[id]
    local c_from = c_froms[id]
    local c_to = lower_bound(c_pos, dp + k) - 1
    if 1 <= c_to then
      ret = ret + m_sum_per_c[c_to] - (c_to - c_from + 1) * m_cnt[dp]
      if 1 < c_from then
        ret = ret - m_sum_per_c[c_from - 1]
      end
    end
  end
  llprint(ret)
end
