#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], p[maxn];
void prework() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void solve() {
  memset(p, 0, sizeof(p));
  int n;
  scanf("%d", &n);
  int f = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    p[a[i]]++;
    if (p[a[i]] >= 2) f = 1;
  }
  if (n <= 2 || f == 0) {
    printf("-1\n");
    return;
  }
  int ans = 2e6;
  for (int i = 0; i < n - 1; i++) {
    if (p[a[i]] <= 1) continue;
    int cur = maxn;
    for (int k = i + 1; k < n; k++) {
      if (a[k] == a[i]) {
        cur = k;
        break;
      }
    }
    p[a[i]]--;
    ans = min(ans, cur - i + 1);
  }
  printf("%d\n", ans);
}
int main() {
  prework();
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(p, 0, sizeof(p));
    int len;
    scanf("%d", &len);
    int f = 0;
    for (int i = 0; i < len; i++) {
      scanf("%d", &a[i]);
      p[a[i]]++;
      if (p[a[i]] >= 2) f = 1;
    }
    if (f == 0 || len <= 2) {
      printf("-1\n");
      continue;
    }
    int ans = 1e6;
    for (int i = 0; i < len - 1; i++) {
      if (p[a[i]] < 2) continue;
      int cur = maxn;
      for (int k = i + 1; k < len; k++) {
        if (a[k] == a[i]) {
          cur = k;
          break;
        }
      }
      p[a[i]]--;
      ans = min(ans, cur - i + 1);
    }
    printf("%d\n", ans);
  }
  return 0;
}
