// Ý tưởng: Nếu A[i] có tần suất lớn hơn hoặc bằng 1, chắc chắn sẽ được đẩy vào một vector v[tần suất A[i]], ví dụ 5 xuất hiện 3 lần thì v[3] = {5}
// Sau đó sort từng v[i] theo thứ tự từ bé đến lớn, rồi in ra theo thứ tự tần suất từ lớn đến bé
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 1e5 + 1;
int A[maxn], D[maxn];
vector<int> v[maxn];
void refresh(){
for(int i=1;i<=maxn;++i){
D[i]=0;
}
for(int i=1;i<=1e4;++i){
v[i].clear();
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("file.inp","r",stdin);
// freopen("file.out","w",stdout);
int t;
int n, x, ma = 0, maxFreq = 0;
cin >> t;
while (t--) {
ma = 0; maxFreq = 0;
cin >> n;
refresh();
for (int i = 1; i <= n; ++i) {
cin >> A[i];
++D[A[i]];
ma = max(ma, A[i]);
maxFreq = max(maxFreq, D[A[i]]);
}
for (int i = 1; i <= n; ++i) {
v[D[A[i]]].push_back(A[i]);
}
for (int i = 1; i <= ma; ++i) {
if (v[i].size() != 0) {
sort(v[i].begin(), v[i].end());
}
}
for (int i = maxFreq; i >= 1; --i) {
if (v[i].size() != 0) {
for (auto x : v[i]) {
cout << x << " ";
}
}
}
cout << endl;
}
}
Ly8gw50gdMaw4bufbmc6IE7hur91IEFbaV0gY8OzIHThuqduIHN14bqldCBs4bubbiBoxqFuIGhv4bq3YyBi4bqxbmcgMSwgY2jhuq9jIGNo4bqvbiBz4bq9IMSRxrDhu6NjIMSR4bqpeSB2w6BvIG3hu5l0IHZlY3RvciB2W3ThuqduIHN14bqldCBBW2ldXSwgdsOtIGThu6UgNSB4deG6pXQgaGnhu4duIDMgbOG6p24gdGjDrCB2WzNdID0gezV9Ci8vIFNhdSDEkcOzIHNvcnQgdOG7q25nIHZbaV0gdGhlbyB0aOG7qSB04buxIHThu6sgYsOpIMSR4bq/biBs4bubbiwgcuG7k2kgaW4gcmEgdGhlbyB0aOG7qSB04buxIHThuqduIHN14bqldCB04burIGzhu5tuIMSR4bq/biBiw6kgCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGFsbCh4KSB4LmJlZ2luKCksIHguZW5kKCkKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG1heG4gPSAxZTUgKyAxOwppbnQgQVttYXhuXSwgRFttYXhuXTsKdmVjdG9yPGludD4gdlttYXhuXTsKdm9pZCByZWZyZXNoKCl7CiAgICBmb3IoaW50IGk9MTtpPD1tYXhuOysraSl7CiAgICAgICAgRFtpXT0wOwogICAgfQogICAgZm9yKGludCBpPTE7aTw9MWU0OysraSl7CiAgICAgICAgdltpXS5jbGVhcigpOwogICAgfQp9CmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICAvLyBmcmVvcGVuKCJmaWxlLmlucCIsInIiLHN0ZGluKTsKICAgIC8vIGZyZW9wZW4oImZpbGUub3V0IiwidyIsc3Rkb3V0KTsKICAgIGludCB0OwogICAgaW50IG4sIHgsIG1hID0gMCwgbWF4RnJlcSA9IDA7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBtYSA9IDA7IG1heEZyZXEgPSAwOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIHJlZnJlc2goKTsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKICAgICAgICAgICAgY2luID4+IEFbaV07CiAgICAgICAgICAgICsrRFtBW2ldXTsKICAgICAgICAgICAgbWEgPSBtYXgobWEsIEFbaV0pOwogICAgICAgICAgICBtYXhGcmVxID0gbWF4KG1heEZyZXEsIERbQVtpXV0pOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKICAgICAgICAgICAgdltEW0FbaV1dXS5wdXNoX2JhY2soQVtpXSk7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG1hOyArK2kpIHsKICAgICAgICAgICAgaWYgKHZbaV0uc2l6ZSgpICE9IDApIHsKICAgICAgICAgICAgICAgIHNvcnQodltpXS5iZWdpbigpLCB2W2ldLmVuZCgpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gbWF4RnJlcTsgaSA+PSAxOyAtLWkpIHsKICAgICAgICAgICAgaWYgKHZbaV0uc2l6ZSgpICE9IDApIHsKICAgICAgICAgICAgICAgIGZvciAoYXV0byB4IDogdltpXSkgewogICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgeCA8PCAiICI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBlbmRsOwogICAgfQp9Cg==