#include <bits/stdc++.h>
using namespace std;
void faster() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int t;
int main() {
  faster();
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n], i, b[n], p = 0, mn, min = 10000000;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = 0;
    }
    for (i = 0; i < n; i++) {
      for (int j = n - 1; j > i; j--) {
        if (a[i] == a[j]) {
          b[p] = j - i + 1;
          mn = b[p];
          if (b[p] == 2) {
            cout << "2\n";
            break;
          }
          p++;
        }
      }
      if (b[p] == 2) {
        break;
      }
    }
    if (b[p] == 2) {
      continue;
    }
    sort(b, b + p);
    if (p == 0) {
      cout << "-1\n";
      continue;
    }
    cout << b[0] << '\n';
  }
}
