#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;


unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

class UnionFind {
public:
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

int main() {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    int q;
    cin >> q;
    vector<ll> m(n+1);
    for (int j = 0; j < n; j++) {
        m[j + 1] = m[j];
        if (s[j] == 'M')m[j + 1]++;
    }
    vector<ll> c(n + 1);
    for (int j = 0; j < n; j++) {
        c[j + 1] = c[j];
        if (s[j] == 'C')c[j + 1]++;
    }
    vector<ll> crui(n + 1);
    for (int j = 0; j < n; j++) {
        crui[j + 1] = crui[j];
        if (s[j] == 'C')crui[j + 1] += m[j];
    }
    for (int i = 0; i < q; i++) {
        ll v;
        cin >> v;
        ll ans = 0;
        for (int j = 0; j < n; j++) {
            if (s[j] == 'D')ans += crui[min(n, j + v)]-crui[j]-m[j]*(c[min(n, j + v)]-c[j]);
        }
        cout << ans << endl;
    }

}
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cmath>
using namespace std;
#define REP(i, limit) for(int i=0; i< limit; ++i)
#define FOR(i, j, limit) for(int i=j; i< limit; ++i)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ECHO(x) cout<<x<<endl;
#define MEMSET(a, n, x) REP(i, n) a[i]=x;
#define ARRIN(a, n) REP(i, n) cin>>a[i];
#define LL long long
int INF = 2147483647;
LL LINF = 9223372036854775807;
LL MOD = 1000000007;
typedef pair<int, int>P;
typedef pair<LL, LL>PL;
typedef vector<int> vi;
typedef vector<LL> vl;

class unionfind{
  public:
  int Num;
  vector<int> par; //親の番号
  vector<int> height; //木の高さ(ランク)

  unionfind(int N):
    par(vector<int>(N, 0)), height(vector<int>(N, 0)), Num(N) {}


  //初期化
  void init(){
    REP(i, Num){
      par[i] = i;
      height[i] = 0;
    }
  }

  //xの親番号を返す、再帰処理あり
  int root(int x){
    return par[x] == x ? x : par[x] = root(par[x]);
  }

  //xとyの親番号が同一か判定する
  bool same(int x, int y){
    return root(x) == root(y);
  }

  //xとyのある木を結合する
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if(x == y) return; //親が同一なら結合済み。

    if(height[x] < height[y]) par[x] = y;
    else {
      par[y] = x;
      if(height[x] == height[y]) height[x]++;
    }
  }
};

int main(){
  int n, k, l;
  cin>>n>>k>>l;
  unionfind ro(n);
  unionfind ra(n);
  map<P, LL> mp;
  ro.init();
  ra.init();
  LL ans, r;
  int t1, t2;
  REP(i, k){
    cin>>t1>>t2;
    --t1;
    --t2;
    ro.unite(t1, t2);
  }

  REP(i, l){
    cin>>t1>>t2;
    --t1;
    --t2;
    ra.unite(t1, t2);
  }

  REP(i, n){
    ++mp[make_pair(ro.root(i), ra.root(i))];
  }
  REP(i, n){
    r=mp[make_pair(ro.root(i), ra.root(i))];
    ECHO(r);
  }
  return 0;
}
