#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int ans = INT_MAX;
    map<int, int> m;
    for (int i = 1; i <= n; i++) {
      int temp;
      cin >> temp;
      if (m[temp] != 0) {
        ans = min(ans, i - m[temp] + 1);
        m[temp] = i;
      } else {
        m[temp] = i;
      }
    }
    if (ans == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}
