#include "bits/stdc++.h"
using namespace std;
long long a[2][1000001];
char b[1000001];
int main()
{
	int n, q;
	scanf("%d\n", &n);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%c", &b[i]);
		switch(b[i])
		{
			case 'D':
				a[0][i] = 1;
				break;
			case 'M':
				a[1][i] = 1;
				break;
		}
		for(int j = 0; j < 2; ++j) 
		{
			a[j][i] += a[j][i - 1]; 
		}
	}
	scanf("%d", &q);
	for(int i = 0, j; i < q; ++i)
	{
		scanf("%d", &j);
		long long total = 0, dm = 0;
		for(int k = 1; k <= n; k++)
		{
			if(k - j >= 1) 
			{
				if(b[k - j] == 'D')
				{
					dm -= a[1][k - 1] - a[1][k - j - 1];
				}
			}
			switch(b[k])
			{
				case 'M':
					dm += a[0][k] - a[0][max(0, k - j)]; 
					break;
				case 'C':
					total += dm;
					break;
			}
		}
		printf("%lld\n", total);
	}
}