#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  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 - 1; i++) cin >> a[i];
    ;
    long long int dom = 1000000, vigl = -1;
    unordered_map<long long int, long long int> make_pair;
    for (long long int i = 0; i <= n - 1; i++) {
      if (make_pair.find(a[i]) == make_pair.end())
        make_pair[a[i]] = i;
      else {
        vigl = 1;
        dom = min(dom, i - make_pair[a[i]]);
        make_pair[a[i]] = i;
      }
    }
    if (vigl == -1)
      cout << -1 << '\n';
    else
      cout << dom + 1 << '\n';
  }
}
