#define DEBUG 0
#include <bits/stdc++.h>
#define loop(n) for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto&& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto&& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#if DEBUG
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template <class Key>
class fmt_t {
    Key key;

    template <class Value>
    auto is_exact_match(long, Value) const {
      return false;
    }

    template <class Value, class = decltype(std::declval< Value >() == key)>
    auto is_exact_match(int, Value x) const {
      return x == key;
    }


    // tuple_format_impl
    template < class Tuple,  std::size_t ... Is >
    std::string tuple_format_impl(const Tuple& tuple, std::integer_sequence< std::size_t, Is ... >) const {
      std::string ret = "(";
      (void)std::initializer_list< std::nullptr_t >
        { (ret = ret + (Is == 0 ? "" : ",") + format(std::get< Is >(tuple)), nullptr) ... };
      return ret + ")";
    }

    // usual_format
    template < class Value >
    std::string usual_format(long, Value x) const
      { return std::to_string(x); }

    template < class Value, std::enable_if_t< std::is_same< std::decay_t< Value >, char >::value, std::nullptr_t > = nullptr >
    std::string usual_format(int, Value c) const
      { return std::string{'\''} + std::string{c} + std::string{'\''}; }

    std::string usual_format(int, char const* s) const
      { return "\"" + std::string(s) + "\""; }

    std::string usual_format(int, std::string const s) const
      { return "\"" + s + "\""; }

    template < class Container, class = typename Container::value_type >
    std::string usual_format(int, Container const & v) const {
      return "{" + std::accumulate(v.begin(), v .end(), std::string{},
        [this](auto const& s, auto const x) { return s + (s.empty() ? "" : ",") + this->format(x); })
        + "}";
    }

    template < class Tuple, std::size_t N = std::tuple_size< Tuple >::value >
    std::string usual_format(int, Tuple const& tuple) const
      { return tuple_format_impl(tuple, std::make_index_sequence< N >()); }

    // format
    template < class Value > std::string format(Value&& x) const
      { return is_exact_match(int{}, x) ? std::string{'_'} : usual_format(int{}, x); }

  public:
    fmt_t()=default;
    fmt_t(Key&& key) : key(key){}

    template < class Value > std::string operator()(Value&& x) const
      { return format(x); }
};
template <class Key> auto fmt(Key key)
  { return fmt_t<Key>(std::forward<Key>(key)); }

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n; std::cin >> n;
  std::vector<int> a(n);
  rep(i,0,n) {
    char c; std::cin >> c;
    a.at(i) = c=='D' ? 0 : c=='M' ? 1 : c=='C' ? 2 : -1;
  }
  debug(fmt(-1)(a));

  int q; std::cin >> q;
  loop(q) {
    int k; std::cin >> k;
    int l = 0, r = 0;
    lint x = 0, y = 0;
    lint now = 0;
    lint ans = 0;

    auto pop_left = [&] {
      assert(r - l == k);
      int c = a.at(l);
      if (c==0) {
        now -= y;
      }
      x -= c==0;
      y -= c==1;
      l++;
    };
    auto push_right = [&] {
      assert(r - l < k);
      int c = a.at(r);
      if (c==1) {
        now += x;
      }
      x += c==0;
      y += c==1;
      r++;
    };
    auto check = [&] {
      debug(l,r);
      assert(l < r);
      int c = a.at(r-1);
      if (c==2) {
        ans += now;
      }
    };

    while (r < k) {
      push_right();
      check();
    }
    assert(l == 0 && r == k);
    while (r < n) {
      pop_left();
      push_right();
      check();
    }
    assert(l == n - k && r == n);
    std::cout << ans << std::endl;
  }
  return 0;
}

/*
右端を全探索です。
右端が 2 になるたびに、今見ている区間の 0 と 1 のペアの個数だけスコアに加算です。
左を pop では、0 を pop したら入っている 1 の数の分だけペアが減少します。
右に push では、1 を push したら入っている 0 の数の分だけペアが増加します。
*/

Measured memory_bytes: 4224