#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int a[n];
    for (long long int i = 0; i < n; i++) cin >> a[i];
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    map<long long int, long long int> mp;
    long long int ans = 1e18;
    for (long long int i = 0; i < n; i++) {
      if (!mp.count(a[i])) {
        mp[a[i]] = i;
      } else {
        ans = min(ans, i - mp[a[i]] + 1);
        mp[a[i]] = i;
      }
    }
    if (ans == 1e18)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
