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