#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MAXN = 1;
const long long MOD = 1e+9 + 7, INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
double EPS = 1e-9;
double PI = acos(-1);
vector<long long> adj[MAXN];
long long visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1},
    dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    long long i, j;
    long long x, b, d, d1, d2, max1, min1 = 1e+18, y;
    long long a[500000], prev1[500000];
    cin >> n;
    for (i = 0; i <= n; i++) {
      prev1[i] = -1;
    }
    for (i = 0; i < n; i++) {
      cin >> a[i];
      if (prev1[a[i]] != -1) {
        if (abs(prev1[a[i]] - i) < min1) {
          min1 = abs(prev1[a[i]] - i);
        }
      }
      prev1[a[i]] = i;
    }
    if (n == 1) {
      cout << -1 << endl;
      continue;
    }
    if (min1 == 1e+18) {
      cout << -1 << endl;
      continue;
    } else {
      cout << min1 + 1 << endl;
      continue;
    }
  }
}
