#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    bool ok = 0;
    cin >> n;
    vector<int> v(n + 1, -5);
    int ans = INT_MAX, x;
    for (int i = 1; i <= n; i++) {
      cin >> x;
      if (v[x] >= 0) {
        ans = min(ans, i - v[x] + 1);
      }
      v[x] = i;
    }
    ok = ans > n;
    cout << ((ok) ? -1 : ans) << endl;
  }
}
