#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 2e5 + 36;
int U[N], a[N], T = 1;
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    T++;
    int ans = n + 1;
    for (int i(0); i < n; ++i) {
      int x;
      cin >> x;
      if (U[x] == T) {
        ans = min(ans, i - a[x] + 1);
      }
      U[x] = T;
      a[x] = i;
    }
    if (ans == n + 1) {
      ans = -1;
    }
    cout << ans << endl;
  }
  return 0;
}
