#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v(n);
    long long mark = 0;
    unordered_map<long long, long long> mp;
    long long len = n;
    for (long long i = 0; i < n; i++) {
      cin >> v[i];
      if (mp.find(v[i]) == mp.end()) {
        mp[v[i]] = i;
      } else {
        mark = 1;
        long long curr = i + 1 - mp[v[i]];
        len = min(curr, len);
        mp[v[i]] = i;
      }
    }
    if (!mark) {
      cout << -1 << "\n";
      continue;
    }
    cout << len << "\n";
  }
  return 0;
}
