#include <bits/stdc++.h>
using namespace std;
long long dominatedArr(vector<long long> A, long long n) {
  if (n < 2) return -1;
  if (n == 2) {
    if (A[0] == A[1]) return 2;
    return -1;
  }
  map<long long, long long> M;
  long long len = -1;
  for (int i = 0; i < n; i++) {
    if (M[A[i]] == 0)
      M[A[i]] = i + 1;
    else {
      if (len == -1)
        len = i + 1 - M[A[i]] + 1;
      else
        len = min(len, i + 1 - M[A[i]] + 1);
      M[A[i]] = i + 1;
    }
  }
  return len;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> A(n);
    for (long long i = 0; i < n; i++) cin >> A[i];
    long long ans = dominatedArr(A, n);
    cout << ans << endl;
  }
  return 0;
}
