#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> A(n);
    vector<pair<int, int> > Occ(n + 1);
    for (int i = 0; i < n + 1; i++) Occ[i].first = 0;
    for (int i = 0; i < n; i++) cin >> A[i];
    int next_start_idx = 0;
    int ans = INT_MAX;
    for (int i = next_start_idx; i < n; ++i) {
      Occ[A[i]].first++;
      if (Occ[A[i]].first >= 2) {
        if (i - Occ[A[i]].second < ans) {
          ans = i - Occ[A[i]].second + 1;
        }
      }
      Occ[A[i]].second = i;
    }
    if (ans == INT_MAX)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
