#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  auto start = high_resolution_clock::now();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int A[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> A[i];
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    map<int, int> hash;
    int mnm = INT_MAX;
    for (int i = 1; i <= n; i++) {
      if (hash[A[i]]) {
        mnm = min(i - hash[A[i]] + 1, mnm);
      }
      hash[A[i]] = i;
    }
    cout << mnm << endl;
  }
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
}
