#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=2007; 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); } 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; } } for (int v : G[u]) if (v <= good) { if (dfs(match[v], good)) { match[v] = u; match[u] = v; return true; } } 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]; FOR(i,1,n) { Q.push({-a[i],i}); t[i]=1; } 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(); } if(sz(T)>1) { 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)); int 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); } 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; }