(* O(n) *)
let rec fold_btw f a i n = if i >= n then a else fold_btw f (f a i) (i + 1) n
let rec fold_btwe f a i n = fold_btw f a i (n + 1)
let fold_int f a n = fold_btw f a 0 n

(* O(|s|) *)
let get_left_info s =
  let n = String.length s in
  let left = Array.make (n + 1) 0 in
  fold_btwe
    (fun len i ->
      left.(i) <- left.(i - 1);
      if "AC".[len] = s.[i - 1] then
        (if len = 1 then
          (left.(i) <- left.(i) + 1;
          0)
        else
          len + 1)
      else
        0)
    0
    1
    n |> ignore;
  left

(* O(max {n, q}) *)
let solve n q s lrs =
  let left = get_left_info s in
  List.iter
    (fun (l, r) ->
      let count = left.(r) - left.(l) in (* left.(l) = l + |"AC"| - 2 *)
      string_of_int count |> print_endline)
    lrs;
  left

let _ = Scanf.scanf " %d %d %s" (fun n q s ->
  fold_int
    (fun acc _ -> Scanf.scanf " %d %d" (fun l r ->
      (l, r) :: acc))
    []
    q |> List.rev |> solve n q s)