#include <bits/stdc++.h>
using namespace std;
template <typename T>
void max_self(T &x, T y) {
  x = max(x, y);
}
template <typename T>
void min_self(T &x, T y) {
  x = min(x, y);
}
long long modExpo(long long a, long long b, long long c) {
  a = a % c;
  if (b == 0)
    return 1;
  else if ((b % 2) == 0)
    return modExpo((a * a) % c, b / 2, c) % c;
  else
    return (a * (modExpo((a * a) % c, b / 2, c) % c)) % c;
}
const int nax = 2e5 + 5;
const long long linf = 1e18;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> arr(n);
    for (int i = (0); i < (n); i++) cin >> arr[i];
    bool flag = false;
    vector<int> occ(nax, -1);
    occ[arr[0]] = 0;
    int res = INT_MAX;
    for (int i = (1); i < (n); i++) {
      if (occ[arr[i]] == -1)
        occ[arr[i]] = i;
      else {
        flag = true;
        min_self(res, i - occ[arr[i]] + 1);
        occ[arr[i]] = i;
      }
    }
    if (!flag)
      cout << "-1\n";
    else
      cout << res << "\n";
  }
  return 0;
}
