#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, unsigned long long int y,
                    long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int tc;
  cin >> tc;
  while (tc--) {
    long long int n;
    cin >> n;
    long long int a[n], mx = 0;
    map<long long int, long long int> mp;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
      mx = max(mx, a[i]);
    }
    long long int in[mx + 1];
    long long int ans = 1e18;
    for (long long int i = 0; i < n; i++) {
      mp[a[i]]++;
      if (mp[a[i]] == 2) {
        ans = min(ans, i - in[a[i]]);
        mp[a[i]] = 1;
      }
      in[a[i]] = i;
    }
    if (ans == 1e18) ans = -2;
    cout << ans + 1 << "\n";
    ;
  }
  return 0;
}
