#include "bits/stdc++.h" using namespace std; //#define int long long #define ld long double #define ll long long #define st first #define nd second #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define all(x) begin(x),end(x) #define FOR(i,l,r) for(int i = (l); i <= (r); i++) #define ROF(i,r,l) for (int i = (r); i >= (l); i--) auto& operator<<(auto&o, pair<auto,auto>p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto&o, auto x)->decltype(end(x), o) { o << "{"; int i =0; for (auto e : x) o << ","+!i++ << e; return o << "}"; } #ifdef LOCAL #define debug(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "), ...) << endl; }(x) #else #define debug(...) {} #endif #define rep(i, a, b) for (int i = (a); i < (b); i++) using pii = pair<int, int>; using vi = vector<int>; const int inf = 1e9 + 7; const int N=4007; ll a[N]; ll t[N]; vector<int>G[N*N+N]; ll val[N*N+N]; void edge(int u,int v) { G[u].pb(v); G[v].pb(u); debug(u,v); } int vis[N*N+N], czas = 1; int match[N*N+N]; bool dfs(int u, int good) { if (vis[u] == czas) return false; vis[u] = czas; for (int v : G[u]) if (v <= good) { if (match[v] == 0) { match[v] = u; match[u] = v; return true; } } else { break; } for (int v : G[u]) if (v <= good) { if (dfs(match[v], good)) { match[v] = u; match[u] = v; return true; } } else { break; } return false; } signed main() { cin.tie(0)->sync_with_stdio(0); int tt; cin>>tt; while(tt--) { int n; cin>>n; vector<pair<ll,int>>V; priority_queue<pair<ll,int>>Q; FOR(i,1,n) cin>>a[i]; sort(a + 1, a + 1 + n); reverse(a + 1, a + 1 + n); FOR(i,1,n) { Q.push({-a[i],i}); t[i]=1; } int it=0; while(sz(Q)>0) { vector<int>T; int k=Q.top().st; while(sz(Q)>0&&Q.top().st==k) { T.pb(Q.top().nd); Q.pop(); } it++; val[it]=-k; for(auto i:T) edge(i,n+it); if(sz(T)>1) { it++; debug(-k,T); bool is=1; FOR(i,1,sz(T)-1) if(a[T[i]]!=a[T[i-1]]) is=0; FOR(l,is,sz(T)-1) { int i=T[l]; t[i]++; Q.push({-a[i]*t[i],i}); } } } /* FOR(i,1,n) { FOR(j,1,t[i]) { V.pb({(ll)a[i]*(ll)j,i}); debug(a[i]*(ll)j); } debug(tt,i,t[i],a[i]); } sort(all(V)); it=1; edge(V[0].nd,n+1); val[1]=V[0].st; //int now = 0; FOR(i,1,sz(V)-1) { if(V[i].st!=V[i-1].st) { it++; val[it]=V[i].st; } edge(V[i].nd,n+it); }*/ mt19937 rng(213797); FOR(i, 1, n) { sort(all(G[i])); } vector<bool> in(n + 1); auto check = [&](int mid) { FOR(j, 1, n) if (in[j]) { if (match[j] > mid) { match[match[j]] = 0; match[j] = 0; } } FOR(j, 1, n) if (in[j] && match[j] == 0) { if (!dfs(j, mid)) { czas++; if (!dfs(j, mid)) { return false; } } } return true; }; vi order(n); iota(all(order), 1); sort(all(order), [&](int i, int j) { return sz(G[i]) < sz(G[j]); }); int last = n+n; for (int i : order) { in[i] = true; int l = last - 1, r = n + it; while (r - l > 1) { int mid = (l + r) >> 1; if (check(mid)) { r = mid; } else { l = mid; } } last = r; } cout << val[last-n] << '\n'; /* FOR(i, 1, it) { now++; if (match[n+i] == 0 && !dfs(n+i, n+i)) { czas++; if (!dfs(n+i, n+i)) { now--; // nie udalo sie znalesc matchingu } } if (now == n) { cout<<val[i]<<endl; break; } }*/ czas++; FOR(i,0,n*n+n) { G[i].clear(); match[i] = 0; vis[i] = 0; } } return 0; }