#include <bits/stdc++.h>
const double PI = 3.1415926;
const int KL = 1e6;
using namespace std;
long long q, x, y, z, n, m, a[KL], s[KL], mn = 1e9, mx = -1e9, ans;
string t;
vector<long long> vec[KL];
map<long long, long long> mp;
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    mn = 1e9;
    for (long long i = 0; i < n; i++) {
      mp[a[i]]++;
      if (mp[a[i]] >= 2) {
        mn = min(mn, i - vec[a[i]].back() + 1);
      }
      vec[a[i]].push_back(i);
    }
    if (mn == 1e9)
      cout << -1 << endl;
    else
      cout << mn << endl;
    mp.clear();
  }
  return 0;
}
