#include <bits/stdc++.h>
using namespace std;
inline void filehandling() {}
vector<long long> vector_in(long long n) {
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  return v;
}
void vector_out(vector<long long> v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
long long vector_sum(vector<long long> v) {
  long long sum;
  for (long long i = 0; i < v.size(); i++) sum += v[i];
  return sum;
}
long long Deepanshu[200099];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  filehandling();
  long long TestCases = true;
  cin >> TestCases;
  while (TestCases--) {
    long long n;
    cin >> n;
    long long M = LLONG_MAX;
    for (long long iteratorr = 1; iteratorr <= n; iteratorr++) {
      long long Te;
      cin >> Te;
      if (Deepanshu[Te]) M = min(M, iteratorr - Deepanshu[Te]);
      Deepanshu[Te] = iteratorr;
    }
    if (M > 1e7)
      cout << "-1" << endl;
    else
      cout << M + 1 << endl;
    for (long long iteratorr = 1; iteratorr <= n; iteratorr++)
      Deepanshu[iteratorr] = false;
  }
}
