program k_dmc
  implicit none
  integer :: n, q, k(75), i, j
  integer(8) :: d, m, dm, dmc
  character(1000000) :: s
  k = 0
  read(*,*) n
  read(*,*) s
  read(*,*) q
  read(*,*) k(1:q)
  s = trim(adjustl(s))
  do j = 1, q
    d = 0_8
    m = 0_8
    dm = 0_8
    dmc = 0_8
    do i = 1, k(j)-1
      select case(s(i:i))
      case("D")
        d = d+1_8
      case("M")
        m = m+1_8
        dm = dm+d
      case("C")
        dmc = dmc+dm
      end select
    end do
    do i = k(j), n
      select case(s(i:i))
      case("D")
        d = d+1_8
      case("M")
        m = m+1_8
        dm = dm+d
      case("C")
        dmc = dmc+dm
      end select
      select case(s(i-k(j)+1:i-k(j)+1))
      case("D")
        d = d-1_8
        dm = dm-m
      case("M")
        m = m-1_8
      end select
    end do
    write(*,'(i0)') dmc
  end do
  stop
end program k_dmc