#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    map<int, int> mp;
    int ans = INF;
    for (int i = 0; i < n; ++i) {
      int x;
      scanf("%d", &x);
      if (mp.count(x)) ans = min(ans, i - mp[x] + 1);
      mp[x] = i;
    }
    if (ans == INF) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
