n = gets.chomp.to_i
s = gets.chomp
q = gets.chomp.to_i
k = gets.chomp.split.map(&:to_i)
sum = 0
a = []
b = []
c = []
l = 0
s.each_byte { |byte|
  a << l if byte.chr == "D"
  b << l if byte.chr == "M"
  c << l if byte.chr == "C"
  l = l + 1
}
for v in 0..q - 1
  result = 0
  for w in 0..a.length - 1
    for x in 0..b.length - 1
      if a[w] < b[x]
        for y in 0..c.length - 1
          if b[x] < c[y]
            if c[y] - a[w] < k[v]
              result = result + 1
            end
          end
        end
      end
    end
  end
  puts result
end
