#include <bits/stdc++.h>
using namespace std;
signed main() {
  char s[1000010];
  char a;
  long long N, Q;
  long long x = 0;
  long long ans = 0;
  long long k[100];
  long long l[4];
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> a;
    if (a == 'D' || a == 'M' || a == 'C') {
      s[x] = a;
      x++;
    }
  }
  cin >> Q;
  for (long long i = 0; i < Q; i++) {
    cin >> k[i];
    long long td = 0, tm = 0, tc = 0;
    long long count = 0;
    long long ans = 0;
    for (long long j = 0; j < x; j++) {
      if (s[j] == 'D') {
        td++;
      }
      if (j >= k[i]) {
        if (s[j - k[i]] == 'D') {
          td--;
        }
      }
      if (j >= k[i]) {
        if (s[j - k[i]] == 'D') {
          tm -= count;
        }
      }
      if (s[j] == 'M') {
        tm += td;
        count++;
      }
      if (j >= k[i]) {
        if (s[j - k[i]] == 'M') {
          count--;
        }
      }
      if (s[j] == 'C') {
        ans += tm;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
