#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> g(2e5 + 5);
    int ans = 3e5;
    for (long long i = 0; i < n; i++) {
      long long q;
      cin >> q;
      g[q].push_back(i);
    }
    for (long long i = 0; i < g.size(); i++) {
      if (g[i].size() <= 1) {
        continue;
      }
      for (long long j = 0; j < g[i].size() - 1; j++) {
        ans = min(ans, g[i][j + 1] - g[i][j] + 1);
      }
    }
    cout << ((ans == 3e5) ? -1 : ans) << "\n";
  }
  return 0;
}
