#include <bits/stdc++.h>
 
using namespace std;

typedef int64_t i64;

 
int main()
{
  int n,q, k;
	string s;
  cin >> n >> s >> q;
 
  while (q--) {
    cin >> k;
    i64 r = 0, dc = 0, mc = 0, mdc = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'D') {
        dc++;
      } else if (s[i] == 'M') {
        mc++;
        mdc += dc;
      } else if (s[i] == 'C') {
        r += mdc;
      }
 
      if (i >= k-1) {
        if (s[i-k+1] == 'D') {
          dc--;
          mdc -= mc;
        } else if (s[i-k+1] == 'M') {
          mc--;
        }
      }
    }
    cout << r << endl;
  }
 
  return 0;
}