#include <bits/stdc++.h>
using namespace std;
vector<int> a;
map<int, int> used;
map<int, int> gr;
void solve() {
  bool t = 0;
  used.clear();
  gr.clear();
  int n;
  cin >> n;
  int mx = n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    used[a] += 1;
    if (used[a] >= 2) {
      t = 1;
      if (i - gr[a] + 1 < mx) mx = i - gr[a] + 1;
    }
    gr[a] = i;
  }
  if (t == 0) {
    cout << -1 << endl;
    return;
  }
  if (mx >= 2)
    cout << mx << endl;
  else
    cout << -1 << endl;
}
int main() {
  int q;
  cin >> q;
  while (q) {
    solve();
    q--;
  }
}
