#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &a, U b) {
  a = (a > b ? b : a);
}
template <typename T, typename U>
inline void amax(T &a, U b) {
  a = (a > b ? a : b);
}
const int M = 2e5 + 7;
vector<int> pos[M + 1];
void solve() {
  int N;
  cin >> N;
  vector<int> a(N);
  set<int> s;
  for (int i = 0; i < N + 1; ++i) pos[i].clear();
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    s.insert(a[i]);
    pos[a[i]].push_back(i);
  }
  if (((int)s.size()) == N) {
    cout << "-1\n";
    return;
  }
  int ans = N;
  for (int i = 0; i < N + 1; ++i) {
    for (int j = 1; j < ((int)pos[i].size()); ++j) {
      amin(ans, pos[i][j] - pos[i][j - 1] + 1);
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tests = 1;
  cin >> tests;
  while (tests--) {
    solve();
  }
}
