#include <bits/stdc++.h>
using namespace std;
long long q;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> q;
  while (q--) {
    long long n;
    long long m2 = 0, kar = 200001;
    cin >> n;
    long long last[n + 4];
    for (int i = 0; i <= n; i++) last[i] = -1;
    for (int i = 0; i < n; i++) {
      long long x;
      cin >> x;
      if (last[x] == -1) {
        last[x] = i;
        continue;
      } else {
        kar = min(kar, i - last[x] + 1);
      }
      last[x] = i;
    }
    cout << (kar == 200001 ? -1 : kar) << "\n";
  }
}
