#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
	int num;
	scanf("%d", &num);
	string s;
	cin >> s;
	int query;
	scanf("%d", &query);
	for (int p = 0; p < query; p++)
	{
		int z;
		scanf("%d", &z);
		ll cnt = 0, cd = 0, cm = 0, ans = 0;
		for (int i = 0; i < num; i++)
		{
			if (s[i] == 'D')cd++;
			else if (s[i] == 'M')cnt += cd, cm++;
			else if (s[i] == 'C')ans += cnt;
			if (i >= z - 1)
			{
				if (s[i - z + 1] == 'D')cnt -= cm, cd--;
				else if (s[i - z + 1] == 'M')cm--;
			}
		}
		printf("%lld\n", ans);
	}
}