#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int m = 2 * n;
    vector<int> a(n);
    map<int, int> b;
    b.clear();
    for (int i = 0; i < n; i++) cin >> a[i], b[a[i]] = -1;
    for (int i = 0; i < n; i++) {
      if (b[a[i]] != -1) m = min(m, i - b[a[i]] + 1);
      b[a[i]] = i;
    }
    cout << ((m > n) ? -1 : m) << endl;
  }
  return 0;
}
