#include <bits/stdc++.h>
using namespace std;
int v[200100];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> p(n, -1);
    int ans = 1000000007;
    for (int i = 0; i < n; ++i) {
      cin >> v[i];
      if (!i) {
        p[v[i]] = i;
      } else {
        if (p[v[i]] > -1) {
          ans = min(i - p[v[i]] + 1, ans);
        }
        p[v[i]] = i;
      }
    }
    if (ans == 1000000007)
      cout << "-1\n";
    else
      cout << ans << "\n";
  }
  return 0;
}
