#include <bits/stdc++.h>
using namespace std;
int v[200001], lastap[200001];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t, l, n, i, min1;
  cin >> t;
  for (l = 1; l <= t; l++) {
    cin >> n;
    min1 = n + 1;
    for (i = 1; i <= n; i++) cin >> v[i];
    for (i = 1; i <= n; i++) lastap[i] = 0;
    for (i = 1; i <= n; i++) {
      if (lastap[v[i]]) min1 = min(min1, i - lastap[v[i]] + 1);
      lastap[v[i]] = i;
    }
    if (min1 == n + 1)
      cout << "-1\n";
    else
      cout << min1 << '\n';
  }
  return 0;
}
