#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    set<int> s;
    vector<int> li(200005, 0);
    long long int ans = (long long)(1e18 + 1e15);
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      if (s.count(x)) {
        ans = min(ans, (long long int)(i - li[x] + 1));
      }
      li[x] = i;
      s.insert(x);
    }
    if (ans == (long long)(1e18 + 1e15)) ans = -1;
    cout << ans << endl;
  }
}
