#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
int N, a[MAXN], last[MAXN], c[MAXN];
int main() {
  int t;
  cin >> t;
  while (t) {
    cin >> N;
    bool ok = false;
    for (int i = 0; i < N; i++) {
      c[i] = 0;
      a[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      cin >> a[i];
      a[i]--;
      c[a[i]]++;
      if (c[a[i]] == 2) ok = true;
      last[i] = -1;
    }
    if (ok) {
      int min_val = N + 1;
      for (int i = 0; i < N; i++) {
        if (last[a[i]] == -1)
          last[a[i]] = i;
        else {
          min_val = min(min_val, i - last[a[i]]);
          last[a[i]] = i;
        }
      }
      cout << min_val + 1 << endl;
    } else {
      cout << -1 << endl;
    }
    t--;
  }
}
