#include <bits/stdc++.h>
using namespace std;
long long t, n;
void cal() {
  int Max = 0;
  long long ans = 1234567899;
  vector<long long> a[n + 3];
  for (long long i = 1; i <= n; i++) {
    long long k;
    cin >> k;
    a[k].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (Max < a[i].size()) Max = a[i].size();
    a[i].push_back(1234567899);
    for (int j = 0; j < a[i].size() - 1; j++)
      ans = min(a[i][j + 1] - a[i][j] + 1, ans);
  }
  if (n < 2)
    cout << -1 << '\n';
  else if (Max == 1)
    cout << 1 << "\n";
  else
    cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    cal();
  }
  return 0;
}
