#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include<iostream>
#include<unordered_set>

using namespace std;

#define dprint(Exp,...) if(Exp){fprintf(stderr, __VA_ARGS__);}
#define printe(...) fprintf(stderr, __VA_ARGS__);
#define PrtExp(_Exp)  cerr<< #_Exp <<" = "<< (_Exp)
#define PrtExpN(_Exp)  cerr<< #_Exp <<" = "<< (_Exp) <<"\n"

#define SINT(n) scanf("%d",&n)
#define SINT2(n,m) scanf("%d %d",&n,&m)
#define SINT3(n,m,o) scanf("%d %d %d",&n,&m,&o)
#define SINT4(n,m,o,P) scanf("%d %d %d %d",&n,&m,&o,&P)
#define SINT5(n,m,o,P,q) scanf("%d %d %d %d %d",&n,&m,&o,&P,&q)
#define SLL(n) scanf("%lld",&n)
#define SLL2(n,m) scanf("%lld %lld",&n,&m)
#define SLL3(n,m,o) scanf("%lld %lld %lld",&n,&m,&o)
#define SST(s) scanf("%s",s)
#define SCH(c) scanf("%c",&c)

#define GC() getchar()

#define PINT(n) printf("%d",(int)(n))
#define PINT2(n,m) printf("%d %d",(int)(n),(int)(m))
#define PINT3(n,m,l) printf("%d %d %d",(int)(n),(int)(m),(int)(l))
#define PLL(n) printf("%lld",(long long)(n))
#define PST(s) printf("%s",(s))
#define PCH(s) printf("%c",(s))

#define PINTN(n) printf("%d\n",(int)(n))
#define PINT2N(n,m) printf("%d %d\n",(int)(n),(int)(m))
#define PINT3N(n,m,l) printf("%d %d %d\n",(int)(n),(int)(m),(int)(l))
#define PLLN(n) printf("%lld\n",(long long)(n))
#define PSTN(s) printf("%s\n",(s))
#define PCHN(s) printf("%c\n",(s))

#define PSP() printf(" ")
#define PN() printf("\n")

#define PC(c) putchar(c)
#define CSP (' ')
#define SN ("\n")

#define rep(i,a) for(int i=0;i<a;i++)
#define reP(i,a) for(int i=0;i<=a;i++)
#define Rep(i,a) for(int i=a-1;i>=0;i--)
#define ReP(i,a) for(int i=a;i>=0;i--)

#define rEp(i,a) for(i=0;i<a;i++)
#define rEP(i,a) for(i=0;i<=a;i++)
#define REp(i,a) for(i=a-1;i>=0;i--)
#define REP(i,a) for(i=a;i>=0;i--)

#define repft(i,a,b) for(int i=a;i<b;i++)
#define repfT(i,a,b) for(int i=a;i<=b;i++)
#define Repft(i,a,b) for(int i=a-1;i>=b;i--)
#define RepfT(i,a,b) for(int i=a;i>=b;i--)

#define foreach(a,it) for(auto it = a.begin(); it != a.end(); ++it)

#define FILL(a,v) fill(begin(a),end(a), v)
#define FILL0(a) memset(a,0,sizeof(a))
#define FILL1(a) memset(a,-1,sizeof(a))

typedef long long ll;

typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll>   Pll;


const int INF = 1010000000; // 0x3C33'6080
const ll INFLL = 0x1f1f1f1f1f1f1f1fLL;//2,242,545,357,980,376,863

template <class A, class B> inline ostream& operator<<(ostream& st, const pair<A, B>& P) { return st << "(" << P.first << "," << P.second << ")"; };
template <class A, class B> inline pair<A, B> operator+(const pair<A, B>& P, const pair<A, B>& Q) { return pair<A, B>(P.first + Q.first, P.second + Q.second); };
template <class A, class B> inline pair<A, B> operator-(const pair<A, B>& P, const pair<A, B>& Q) { return pair<A, B>(P.first - Q.first, P.second - Q.second); };

#define fs  first
#define sc  second

int bitCount(unsigned int bits) {
	bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);    //  2bitごとに計算
	bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);    //  4bitごとに計算
	bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);    //  8bitごとに計算
	bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);    //  16ビットごとに計算   
	return (bits & 0x0000ffff) + (bits >> 16);    //  32ビット分を計算   
}


template <typename T>
class segment_tree {
public:
	segment_tree(int n_) : n(__lowest_power_of_2(n_)), dat(n * 2, 0), lazy(n * 2, 0) {}

