#include <bits/stdc++.h>
using namespace std;
long long int fastModExp(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % 1000000007;
    a = (a * a) % 1000000007;
    b = b >> 1;
  }
  return res;
}
vector<bool> sieve(int N) {
  vector<bool> isPrime(N + 1);
  for (int i = 0; i <= N; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) {
      for (int j = i * i; j <= N; j += i) isPrime[j] = false;
    }
  }
  return isPrime;
}
map<long long int, long long int> mpx1, mpx2;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> v;
    long long int x;
    for (int i = 0; i < n; i++) {
      cin >> x;
      v.push_back(x);
    };
    mpx1.clear();
    mpx2.clear();
    long long int ans = 2e18, sum = 0;
    if (v.size() == 1) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++) {
      if (mpx1[v[i]] == 0) {
        mpx1[v[i]] = 1;
        mpx2[v[i]] = i;
      } else {
        ans = min(ans, (i - mpx2[v[i]] + 1));
        mpx2[v[i]] = i;
      }
    }
    if (ans == 2e18)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
