inline T rd(){ T x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007
inlinevoidwork(){ int w = rd(), d = rd(), h = rd(); int a = rd(), b = rd(), f = rd(), g = rd(); int ans = 1e9; auto calc = [&](int x, int y) { ans = min(ans, h + abs(a - x) + abs(b - y) + abs(f - x) + abs(g - y)); };
for (int i = 0; i <= d; ++i) {calc(0, i); calc(w, i);} for (int i = 0; i <= w; ++i) {calc(i, 0); calc(i, d);}
printf("%d\n", ans); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inline T rd(){ T x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007
int a[N];
inlinevoidwork(){ int n = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); sort(a + 1, a + 1 + n); a[n + 1] = 1e9; int ans = (a[1] > 0); for (int i = 1; i <= n; ++i) { if (i > a[i] && i < a[i + 1]) ++ans; } printf("%d\n", ans); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inline T rd(){ T x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007
char s[N], t[N];
int n, cnt[26], rem[26], id[26];
vector<char> add;
inlineintcalc(int m){ int ret = n; for (int i = 0; i < m; ++i) ret -= min(n / m, cnt[i]); return ret; }
inlinevoidwork(){ n = rd(); scanf("%s", s + 1); for (int i = 0; i < 26; ++i) cnt[i] = rem[i] = 0; for (int i = 1; i <= n; ++i) ++cnt[s[i] - 'a']; for (int i = 0; i < 26; ++i) id[i] = i;
sort(id, id + 26, [&](int a, int b) {return cnt[a] > cnt[b];}); sort(cnt, cnt + 26, [&](int a, int b) {return a > b;}); int ans = 1e9, res = 1; for (int i = 1; i <= 26; ++i) if (n % i == 0) { int ret = calc(i); if (ret < ans) {ans = ret; res = i;} }
for (int i = 0; i < res; ++i) rem[id[i]] = n / res; printf("%d\n", ans); for (int i = 1; i <= n; ++i) { if (rem[s[i] - 'a']) {--rem[s[i] - 'a']; t[i] = s[i];} else t[i] = 0; } add.clear(); for (int i = 0; i < res; ++i) if (rem[id[i]]) for (int j = 1; j <= rem[id[i]]; ++j) add.push_back(id[i] + 'a'); for (int i = 1; i <= n; ++i) if (t[i] == 0) {t[i] = add.back(); add.pop_back();} t[n + 1] = '\0'; puts(t + 1); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inline T rd(){ T x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 57
int a[N], ans;
unordered_map<ll, bool> vis;
inlinevoidwork(){ int n = rd(); ans = 1; vis.clear(); for (int i = 1; i <= n; ++i) a[i] = rd();
auto issqr = [&](ll x) { ll rt = sqrt(x); for (ll i = rt - 2; i <= rt + 2; ++i) if (i * i == x) returntrue; returnfalse; };
auto test = [&](ll x) { int cnt = 0; for (int i = 1; i <= n; ++i) cnt += issqr(a[i] + x); return cnt; };
for (int i = 1; i < n; ++i) for (int j = i + 1; j <= n; ++j) { int x = a[j] - a[i]; int lim = sqrt(x); for (int d = 1; d <= lim; ++d) if (x % d == 0) { int t = x / d - d; if (t & 1) continue; t >>= 1; ll tar = 1ll * t * t - a[i]; if (tar < 0) break; if (vis[tar]) continue; vis[tar] = true; ans = max(ans, test(tar)); } } printf("%d\n", ans); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inline T rd(){ T x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007
vector<int> s;
structnode {int u, d, l, r;} c[N];
inlinevoidwork(){ int n = rd(); for (int i = 1; i <= n; ++i) { c[i].u = rd(); c[i].l = rd(); c[i].d = rd(); c[i].r = rd(); }
// u = 1 && d = 2 s.clear(); for (int i = 1; i <= n; ++i) if (c[i].u == 1 && c[i].d == 2) s.pb(i); sort(all(s), [&](int a, int b){return c[a].l == c[b].l ? c[a].r < c[b].r : c[a].l < c[b].l;}); int r = 0; for (auto x : s) { if (c[x].r <= r) c[x] = (node){0, 0, 0, 0}; else {c[x].l = max(c[x].l, r + 1); r = c[x].r;} }
// u = 1 s.clear(); for (int i = 1; i <= n; ++i) if (c[i].u == 1) s.pb(i); sort(all(s), [&](int a, int b){return c[a].l == c[b].l ? c[a].r < c[b].r : c[a].l < c[b].l;}); r = 0; for (int i = 0, lim = (int)s.size(), ptr = 0; i < lim; ++i) { int x = s[i]; if (c[x].d == 2) { if (r >= c[x].r) c[x].u = 2; else { for (; ptr < i; ++ptr) if (c[s[ptr]].r >= c[x].l) c[s[ptr]].r = c[x].l - 1; r = c[x].r; } } else { if (c[x].r <= r) c[x] = (node){0, 0, 0, 0}; else {c[x].l = max(c[x].l, r + 1); r = c[x].r;} } }
// d = 2 s.clear(); for (int i = 1; i <= n; ++i) if (c[i].d == 2) s.pb(i); sort(all(s), [&](int a, int b){return c[a].l == c[b].l ? c[a].r < c[b].r : c[a].l < c[b].l;}); r = 0; for (int i = 0, lim = (int)s.size(), ptr = 0; i < lim; ++i) { int x = s[i]; if (c[x].u == 1) { if (r >= c[x].r) c[x].d = 1; else { for (; ptr < i; ++ptr) if (c[s[ptr]].r >= c[x].l) c[s[ptr]].r = c[x].l - 1; r = c[x].r; } } else { if (c[x].r <= r) c[x] = (node){0, 0, 0, 0}; else {c[x].l = max(c[x].l, r + 1); r = c[x].r;} } }
int tot = 0; for (int i = 1; i <= n; ++i) { if (c[i].u > c[i].d || c[i].l > c[i].r) c[i] = (node){0, 0, 0, 0}; if (c[i].u) tot += (c[i].d - c[i].u + 1) * (c[i].r - c[i].l + 1); } printf("%d\n", tot); for (int i = 1; i <= n; ++i) printf("%d %d %d %d\n", c[i].u, c[i].l, c[i].d, c[i].r); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }