#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int> > occ(n + 1);
    for (int i = 0; i < n; ++i) {
      int x;
      cin >> x;
      occ[x].push_back(i);
    }
    int ans = n + 1;
    for (int i = 1; i <= n; ++i) {
      if (occ[i].size() < 2) continue;
      for (int j = 1; j < occ[i].size(); ++j)
        ans = min(ans, occ[i][j] - occ[i][j - 1] + 1);
    }
    if (ans == n + 1)
      cout << -1 << "\n";
    else
      cout << ans << "\n";
  }
}
