#include <bits/stdc++.h>
#define ll long long
#define sz(a) int((a).size())
using namespace std;
const int N = 5e5 + 10;
int T, n, B;
int a[N], cnt[N], b[N];
int sum[N], buc[N], mx[N];
int ans, res[N], tot = 0;
vector<int> g[N];
struct fwt {
    int mx[N];
    void init() {
        for(int i = 1; i <= n; i++) mx[i] = -1e9;
    }
    void add(int x, int v) {
        for(int i = x; i <= n; i += i & (-i)) mx[i] = max(mx[i], v);
    }
    int ask(int x) {
        int res = -1e9;
        for(int i = x; i > 0; i -= i & (-i)) res = max(res, mx[i]);
        return res;
    }
}t;
void check(int x, int y) {
    if(ans < x) ans = x, tot++, res[y] = tot;
    else if(ans == x) res[y] = tot;
}
void init() {
    for(int i = 1; i <= n; i++) b[i] = a[i];
    sort(b + 1, b + n + 1);
    int tot = unique(b + 1, b + n + 1) - (b + 1);
    for(int i = 1; i <= n; i++) {
        a[i] = lower_bound(b + 1, b + tot + 1, a[i]) - b;
    }
}
void solve1(int v) {
    for(int i = 1; i <= n; i++) {
        buc[i] = mx[i] = 0;
        sum[i] = sum[i - 1] + (v == a[i]);
    }
    for(int i = 1; i <= n; i++) {
        if(a[i] == v) continue;
        check(mx[a[i]] + sum[i - 1] - buc[a[i]] + cnt[a[i]], a[i]);
        buc[a[i]]++;
        mx[a[i]] = max(mx[a[i]], buc[a[i]] - sum[i]);
    }
    for(int i = 1; i <= n; i++) {
        check(mx[i] + sum[n], i);
    }
    return;
}
void solve3(int v) {
    for(int i = 1; i <= n; i++) {
        buc[i] = mx[i] = 0;
        sum[i] = sum[i - 1] + (v == a[i]);
    }
    for(int i = 1; i <= n; i++) {
        if(a[i] == v) continue;
        mx[a[i]] = max(mx[a[i]], -buc[a[i]] + sum[i - 1]);
        buc[a[i]]++;
        check(mx[a[i]] - sum[i] + buc[a[i]] + sum[n], v);
    }
    return;
}
void solve2() {
    t.init();
    for(int i = n; i >= 1; i--) {
        int pos = -1, x = a[i];
        if(sz(g[x]) >= B) continue;
        for(int j = 0; j < sz(g[x]); j++) if(g[x][j] == i) pos = j;
        for(int j = pos; j < sz(g[x]); j++) {
            int v = g[x][j];
            check(t.ask(v - 1) + pos + 1 + sz(g[x]) - j, x);
        }
        check(t.ask(n) + pos + 1, x);
        for(int j = pos; j < sz(g[x]); j++) {
            int v = g[x][j];
            t.add(v, j - pos + 1);
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < sz(g[i]); j++) {
            int v = g[i][j];
            check(t.ask(v - 1) + sz(g[i]) - j, i);
        }
    }
    return;
}
void solve() {
    scanf("%d", &n);
    B = max(int(sqrt(n)) / 10, 1);
    ans = 0, tot = 0;
    for(int i = 1; i <= n; i++) res[i] = cnt[i] = 0, g[i].clear();
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    init();
    for(int i = 1; i <= n; i++) g[a[i]].emplace_back(i);
    for(int i = 1; i <= n; i++) cnt[a[i]]++;
    for(int i = 1; i <= n; i++) if(cnt[i] >= B) solve1(i), solve3(i);
    solve2();
    printf("%d\n", ans);
    for(int i = 1; i <= n; i++) if(res[i] == tot) printf("%d\n", b[i]);
    return;
}
int main() {
    freopen("mode.in", "r", stdin);
    freopen("mode.out", "w", stdout);
    scanf("%d", &T);
    for(; T--; ) solve();
    return 0;
}