#include <bits/stdc++.h>
using namespace std;
int T, n, a[200005], f[200005];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    if (n < 2) {
      puts("-1");
      continue;
    }
    map<int, int> dd;
    for (int i = n; i >= 1; --i) {
      f[i] = dd[a[i]];
      dd[a[i]] = i;
    }
    int ans = 1000000007;
    for (int i = 1; i <= n; ++i)
      if (f[i] != 0) ans = min(ans, f[i] - i);
    if (ans == 1000000007) {
      puts("-1");
      continue;
    }
    printf("%d\n", ans + 1);
  }
  return 0;
}
