import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

struct SegTree(alias fun, alias E, T)
if (is(typeof(E) : T))
{
    import std.functional : binaryFun;
    alias OP = binaryFun!fun;

    ///
    this(size_t n, T[] ts) {
        this.n = 1;
        while (this.n < n) this.n *= 2;
        this.tree.length = this.n * 2 - 1;
        foreach (ref e; this.tree) e = E;
        foreach (i, e; ts) this.update(i, e);
    }

    ///
    void replace(size_t i, T e) {
        i += this.n - 1;
        this.tree[i] = e;
        while (i > 0) {
            i = (i-1) / 2;
            this.tree[i] = OP(this.tree[i*2+1], this.tree[i*2+2]);
        }
    }

    ///
    void update(size_t i, T e) {
        replace(i, OP(e, tree[i + this.n - 1]));
    }

    ///
    T query(size_t a, size_t b) {
        T impl(size_t i, size_t l, size_t r) {
            if (r <= a || b <= l) return E;
            if (a <= l && r <= b) return this.tree[i];
            return OP(
                impl(i*2+1, l, (l+r)/2),
                impl(i*2+2, (l+r)/2, r)
            );
        }
        return impl(0, 0, this.n);
    }

private:
    size_t n;
    T[] tree;
}

///
auto seg_tree(alias f, alias E, T)(size_t n, T[] arr = [])
{
    return SegTree!(f, E, T)(n, arr);
}

///
auto seg_tree(alias f, alias E, T)(T[] arr)
{
    return SegTree!(f, E, T)(arr.length, arr);
}

auto sum_seg_tree(T)(size_t n, T[] arr = [])
{
    return seg_tree!("a + b", 0, T)(n, arr);
}

auto sum_seg_tree(T)(T[] arr)
{
    return sum_seg_tree!T(arr.length, arr);
}

void main()
{
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto Q = readln.chomp.to!int;
    auto ks = readln.split.to!(int[]);

    auto ds = new long[](N);
    auto ms = sum_seg_tree!long(N);
    auto cs = sum_seg_tree!long(N);
    size_t[] cis;
    long m, c;
    foreach (i, s; S) {
        if (s == 'M') ++m;
        if (s == 'D') {
            ds[i] = m;
        }
        if (s == 'C') {
            ++c;
            ms.update(i, m);
            cs.update(i, 1);
            cis ~= i;
        }
    }

    foreach (k; ks) {
        long res;
        size_t ci;
        foreach (i, s; S) if (s == 'D') {
            while (ci < cis.length && cis[ci] < i) ++ci;
            if (ci == cis.length) break;
            if (cis[ci] - i >= k) continue;
            size_t l = ci, r = cis.length-1;
            if (cis[$-1] - i < k) {
                l = r;
            } else {
                while (l+1 < r) {
                    m = (l+r)/2;
                    if (cis[m] - i >= k) {
                        r = m;
                    } else {
                        l = m;
                    }
                }
            }
            auto msum = ms.query(i, cis[l]+1);
            auto ccount = cs.query(i, cis[l]+1);
            auto prem = ds[i];
            res += msum - prem * ccount;
        }
        writeln(res);
    }
}