#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<int> a(n);
    vector<vector<int>> g(2e5 + 5);
    int ans = 3e5;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      g[a[i]].push_back(i);
      if (i - 1 >= 0 && a[i] == a[i - 1]) {
        ans = 2;
      }
    }
    for (long long i = 0; i < g.size(); i++) {
      if (g[i].size() <= 1) {
        continue;
      }
      ans = min(ans, g[i][g[i].size() - 1] - g[i][g[i].size() - 2] + 1);
    }
    cout << ((ans == 3e5) ? -1 : ans) << "\n";
    return 0;
  }
}