	template <typename Iterator>
	segment_tree(Iterator left, Iterator right)
		: n(__lowest_power_of_2(std::distance(left, right))), dat(n * 2, 0), lazy(n * 2, 0) {
		std::copy(left + n, right + n, dat.begin());
		for (int i = n - 1; i >= 1; --i) {
			dat[i] = std::max(dat[i * 2], dat[i * 2 + 1]);
		}
	}

private:
	int n;
	std::vector<T> dat, lazy;

public:
	void add(int ql, int qr, const T &x) { __add(1, 0, n, ql, qr, x); }

	T sum(int ql, int qr) { return __sum(1, 0, n, ql, qr); }

private:
	void __flush_lazy(int v, int l, int r) {
		dat[v] += lazy[v] * (r - l);
		if (v < n) {
			lazy[v * 2] += lazy[v];
			lazy[v * 2 + 1] += lazy[v];
		}
		lazy[v] = 0;
	}

	void __update_dat(int v) { dat[v] = dat[v * 2] + dat[v * 2 + 1]; }

	void __add(int n, int l, int r, const int ql, const int qr, const T &x) {
		__flush_lazy(n, l, r);
		if (r <= ql || qr <= l) {
			return;
		} else if (ql <= l && r <= qr) {
			lazy[n] += x;
			__flush_lazy(n, l, r);
		} else {
			int m = (l + r) / 2;
			__add(n * 2, l, m, ql, qr, x);
			__add(n * 2 + 1, m, r, ql, qr, x);
			__update_dat(n);
		}
	}

	T __sum(int n, int l, int r, const int ql, const int qr) {
		__flush_lazy(n, l, r);
		if (r <= ql || qr <= l) {
			return 0;
		} else if (ql <= l && r <= qr) {
			return dat[n];
		} else {
			int m = (l + r) / 2;
			T res_left = __sum(n * 2, l, m, ql, qr);
			T res_right = __sum(n * 2 + 1, m, r, ql, qr);
			__update_dat(n);
			return res_left + res_right;
		}
	}

	int __lowest_power_of_2(int x) const {
		int res = 1;
		while (res < x) res <<= 1;
		return res;
	}
};


int K[80];

int M[1000006];


int main() {
	int n;
	string s;
	int q;
	cin >> n;
	cin >> s;
	cin >> q;

	rep(i, q) {
		cin >> K[i];
	}


	FILL0(M);
	M[0] = (s[0] == 'M');
	rep(i, n-1) M[i+1] = M[i] + (s[i+1] == 'M');


	//rep(i, n) printf("%2d", d[i]); puts("");
	//rep(i, n) printf("%2d", c[i]); puts("");

	/*
	rep(i, n) {
		if (s[i] == 'D') { printf("D"); continue; }
		if (s[i] == 'M') { printf("M"); continue; }
		if (s[i] == 'C') { printf("C"); continue; }
		printf(".");
	}puts("");
	
	*/

	rep(Q, q) {
		//segment_tree<ll> seg(n+10);
		int k = K[Q];

		ll ret = 0;
		int j = 0;
		int I = 0;
		int J = 0;

		deque<ll> DQ;
		ll DQsum = 0;
		ll DQsub = 0;
		for (int i = 0; i < n; ++i) {
			//printf("%2d:", i); rep(j, J) printf(" %2d", seg.sum(j, j + 1)); puts("");
			if (s[i] == 'D' || s[i] == 'M' || s[i] == 'C') {
				//printf("%2d:", i); rep(j, J) printf(" %2d", seg.sum(j, j + 1)); puts("");
			}

			if (s[i] == 'D') {
				for (; j < i + k; ++j) {
					if (j >= n) break;
					if (s[j] != 'C') continue;
					//seg.add(J, J + 1, M[j] - M[i]);
					//DQ.push_back(M[j] - M[i]);

					DQsum += M[j] - M[i];
					++J;
				}
				ll r = DQsum - DQsub;
				ret += r;
			}
			if (s[i] == 'M') {
				if (I < J) {
					//seg.add(I, J, -1);
				}
				DQsub += J - I;
			}
			if (s[i] == 'C') {
				++I;
				//DQ.pop_front();
			}
		}
		cout << ret << endl;

	}
}


/*

DWANGOMEDIACLUSTER
D.....M.D..C......

012345678901234567890123456789012345678901234567890123
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
D.......D..............M............C............M...D

*/

Measured memory_bytes: 5252