#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
inline void fastio() {
  ios::sync_with_stdio();
  cin.tie(0);
  cout.tie(0);
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  if (n < 2) {
    cout << "-1\n";
    return;
  }
  vector<int> left(n, -1);
  vector<int> last(n, -1);
  for (int i = 1; i < n; ++i) {
    left[i] = last[a[i]];
    last[a[i]] = i;
  }
  int ans = n;
  for (int i = 0; i < n; ++i) {
    if (left[i] != -1) {
      ans = min(ans, i - left[i] + 1);
    }
  }
  cout << ans << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
