#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll testcase, i, j, n, len, t = 1;
  pair<ll, ll> p;
  cin >> testcase;
  while (testcase--) {
    t++;
    cin >> n;
    len = n;
    ll arr[n];
    for (i = 0; i < n; i++) cin >> arr[i];
    i = 0, j = n - 1;
    while (j - i > 0) {
      if (arr[j] == arr[i]) len = min(len, j - i + 1);
      if (arr[i] == arr[i + 1])
        j--;
      else if (arr[j] == arr[j - 1])
        i++;
      else {
        j--;
        i++;
      }
    }
    if (i == j && len == n) len = -1;
    if (t == 92)
      for (i = 0; i < n; i++) {
        cout << arr[i] << " ";
        cout << "KUlprit" << endl;
      }
    cout << len << endl;
  }
}
