#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let s = read::<String>().chars().collect::<Vec<_>>();
    let q = read::<usize>();
    let ks = read_vec::<usize>();

    let mut accum_m = vec![0; n];
    if s[0] == 'M' {
        accum_m[0] = 1;
    }
    for i in 1..n {
        accum_m[i] = accum_m[i - 1];
        if s[i] == 'M' {
            accum_m[i] += 1;
        }
    }
    let mut accum_mc = vec![0i64; n];
    for i in 1..n {
        accum_mc[i] = accum_mc[i - 1];
        if s[i] == 'C' {
            accum_mc[i] += accum_m[i];
        }
    }

    let mut d_poses = vec![];
    let mut c_poses = vec![];
    for i in 0..n {
        if s[i] == 'C' {
            c_poses.push(i);
        } else if s[i] == 'D' {
            d_poses.push(i);
        }
    }

    let mut most_left_memo = vec![0; d_poses.len()];
    for (i, &d_pos) in d_poses.iter().enumerate() {
        most_left_memo[i] = c_poses.lower_bound(&d_pos);
    }

    for k in ks {
        let mut ans = 0i64;
        for (i, &d_pos) in d_poses.iter().enumerate() {
            let most_right_c_pos = min(n - 1, d_pos + k - 1);
            let most_left_c_idx = most_left_memo[i];
            let most_right_c_idx = c_poses.upper_bound(&(d_pos + k - 1));
            let already_consumed = (most_right_c_idx - most_left_c_idx) as i64 * accum_m[d_pos];
            ans += accum_mc[most_right_c_pos] - accum_mc[d_pos] - already_consumed;
        }
        println!("{}", ans);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

use std::cmp::Ordering;

pub trait BinarySearch<T> {
    fn lower_bound(&self, x: &T) -> usize;
    fn upper_bound(&self, x: &T) -> usize;
}

impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }

    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}


Measured memory_bytes: 41068