#include <bits/stdc++.h>
using namespace std;
int n;
string r;
int main() {
  cin >> n;
  cin >> r;
  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    long long ret = 0;
    long long dm = 0;
    int d = 0;
    int m = 0;
    for (int i = 0; i < n; i++) {
      if (r[i] == 'C')
        ret += dm;
      else if (r[i] == 'D')
        d++;
      else if (r[i] == 'M') {
        dm += d;
        m++;
      }
      if (i >= k - 1) {
        int p = i - k + 1;
        if (r[p] == 'D') {
          dm -= m;
          d--;
        } else if (r[p] == 'M')
          m--;
      }
    }
    cout << ret << endl;
  }
  return 0;
}
