#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(),in.end()

template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

using ll = long long;
using ull = unsigned long long;
using namespace std;

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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

void func(long long N, std::string S, long long Q, std::vector<long long> k){

    lli d=0,m=0,dm=0;

    REP(j,0,Q){
        lli ans = 0;
        d=0,m=0,dm=0;
        REP(i,0,N){
            if(S[i]=='D')d++;
            if(S[i]=='M'){
                m++;
                dm+= d;
            }
            if(S[i]=='C')ans += dm;

            if(i<k[j]-1)continue;
            if(S[i-k[j]+1]=='D'){
                    dm -= m;
                    d--;
            }
            if(S[i-k[j]+1]=='M'){
                    m--;
            }
        }
        cout<<ans<<endl;
    }


}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    std::string S;
    std::cin >> S;
    long long Q;
    scanf("%lld",&Q);
    std::vector<long long> k(Q-1-0+1);
    for(int i = 0 ; i < Q-1-0+1 ; i++){
        scanf("%lld",&k[i]);
    }
    func(N, S, Q, std::move(k));
    return 0;
}
