#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const double eps = 1e-4;
const int mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
const int maxm = 1e6 + 7;
const double pi = acos(-1.0);
int a[maxn];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    map<int, int> mp;
    mp.clear();
    int ans = inf;
    for (int i = 1; i <= n; i++) {
      if (mp[a[i]] != 0) {
        ans = min(ans, i - mp[a[i]] + 1);
      }
      mp[a[i]] = i;
    }
    if (ans == inf)
      puts("-1");
    else
      printf("%d\n", ans);
  }
  return 0;
}
