#include <bits/stdc++.h>
using namespace std;
const int S = 600;
int n, a[200010], b[200010], tag[200010];
vector <int> pos[200010];
int ans[200010];
void part1() {
    for (int i = 1; i <= n; i++) pos[i].clear(), pos[i].push_back(n + 1);
    for (int i = n; i > 0; i--) {
        vector <int> &cps = pos[a[i]];
        for (int j = 0; j < (int)cps.size(); j++) {
            ans[a[i]] = max(ans[a[i]], tag[cps[j] - 1] - j);
        }
        cps.insert(cps.begin(), i);
        for (int j = 0; j < (int)cps.size(); j++) {
            int cur = cps[j];
            while (tag[cur] < j + 1 && cur <= n) tag[cur++] = j + 1;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < (int)pos[i].size(); j++) {
            ans[i] = max(ans[i], tag[pos[i][j] - 1] - j);
        }
        ans[i] += pos[i].size() - 1;
    }
}
void part2(int _max) {
    for (int i = 1; i <= n; i++) if (pos[i].size() * 2 >= _max) {
        for (int j = 1, p = 0; j <= n; j++) {
            if (p < (int)pos[i].size() && pos[i][p] == j) p++;
            tag[j] = p;
        }
        for (int t = 1; t <= n; t++) {
            int lst = 0, cur = 0;
            for (auto j : pos[t]) {
                cur = max(0, cur + lst - tag[j]);
                lst = tag[j];
                ans[i] = max(ans[i], ++cur);
            }
        }
        ans[i] += pos[i].size();
    }
}
void rmain() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) pos[i].clear(), ans[i] = tag[i] = 0;
    for (int i = 1; i <= n; i++) scanf("%d", a + i), b[i] = a[i];
    sort(b + 1, b + 1 + n);
    for (int i = 1; i <= n; i++) pos[a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b].push_back(i);
    int _max = 0;
    for (int i = 1; i <= n; i++) _max = max(_max, (int)pos[i].size());
    if (_max <= S) part1();
    else part2(_max);
    for (int i = 1; i <= n; i++) _max = max(_max, ans[i]);
    printf("%d\n", _max);
    for (int i = 1; i <= n; i++) if (ans[i] == _max) printf("%d\n", b[i]);
}
int main() {
    freopen("mode.in", "r", stdin);
    freopen("mode.out", "w", stdout);
    int T;
    scanf("%d", &T);
    while (T--) rmain();
}