{"problem":{"name":"D. Counting Test","description":{"content":"Yousef has a string s that is used to build a magical string w by repeating the string s infinitely many times. For example, if s = aabbb , then w = aabbbaabbbaabbbaabbb.... Mohammad always claims th","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF10153D"},"statements":[{"statement_type":"Markdown","content":"Yousef has a string s that is used to build a magical string w by repeating the string s infinitely many times. For example, if s = aabbb , then w = aabbbaabbbaabbbaabbb....\n\nMohammad always claims that his memory is strong, and that his ability to count is very high, so Yousef decided to hold a test for Mohammad, in order to know the truth of his claims.\n\nYousef will give Mohammad q queries, such that each query consisting of two integers l and r, and a lowercase English letter c. The answer of each query is how many times the letter c appears between the lth and rth letters in *string w*.\n\nMohammad must answer all the queries correctly, in order to proof his claims, but currently he is busy finishing his meal. Can you help Mohammad by answering all queries?\n\nThe first line contains an integer T, where T is the number of test cases.\n\nThe first line of each test case contains two integers n and q (1 ≤ n ≤ 104) (1 ≤ q ≤ 105), where n is the length of string s, and q is the number of queries.\n\nThe second line of each test case contains the string s of length n consisting only of lowercase English letters.\n\nThen q lines follow, each line contains two integers l and r and a lowercase English letter c (1 ≤ l ≤ r ≤ 109), giving the queries.\n\nFor each query, print a single line containing how many times the letter c appears between the lth and rth letters in *string w*.\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use _scanf/printf_ instead of _cin/cout_ in C++, prefer to use _BufferedReader/PrintWriter_ instead of _Scanner/System.out_ in Java.\n\n## Input\n\nThe first line contains an integer T, where T is the number of test cases.The first line of each test case contains two integers n and q (1 ≤ n ≤ 104) (1 ≤ q ≤ 105), where n is the length of string s, and q is the number of queries.The second line of each test case contains the string s of length n consisting only of lowercase English letters.Then q lines follow, each line contains two integers l and r and a lowercase English letter c (1 ≤ l ≤ r ≤ 109), giving the queries.\n\n## Output\n\nFor each query, print a single line containing how many times the letter c appears between the lth and rth letters in *string w*.\n\n[samples]\n\n## Note\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use _scanf/printf_ instead of _cin/cout_ in C++, prefer to use _BufferedReader/PrintWriter_ instead of _Scanner/System.out_ in Java.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ T \\in \\mathbb{Z}^+ $ be the number of test cases.  \nFor each test case:  \n- Let $ n \\in \\mathbb{Z}^+ $ be the length of the base string $ s $.  \n- Let $ s = s_1 s_2 \\dots s_n \\in \\Sigma^n $, where $ \\Sigma $ is the set of lowercase English letters.  \n- Let $ w $ be the infinite string defined by $ w = s^\\infty $, i.e., $ w_i = s_{(i-1) \\bmod n + 1} $ for all $ i \\geq 1 $.  \n- Let $ q \\in \\mathbb{Z}^+ $ be the number of queries.  \n\n**Constraints**  \n1. $ 1 \\leq T \\leq \\text{unknown (implied by input)} $  \n2. For each test case:  \n   - $ 1 \\leq n \\leq 10^4 $  \n   - $ 1 \\leq q \\leq 10^5 $  \n   - $ 1 \\leq l \\leq r \\leq 10^9 $  \n\n**Objective**  \nFor each query $ (l, r, c) $, compute:  \n$$  \nf(l, r, c) = \\sum_{i=l}^{r} \\mathbf{1}_{w_i = c}  \n$$  \nwhere $ \\mathbf{1}_{w_i = c} = 1 $ if $ w_i = c $, and $ 0 $ otherwise.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10153D","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}