#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 5e5 + 20;
const long long mod = 1e9 + 7;
int a[MAXN];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    memset(a, 0, sizeof(a));
    int minn = 0x3f3f3f3f;
    for (int i = 1; i <= n; ++i) {
      int x;
      cin >> x;
      if (a[x] != 0) minn = min(i - a[x] + 1, minn);
      a[x] = i;
    }
    if (minn == 0x3f3f3f3f)
      cout << -1 << endl;
    else
      cout << minn << endl;
  }
}
