#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);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<int> f(200001, -1);
    int best = INT_MAX;
    for (int i = 0; i < n; i++) {
      if (f[a[i]] != -1) best = min(best, i - f[a[i]] + 1);
      f[a[i]] = i;
    }
    cout << (best == INT_MAX ? -1 : best) << "\n";
  }
  return 0;
}
