DBG = !true
n = gets.to_i
s = gets.chomp!
q = gets.to_i
ks = gets.split.map{|x| x.to_i}

def selgoodc(d,k,cs)
  a = []
  for c in cs
    a << c if d < c && c < d+k
  end
  return a
end

def selgoodm(d,c,ms)
  return selgoodc(d,c-d,ms)
end

ds = []
ms = []
cs = []
for i in 0...s.size
  ds << i if s[i] == 'D'
  ms << i if s[i] == 'M'
  cs << i if s[i] == 'C'
end

if DBG
  print "----\n"
  print ds
  print "----\n"
  print ms
  print "----\n"
  print cs
  print "----\n"
end

for k in ks
  sum = 0
  for d in ds
    goodcs = selgoodc(d,k,cs)
    if DBG
      printf("k:%d d:%d goodcs:", k, d)
      print goodcs
      print "\n"
    end

    for c in goodcs
      goodms = selgoodm(d,c,ms)
      if DBG
        printf("c:%d goodms:", c)
        print goodms
        printf("  -- add %d\n", goodms.size)
      end
      sum += goodms.size
    end
  end
  puts sum
end
