#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rund() {
  int x576363482791fuweh = rng();
  return abs(x576363482791fuweh) % RAND_MAX;
}
const char en = '\n';
vector<int> bases;
ll myrand() {
  ll a = rund();
  a *= RAND_MAX;
  a += rund();
  return a;
}
const ll llinf = 1LL << 60;
const int inf = 1 << 30;
const int MOD = 1e9 + 7;
void yes() {
  cout << "YES" << en;
  return;
}
void no() {
  cout << "NO" << en;
  return;
}
inline int pot(int n, int k) {
  ll ret = 1;
  while (k) {
    if (k & 1) {
      ret *= n;
      ret %= MOD;
    }
    n *= n;
    n %= MOD;
    k >>= 1;
  }
  return ret;
}
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int MAXN = 1e5 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < 10; ++i)
    bases.push_back(rand() % (MOD - 2 * 13893829) + 13893829);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto& it : v) cin >> it;
    vector<int> ls(n + 1, -1);
    int sol = inf;
    for (int i = 0; i < n; ++i) {
      if (ls[v[i]] != -1) sol = min(sol, i - ls[v[i]] + 1);
      ls[v[i]] = i;
    }
    if (sol != inf)
      cout << sol;
    else
      cout << -1;
    cout << en;
  }
  return 0;
}
