#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long min(long long x, long long y) { return x < y ? x : y; }
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
long long n, arr[maxn], brr[maxn], mk[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    n = read();
    for (long long i = 0; i < n; i++) arr[i] = read();
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    memset(mk, -0x3f3f3f3f, sizeof mk);
    memset(brr, 0x3f3f3f3f, sizeof brr);
    long long ans = 0x3f3f3f3f;
    for (long long i = 0; i < n; i++) {
      if (mk[arr[i]] != -0x3f3f3f3f)
        brr[arr[i]] = min(brr[arr[i]], i - mk[arr[i]] + 1);
      mk[arr[i]] = i;
      ans = min(ans, brr[arr[i]]);
    }
    if (ans < 0x3f3f3f3f)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
