We need to solve problem: given n, k, and string s of length n, find lexicographically smallest beautiful string t of length n, t >= s, where beautiful means each letter (from 'a' to 'z') count is divisible by k. If not exist, output -1.
Constraints: T up to 10000, sum n <= 100000.
We need to output answer per test.
We need to find lexicographically smallest t >= s such that length n, and for each character c in 'a'..'z', count_c(t) % k == 0.
We can think of constructing t from left to right, trying to match s as much as possible and then try to adjust at some position where we can increase a character to something larger, then fill the rest with minimal possible characters (subject to divisibility constraints). However, careful: we need to ensure overall counts divisible by k. So it's a typical "next permutation with constraints" problem.
Approach: We'll treat as: we want to find the lexicographically smallest t >= s, so we can iterate from position i from n-1 down to 0, where we try to change the character at i to a larger one, while leaving positions 0..i-1 unchanged (same as s). Then, for suffix i+1..n-1 we can fill with the smallest possible characters to meet the divisibility constraints. If such filling is possible, we've found answer. If not, we continue to earlier position. If none works, answer -1.
We need to efficiently check feasibility given prefix constraints.
Let's define for each prefix of length i (0-indexed) after we decide to keep s[0..i-1] unchanged and set s[i] to something >= original, we have fixed counts for the first i+1 characters (since we set a specific character at position i). Then the suffix of length n - (i+1) can be chosen arbitrarily. We need to see if we can assign characters to the remaining positions such that total counts are divisible by k. Also we want the smallest lexicographically overall, meaning we should choose at each step the smallest possible character at i (>= original if we are changing) that allows a feasible suffix, and then fill suffix with smallest possible characters subject to counts being multiples of k.
Thus algorithm for each test case:
Preprocess: freq[26] counts of s? Not needed. We'll work iteratively.
We'll compute prefix counts of s: pref[i][c]? But n up to 1e5, sum over test cases 1e5, we can store prefix counts as arrays of length n+1 each of 26 ints. That's 26 * 1e5 = 2.6e6 ints, which is okay. But careful: T up to 10000, but sum n = 1e5, so overall memory usage is okay if we allocate per test case. However we may want to avoid per-test O(26*n) if we can compute incremental. But we need to compute many prefix counts as we try positions. However we can compute prefix counts for s initially. But when we modify char at i, the prefix up to i will be same as s's prefix for indices 0..i-1, and at i we change char, so counts differ. We need to recompute counts for each try? But number of tries is at most n per test case (in worst case we try each position), but n can be 1e5 per test case, and if we do O(26) per try, total O(26n) = 2.6e6, which is okay given sum n 1e5. Actually sum of n over test cases is 1e5, so overall O(26 * sum n) is fine. But we might try for each i from n-1 down to 0, and for each i we might try several candidate characters (from s[i]+1 to 'z'), but that could be up to 25. However we can break early once we find a feasible candidate. In worst case, we might have to try many characters per position, but we can prune by using the fact that we want smallest candidate; so we can iterate candidate char from s[i]+1 (or from s[i] if we consider keeping original? But we need t >= s, so if we keep original and suffix is feasible, that's candidate. Actually we need to check if s itself is beautiful. If it is, answer is s. So first check if s itself beautiful; if yes, output s.
If not, then we need t > s. So we need to find some position where we increase a character, and suffix fill minimal. So we can start by checking if there exists any t > s. So algorithm: Check s beautiful? if yes, answer s, done.
Otherwise, we need to find the next one.
We can iterate i from n-1 down to 0. At each i, we consider changing the character at i to some c > s[i] (c from s[i]+1 to 'z'). For each candidate c, we compute new prefix counts: for prefix of length i (positions 0..i-1) we use counts from s (pref[i] from s), then add c at position i. Then we have some remaining positions: rem = n - i - 1.
We need to see if we can fill these rem positions with letters to achieve that total count (including prefix) for each letter is divisible by k. Let current counts for each letter after prefix (including chosen c) be cur[0..25]. For each letter, let needed = (k - cur[letter] % k) % k; that's the additional number needed to make it a multiple of k (i.e., how many more occurrences we must add in the suffix). However, note that we can also exceed by multiples of k? Actually total count must be multiple of k. If we add some characters in suffix, we can only increase counts. So after we add suffix, the final count for each letter will be cur[letter] + add[letter]. We require that (cur[letter] + add[letter]) % k == 0.