#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int a[200005];
vector<int> G[200005];
int solve() {
  int n;
  cin >> n;
  for (int i = 0; i <= n; i++) G[i].clear();
  for (int i = 0; i < n; i++) cin >> a[i], G[a[i]].push_back(i);
  if (n == 1) return -1;
  int mn = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)G[i].size() - 1; j++) {
      mn = min(mn, G[i][j + 1] - G[i][j] + 1);
    }
  }
  if (mn == INF) return -1;
  return mn;
}
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) cout << solve() << '\n';
}
