#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <limits.h>
using namespace std;
typedef long long LL;
const int N=1<<20;

int n,q,k;
char s[N];
LL sa[N],sb[N],sc[N];

int main() {
	scanf("%d %s %d",&n,s+1,&q);
	for(int i=n;i>0;i--) {
		sa[i]=sa[i+1]+(s[i]=='M');
		sb[i]=sb[i+1]+(s[i]=='C');
		sc[i]=sc[i+1]+(s[i]=='M')*sb[i];
	}
	while(q--) {
		scanf("%d",&k);
		LL ans=0;
		for(int i=1;i<=n;i++) {
			if(s[i]!='D') continue;
			ans+=sc[i]-sc[min(n+1,i+k)];
			ans-=(sa[i]-sa[min(i+k,n+1)])*sb[min(i+k,n+1)];
			//printf("i=%d sc=%lld sa=%lld sb=%lld ans=%lld\n",i,sc[i],sa[i]-sa[i+k],sb[i+k],ans);
		}
		printf("%lld\n",ans);
	}
}