#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int c = n + 1;
  int b[n];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    if (m.find(b[i]) == m.end())
      m[b[i]] = i;
    else {
      c = min(c, i - m[b[i]] + 1);
      m[b[i]] = i;
    }
  }
  m.clear();
  if (c != n + 1) {
    cout << c << endl;
  } else
    cout << "-1" << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
