#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 50;
const int inf = 1e9;
const int mod = 1e9 + 7;
int a[maxn], p[maxn];
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      p[i] = -1;
      cin >> a[i];
    }
    int ans = inf;
    for (int i = 1; i <= n; i++) {
      if (p[a[i]] != -1) {
        ans = min(ans, i - p[a[i]] + 1);
      }
      p[a[i]] = i;
    }
    if (ans == inf)
      cout << "-1" << endl;
    else
      printf("%d\n", ans);
  }
  return 0;
}
