#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> A;
void solve_case() {
  cin >> N;
  A.resize(N);
  for (auto &a : A) cin >> a;
  vector<int> last(N + 1, -1);
  vector<int> next(N, -1);
  for (int i = N - 1; i >= 0; i--) {
    next[i] = last[A[i]];
    last[A[i]] = i;
  }
  int best = N + 1, min_next = N + 1;
  for (int i = N - 1; i >= 0; i--)
    if (next[i] >= 0) {
      if (min_next > next[i]) best = min(best, next[i] - i + 1);
      min_next = min(min_next, next[i]);
    }
  cout << (best <= N ? best : -1) << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T-- > 0) solve_case();
}
