#include <bits/stdc++.h>
int a[200005];
int pos[200005];
using namespace std;
int main() {
  set<int> s;
  int t;
  cin >> t;
  while (t--) {
    int n, ans;
    s.clear();
    memset(pos, 0, sizeof pos);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    ans = 0x3f;
    for (int i = 1; i <= n; i++) {
      if (s.count(a[i]) == 1) ans = min(ans, i - pos[a[i]] + 1);
      s.insert(a[i]);
      pos[a[i]] = i;
    }
    if (ans == 0x3f)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
