#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, length, t;
  cin >> t;
  for (long long k = 0; k < t; k++) {
    length = 200005;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (long long i = 0, j = n - 1; j > i; i++, j--) {
      if (a[i] == a[j]) {
        length = min(length, j - i + 1);
      }
    }
    if (length == 200005)
      cout << -1 << endl;
    else
      cout << length << endl;
  }
}
