#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
void solve() {
int n = 0;
std::string str;
std::cin >> n >> str;
std::vector<std::pair<int, char>> map;
for (char chr = 'a'; chr <= 'z'; ++chr) {
map.push_back({0, chr});
}
for (int i = 0; i < n; ++i) {
++map[str[i] - 'a'].first;
}
std::sort(map.rbegin(), map.rend());
int answer = -1;
int letter_count = -1;
for (int l_cnt = 1; l_cnt <= 26; ++l_cnt) {
if (n % l_cnt)
continue;
int freq = n / l_cnt;
int new_answer = 0;
for (int i = 0; i < l_cnt; ++i) {
if (map[i].first < freq)
new_answer += freq - map[i].first;
}
if (answer == -1 || new_answer < answer) {
answer = new_answer;
letter_count = l_cnt;
}
}
std::cout << answer << "\n";
int freq = n / letter_count;
std::map<char, int> fixed;
std::queue<char> q;
for (int i = 0; i < letter_count; ++i) {
fixed[map[i].second] = map[i].first;
if (map[i].first < freq) {
for (int j = 0; j < freq - map[i].first; ++j) {
q.push(map[i].second);
}
}
}
for (int i = 0; i < n; ++i) {
if (!fixed.count(str[i]) || fixed[str[i]] > freq) {
if (fixed.count(str[i]))
--fixed[str[i]];
str[i] = q.front();
q.pop();
}
}
std::cout << str << "\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
#ifndef ONLINE_JUDGE
std::freopen("input.txt", "r", stdin);
#endif
int tests = 0;
std::cin >> tests;
while (tests--)
solve();
return 0;
}
115A - Party | 746B - Decoding |
1424G - Years | 1663A - Who Tested |
1073B - Vasya and Books | 195B - After Training |
455A - Boredom | 1099A - Snowball |
1651D - Nearest Excluded Points | 599A - Patrick and Shopping |
237A - Free Cash | 1615B - And It's Non-Zero |
1619E - MEX and Increments | 34B - Sale |
1436A - Reorder | 1363C - Game On Leaves |
1373C - Pluses and Minuses | 1173B - Nauuo and Chess |
318B - Strings of Power | 1625A - Ancient Civilization |
864A - Fair Game | 1663B - Mike's Sequence |
448A - Rewards | 1622A - Construct a Rectangle |
1620A - Equal or Not Equal | 1517A - Sum of 2050 |
620A - Professor GukiZ's Robot | 1342A - Road To Zero |
1520A - Do Not Be Distracted | 352A - Jeff and Digits |