#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn];
int n;
int cnt[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(cnt, 0, sizeof(cnt));
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    int l = 1;
    int r = 2;
    int ans = 1e9;
    cnt[a[l]] = 1;
    while (1) {
      while (r <= n) {
        if (!cnt[a[r]])
          cnt[a[r]] = 1;
        else {
          cnt[a[r]]++;
          break;
        }
        r++;
      }
      if (r > n) break;
      while (cnt[a[l]] != 2) {
        cnt[a[l]]--, l++;
      }
      cnt[a[l]]--;
      l++;
      r++;
      ans = min(ans, r - l + 1);
    }
    if (ans != 1e9)
      printf("%d\n", ans);
    else
      printf("-1\n");
  }
  return 0;
}
