#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long N = 1000005;
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  map<long long, long long> m;
  long long mn = 1e18;
  for (long long i = 0; i < n; i++) {
    long long x = a[i];
    if (m.find(x) != m.end()) {
      mn = min(mn, i - m[x] + 1);
    }
    m[a[i]] = i;
  }
  if (mn == 1e18)
    cout << -1 << "\n";
  else
    cout << mn << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
}
