#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
const ll MOD = 1e9+7;

int main(){
  ll n,q;
  string s;
  cin>>n>>s>>q;
  for(int i=0;i<=n;i++)s+='.';
  for(int cn=0;cn<q;cn++){
	ll k,ans=0;
	cin>>k;
	ll cntc=0,sumn=0,cntm=0;
	for(int i=0;i<k;i++){
	  if(s[i]=='M')cntm++;
	  if(s[i]=='C'){
		sumn+=cntm;
		cntc++;
	  }
	}
	for(int i=0;s[i]!='.';i++){
	  if(s[i]=='D')ans+=sumn;
	  if(s[i]=='M'){
		cntm--;
		sumn-=cntc;
	  }
	  if(s[i]=='C'){
		cntc--;
	  }
	  if(s[i+k]=='M')cntm++;
	  if(s[i+k]=='C'){
		cntc++;
		sumn+=cntm;
	  }
	}
	cout<<ans<<endl;
  }
  return 0;
}
