#include <bits/stdc++.h>
using namespace std;
int n, ans = 999999999, ans1 = 999999999;
int a[3000003], dp[3000003], l[3000003];
void solve() {
  cin >> n;
  for (int i = 0; i < 2 * n; i++) {
    a[i] = 0;
    dp[i] = 0;
    l[i] = 0;
  }
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = l[a[i]];
    l[a[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (dp[i] == 0 || dp[i] - i == 1) continue;
    ans = dp[i] - i + 1;
    ans1 = min(ans, ans1);
  }
  if (ans == 999999999)
    cout << "-1\n";
  else
    cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) solve();
}
