#include <bits/stdc++.h>
using namespace std;

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

    for(int i=0; i<q; i++){
        int k;
        cin >> k;

        long long int d = 0, m = 0, dm = 0, ans = 0;
        for(int i=0; i<n; i++){
            if(s[i] == 'D') d += 1;
            if(s[i] == 'M'){ m += 1; dm += d; }

            if(i >= k){
                if(s[i-k] == 'D'){ d -= 1; dm -= m; }
                if(s[i-k] == 'M') m -= 1;
            }

            if(s[i] == 'C') ans += dm;
        }
        cout << ans << endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for(int i=0; i<(a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template<typename Head> void dump(Head&& head) { cerr << head; dump(); }
template<typename Head, typename... Tail> void dump(Head&& head, Tail&&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do {cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template<typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template<typename T>
using Edges = vector<edge<T>>;
template<typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template<typename T>
using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////
const ll inf = 1LL<<60;

int main()
{
  ll n,k; cin>>n>>k;
  vector<ll> a(k);
  rep(i, k) cin>>a[i];
  n--;

  while (true) {
    n++;
    string s = to_string(n);
    bool flg = true;
    for (auto c:s) {
      rep(i, k) if (c-'0' == a[i]) flg = false;
    }
    if (flg) {
      cout << n << endl;
      return 0;
    }
  }

  return 0;
}
