#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 13;
const double pi = 3.14159265359;
const double e = 2.718281828459045;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> v(n);
    map<int, vector<int>> mp;
    map<int, set<int>> mm;
    map<int, bool> mpb;
    for (int i = 0; i < n; ++i) {
      cin >> v[i];
      mp[v[i]].push_back(i);
      mm[v[i]].insert(i);
      mpb[v[i]] = 0;
    }
    if (mp.size() == n || n == 1) {
      cout << "-1\n";
      continue;
    }
    int ans = n;
    for (int i = 0; i < n; ++i) {
      if (!mpb[v[i]]) {
        mpb[v[i]] = 1;
        int cnt = 0;
        int l = 0;
        for (auto t : mm[v[i]]) {
          if (cnt > 0) {
            ans = min(ans, t - l + 1);
            l = t;
          } else {
            l = t;
          }
          cnt++;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
