import java.io.BufferedReader
import java.io.InputStreamReader

class C

fun main( args: Array<String> ) {
    val st = BufferedReader( InputStreamReader( System.`in` ) )
    val n = st.nextInt()
    val s = st.readLine()
    val q = st.nextInt()
    val len = run {
        val list = st.nextInts().toMutableList()
        list.add( 3 )
        return@run list.toIntArray()
    }
    val p = Array( len.size, { it } )
    p.sortBy { len[it] }
    val r = LongArray( len.size )
    r[p[0]] = run {
        var cnt = 0L
        for ( i in 0 .. s.length - 3 ) {
            if ( s[i] == 'D' && s[i + 1] == 'M' && s[i + 2] == 'C' ) {
                cnt ++
            }
        }
        return@run cnt
    }
    val mCount = run {
        val tmp = LongArray( n + 1 )
        for ( i in s.indices ) {
            tmp[i + 1] = tmp[i] + if ( s[i] == 'M' ) 1 else 0
        }
        return@run tmp
    }
    val cCount = run {
        val tmp = LongArray( n + 1 )
        for ( i in s.indices ) {
            tmp[i + 1] = tmp[i] + if ( s[i] == 'C' ) 1 else 0
        }
        return@run tmp
    }

    for ( i in 1 until p.size ) {
        r[p[i]] = r[p[i - 1]]
        if ( len[p[i]] == len[p[i - 1]] ) {
            continue
        }

        val pairs = LongArray( s.length + 1 )
        run {
            val l = len[p[i]] - len[p[i - 1]]
            var mc = 0L
            var cc = 0L
            for ( j in s.indices.reversed() ) {
                if ( j < s.lastIndex ) pairs[j] = pairs[j + 1]
                if ( j + l < s.length ) {
                    if ( s[j + l] == 'C' ) {
                        pairs[j] -= mc
                        cc --
                    }
                    if ( s[j + l] == 'M' ) {
                        mc --
                    }
                }
                if ( s[j] == 'M' ) {
                    pairs[j] += cc
                    mc ++
                }
                if ( s[j] == 'C' ) {
                    cc ++
                }
            }
        }

        for ( j in 0 .. s.length - len[p[i - 1]] ) {
            if ( s[j] != 'D' ) continue
            r[p[i]] += ( mCount[j + len[p[i - 1]]] - mCount[j] ) * ( cCount[Math.min( cCount.lastIndex, j + len[p[i]] )] - cCount[j + len[p[i - 1]]] )
            r[p[i]] += pairs[j + len[p[i - 1]]]
        }
    }
    for ( i in 0 until q ) {
        println( r[i] )
    }
}

private fun BufferedReader.nextInt(): Int {
    return readLine().toInt()
}

private fun BufferedReader.nextInts(): IntArray {
    val s = readLine().split( " " )
    return IntArray( s.size, { s[it].toInt() } )
}
