n = gets.to_i
s = gets.chomp

list = []
sumd,summ = 2.times.map{Array.new(n+1,0)}
for i in 0..n-1
    if s[i] == "D"
        sumd[i+1] += 1
    elsif s[i] == "M"
        summ[i+1] += 1
    end
end

for i in 0..n-1
    sumd[i+1] += sumd[i]
    summ[i+1] += summ[i]
end

q = gets.to_i
k = gets.split.map(&:to_i)
q.times do |i|
    dm = 0
    ans = 0
    for j in 0..n-1
        if j >= k[i] && s[j-k[i]] == "D"
            dm -= summ[j]-summ[j-k[i]]
        end
        if s[j] == "M"
            dm += sumd[j+1]-sumd[[0,j-k[i]+1].max]
        elsif s[j] == "C"
            ans += dm
        end
    end
    puts ans
end