#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int t, n, pre, res;
int last[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    res = 1e9;
    memset(last, 0, sizeof last);
    for (int i = 1; i <= n; i++) {
      cin >> t;
      if (0 != last[t]) res = min(res, i - last[t] + 1);
      last[t] = i;
    }
    cout << (res = (res == 1e9) ? -1 : res) << "\n";
  }
}
