#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, int> mp;
    int flag = 0;
    int len = 0;
    int ans = 1e9;
    vector<int> adj[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      adj[a[i] - 1].push_back(i);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (int)adj[i].size() - 1; j++) {
        ans = min(adj[i][j + 1] - adj[i][j], ans);
      }
    }
    if (ans == 1e9)
      cout << -1 << endl;
    else
      cout << ans + 1 << endl;
  }
  return 0;
}
