#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);

  int n, q;
  string s;
  cin >> n >> s >> q;

  while (q--) {
    int k;
    cin >> k;

    ll ans = 0, a = 0, b = 0, m = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'D') a++;
      else if (s[i] == 'M') {
        m++;
        b += a;
      } else if (s[i] == 'C') ans += b;
    }

    for (int i = 1; i <= n - k; i++) {
      if (s[i - 1] == 'D') {
        a--;
        b -= m;
      } else if (s[i - 1] == 'M') m--;

      if (s[i + k - 1] == 'D') a++;
      else if (s[i + k - 1] == 'M') {
        m++;
        b += a;
      } else if (s[i + k - 1] == 'C') ans += b;
    }

    cout << ans << '\n';
  }
}

#include <iostream>
#include <vector>
#include <algorithm>

class Baseball {
private:
	int out;
	int runner;
	int score;

public:
	//constructor
	Baseball() {
		out = 0; 
		runner = 0;
		score = 0;
	}

	//getter
	int getOut() { return out; }
	int getRunner() { return runner; }
	int getScore() { return score; }
	//input OUT
	void execOut() { ++out; }
	//input HIT
	void execHit() {
		if(runner == 3)++score;
		else ++runner;
	}
	//input HOMERUN
	void execHomerun() {
		score += runner + 1;
		runner = 0;
	}

};

int main(){
	int dataSetNum;
	std::cin >> dataSetNum;

	std::vector< Baseball > baseball;
	baseball.resize(dataSetNum);

	for(int i = 0; i < dataSetNum; ++i){

		while(baseball[i].getOut() != 3){
			//input
			std::string tmp;
			std::cin >> tmp;

			if(tmp == "OUT")baseball[i].execOut();
			else if(tmp == "HIT")baseball[i].execHit();
			else if(tmp == "HOMERUN")baseball[i].execHomerun();
		}

	}
	//output
	std::for_each(baseball.begin(), baseball.end(),
    [](Baseball x) { std::cout << x.getScore() << std::endl; } );

	return 0;
}