#include <bits/stdc++.h>
using namespace std;
long long qpow(long long a, long long b, long long mod) {
  long long r = 1;
  for (; b; b >>= 1) {
    if (b & 1) r = (r * a) % mod;
    a = (a * a) % mod;
  }
  return r;
}
const long long mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
int a[N], cnt[N];
void work() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    if (n == 1)
      puts("-1");
    else {
      int l = 2, r = n, ans = -1;
      while (l <= r) {
        int mid = (l + r) / 2;
        memset(cnt, 0, sizeof(cnt));
        int ok = 0;
        for (int i = 1; i <= mid; i++) {
          cnt[a[i]]++;
          if (cnt[a[i]] >= 2) {
            ok = 1;
            break;
          }
        }
        int f = 1;
        for (int i = mid + 1; i <= n; i++) {
          cnt[a[f++]]--;
          cnt[a[i]]++;
          if (cnt[a[i]] >= 2) {
            ok = 1;
            break;
          }
        }
        if (ok) {
          ans = mid;
          r = mid - 1;
        } else
          l = mid + 1;
      }
      printf("%d\n", ans);
    }
  }
}
int main() {
  work();
  return 0;
}
