#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T gi() {
  T f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
const long long INF = 0x3f3f3f3f, N = 200003, M = N << 1;
long long n, a[N], pre[N], lst[N];
signed main() {
  long long T = gi<long long>();
  while (T--) {
    n = gi<long long>();
    for (long long i = 1; i <= n; i += 1) a[i] = gi<long long>(), lst[a[i]] = 0;
    for (long long i = 1; i <= n; i += 1) {
      pre[i] = lst[a[i]];
      lst[a[i]] = i;
    }
    long long ans = INF;
    for (long long i = 1; i <= n; i += 1)
      if (pre[i]) ans = min(ans, i - pre[i] + 1);
    if (ans == INF)
      puts("-1");
    else
      printf("%lld\n", ans);
  }
  return 0;
}
