#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  if (n == 1) {
    cout << -1;
  } else {
    long long ans = 1e18;
    map<int, int> mp;
    int l = 0;
    for (int i = 0; i < n; i++) {
      mp[a[i]]++;
      if (mp[a[i]] == 2) {
        while (mp[a[i]] != 1) {
          mp[a[l]]--;
          l++;
        }
        ans = min(ans, (long long)i - l + 2);
      }
    }
    if (ans == 1e18)
      cout << -1;
    else
      cout << ans;
  }
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
