#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e13;
const double PI = acos(-1);
const long long M = 1e9 + 7;
int a[200010];
map<int, vector<int>> mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    mp.clear();
    int n;
    cin >> n;
    if (n == 1) {
      int x;
      cin >> x;
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; ++i) cin >> a[i], mp[a[i]].push_back(i);
    int ans = 1e9;
    for (int i = 1; i <= n; ++i) {
      int best = 1e9;
      if (!mp[i].size()) continue;
      for (int j = 0; j < mp[i].size() - 1; ++j)
        best = min(best, 1 + mp[i][j + 1] - mp[i][j]);
      ans = min(ans, best);
    }
    cout << (ans == 1e9 ? -1 : ans) << endl;
  }
}
