//#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll inf = 1e9 + 7;
const ll Mod = 998244353;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

int N, Q, K[100];
string S;
ll DP[int(1e6 + 5)][3];

int main(){
  cin >> N >> S >> Q;
  for (int i = 0; i < Q; i++) cin >> K[i];

  for (int i = 0; i < Q; i++){
    DP[0][0] = 0; DP[0][1] = 0; DP[0][2] = 0;
    if (S[0] == 'D') DP[0][0] = 1;
    if (S[0] == 'M') DP[0][1] = 1;

    ll ans = 0;
    for (int j = 1; j < N; j++){
      for (int k = 0; k < 3; k++) DP[j][k] = DP[j - 1][k];

      if (j - K[i] >= 0){
        if (S[j - K[i]] == 'D'){
          DP[j][2] -= DP[j][1];
          DP[j][0]--;
        }
        if (S[j - K[i]] == 'M') DP[j][1]--;
      }

      if (S[j] == 'D') DP[j][0]++;
      if (S[j] == 'M'){
        DP[j][1]++;
        DP[j][2] += DP[j][0];
      }
      if (S[j] == 'C') ans += DP[j][2];
    }
    cout << ans << endl;
  }
  
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
struct SegmentTree {
  int sz;
  vector<int> d, m, dm;
  SegmentTree(string s) {
    int n = (int)(s.size());
    sz = 1;
    while(sz < n) sz *= 2;
    d.clear();
    m.clear();
    dm.clear();
    d.resize(2*sz+1, 0);
    m.resize(2*sz+1, 0);
    dm.resize(2*sz+1, 0);
    for(int i=0;i<n;++i) {
      if(s[i] == 'D') {
        d[i+sz-1] = 1;
      } else if(s[i] == 'M') {
        m[i+sz-1] = 1;
      }
    }
    for(int i=sz-2;i>=0;--i) {
      d[i] = d[2*i+1] + d[2*i+2];
      m[i] = m[2*i+1] + m[2*i+2];
      dm[i] = dm[2*i+1] + dm[2*i+2] + d[2*i+1] * m[2*i+2];
    }
  }
  tuple<int, int, int> query(int a, int b, int l, int r, int k) {
    if(b <= l || r <= a) return make_tuple(0, 0, 0);
    if(a <= l && r <= b) return make_tuple(d[k], m[k], dm[k]);
    tuple<int, int, int> v1 = query(a, b, l, (l+r)/2, 2*k+1);
    tuple<int, int, int> v2 = query(a, b, (l+r)/2, r, 2*k+2);
    int resd = get<0>(v1) + get<0>(v2);
    int resm = get<1>(v1) + get<1>(v2);
    int resdm = get<2>(v1) + get<2>(v2) + get<0>(v1) * get<1>(v2);
    return make_tuple(resd, resm, resdm);
  }
  tuple<int, int, int> query(int a, int b) {
    return query(a, b, 0, sz, 0);
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n; cin >> n;
  string s; cin >> s;
  vector<int> su(n);
  su[0] == s[0] == 'M';
  for(int i=1;i<n;++i) {
    su[i] = su[i-1] + (s[i] == 'M');
  }
  int q; cin >> q;
  for(int i=0;i<q;++i) {
    int k; cin >> k;
    ll cntd = 0, cntm = 0, now = 0, ans = 0;
    for(int j=0;j<k;++j) {
      if(s[j] == 'D') {
        cntd++;
      } else if(s[j] == 'M') {
        now += cntd;
      } else if(s[j] == 'C') {
        ans += now;
      }
    }
    for(int j=k;j<n;++j) {
      if(s[j-k] == 'D') {
        cntd--;
        now -= su[j-1] - su[j-k];
      }
      if(s[j] == 'D') {
        cntd++;
      } else if(s[j] == 'M') {
        now += cntd;
      } else if(s[j] == 'C') {
        ans += now;
      }
    }
    cout << ans << endl;
  }
}
