#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, length, t;
  cin >> t;
  map<long long, long long> m;
  for (long long k = 0; k < t; k++) {
    cin >> n;
    length = n + 1;
    m.clear();
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      if (m.find(x) != m.end()) length = min(length, i - m[x] + 1);
      m[x] = i;
    }
    cout << ((length == (n + 1)) ? -1 : length) << '\n';
  }
}
