#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tc;
  cin >> tc;
  while (tc--) {
    long long n;
    cin >> n;
    long long arr[n + 1];
    for (long long i = 1; i <= n; i++) cin >> arr[i];
    if (n == 1) {
      cout << -1 << '\n';
      continue;
    }
    unordered_map<long long, long long> mp;
    long long m = INT_MAX;
    for (long long i = 1; i <= n; i++) {
      if (mp[arr[i]]) {
        m = min(m, i - mp[arr[i]] + 1);
        mp[arr[i]] = i;
      } else {
        mp[arr[i]] = i;
      }
    }
    if (m != INT_MAX)
      cout << m << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}
