https://codeforces.com/contest/1625/problem/B
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;

int n, L, k;

void solve() {
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i ++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < n - 1; i ++)
if (a[i].first == a[i + 1].first)
ans = max (ans, a[i].second + n - a[i + 1].second);
if (!ans) ans = -1;
cout << ans << endl;
}

signed main () {
int t;
cin >> t;
while(t --) solve();

}

相隔越近越好。因为这样可以选京可能的多。