{"problem":{"name":"My Last ABC Problem","description":{"content":"Consider a string $t$ consisting only of characters `A`, `B`, and `C`. We can do the following operation with it: *   Choose any substring $t[l:r]$ and any permutation $(X, Y, Z)$ of characters $($`A","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"agc059_a"},"statements":[{"statement_type":"Markdown","content":"Consider a string $t$ consisting only of characters `A`, `B`, and `C`. We can do the following operation with it:\n\n*   Choose any substring $t[l:r]$ and any permutation $(X, Y, Z)$ of characters $($`A`$,$`B`$,$`C`$)$. Here, $t[l:r]$ denotes the substring formed by the $l$\\-th through the $r$\\-th characters of $t$, where $l$ and $r$ are of your choice. Then, replace each character `A`, `B`, and `C` in $t[l:r]$ by $X$, $Y$, and $Z$, respectively.\n\nFor example, for a string $t = $ `ACBAAC`, we can choose a substring $t[3:6]$ and $(X,Y,Z)=($`C`$,$`B`$,$`A`$)$. After this operation, the string will become `ACBCCA`.\nAlina likes strings in which all characters are the same. She defines the beauty of a string $t$ as the minimum number of operations required to make all its characters equal.\nYou are given a string $S$ of length $N$ consisting only of characters `A`, `B`, and `C`. Answer $Q$ queries. The $i$\\-th query is the following:\n\n*   Given integers $L_i$ and $R_i$, find the beauty of the substring $t=S[L_i:R_i]$.\n\n## Constraints\n\n*   $1 \\le N \\le 10^5$\n*   $S$ is a string of length $N$ consisting only of characters `A`, `B`, and `C`.\n*   $1 \\le Q \\le 10^5$\n*   $1 \\le L_i \\le R_i \\le N$\n*   All numbers in the input are integers.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$N$ $Q$\n$S$\n$L_1$ $R_1$\n$L_2$ $R_2$\n$\\vdots$\n$L_Q$ $R_Q$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"agc059_a","tags":[],"sample_group":[["6 4\nABCCCA\n3 5\n2 3\n1 3\n1 6","0\n1\n2\n2\n\nIn the first query, the string is $t = $ `CCC`, in which all letters are already equal. The answer is $0$.\nIn the second query, the string is $t = $ `BC`. We can change it to `BB` in one operation, by choosing a substring $t[2:2]$ and $(X,Y,Z)=($`A`$,$`C`$,$`B`$)$.\nIn the third query, the string is $t = $ `ABC`. We can change it to `AAB` in one operation, by choosing a substring $t[2:3]$ and $(X,Y,Z)=($`C`$,$`A`$,$`B`$)$, and then to `BBB` in the next operation, by choosing a substring $t[1:2]$ and $(X,Y,Z)=($`B`$,$`A`$,$`C`$)$."]],"created_at":"2026-03-03 11:01:14"}}