#include <bits/stdc++.h>
using namespace std;
long long modularExponentiation(long long x, long long n, long long M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modularExponentiation((x * x) % M, n / 2, M);
  return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M;
}
long long binaryExponentiation(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return binaryExponentiation(x * x, n / 2);
  else
    return x * binaryExponentiation(x * x, (n - 1) / 2);
}
long long GCD(long long A, long long B) {
  if (B == 0)
    return A;
  else
    return GCD(B, A % B);
}
long long extended_GCD(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long modinv(long long a, long long mod) {
  long long x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
long long modPrimeInverse(long long A, long long M) {
  return modularExponentiation(A, M - 2, M);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (n < 2) {
      cout << "-1"
           << "\n";
      continue;
    }
    long long freq[n + 7];
    memset(freq, 0, sizeof(freq));
    long long i = 0, j = 0;
    long long flag = 0, ans = 1e18;
    while (i < n && j < n) {
      if (freq[a[j]] >= 1) {
        flag = 1;
        while (a[i] != a[j]) {
          freq[a[i]]--;
          i++;
        }
        ans = min(ans, j - i + 1);
        while (i < j) {
          freq[a[i]]--;
          i++;
        }
      } else {
        freq[a[j]]++;
        j++;
      }
    }
    if (!flag) {
      cout << "-1"
           << "\n";
    } else {
      cout << ans << "\n";
    }
  }
  return 0;
}
