#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P std::pair<int,int>
#define PiP std::pair<int,std::pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp std::make_pair
#define prique(T) std::priority_queue<T,vector<T>,greater<T>>
using namespace std;
template<class T> inline void chmax(T& a, T b) { a = std::max(a, b); }
template<class T> inline void chmin(T& a, T b) { a = std::min(a, b); }

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
	std::random_device rnd;
	std::mt19937 mt32(rnd());
	std::uniform_int_distribution<int>engine(MIN, MAX);
	return engine(mt32);
}
/*--------Library Zone!--------*/

int n, q;
string s;
int summ[1000005];
signed main() {
	cin >> n >> s >> q;
	rep(i, n) {
		if (s[i] == 'M')summ[i]++;
		summ[i + 1] = summ[i];
	}
	while (q--) {
		int k; cin >> k;
		int ans = 0;
		int d = 0, dm = 0;
		rep(i, n) {
			if (s[i] == 'D')d++;
			else if (s[i] == 'M')dm += d;
			else if (s[i] == 'C')ans += dm;
			if (i >= k - 1 && s[i - k + 1] == 'D') {
				d--;
				dm -= summ[i] - (i >= k ? summ[i - k] : 0);
			}
		}
		cout << ans << endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF = 1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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; }

int n;string s;

ll solve(int k){
    ll d=0,m=0,dm=0,ret=0;
    rep(i,k){
        if(s[i]=='D')d++;
        if(s[i]=='M'){
            dm+=d;
            m++;
        }
        if(s[i]=='C')ret+=dm;
    }
    for(int i=k;i<n;i++){
        if(s[i-k]=='D'){
            dm-=m;d--;
        }
        if(s[i-k]=='M')m--;
        
        if(s[i]=='D') d++;
        if(s[i]=='M'){
            dm+=d;
            m++;
        }
        if(s[i]=='C')ret+=dm;
    }
    return ret;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    cin>>n;
    cin>>s;

    vector<ll> ans;
    int q;cin>>q;
    while(q--){
        int k;cin>>k;
        ans.push_back(solve(k));
    }
    for(auto x:ans){
        cout<<x<<endl;
    }
    return 0;
}
