#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int i, a[200005] = {0}, c, ans = n, flag = 0;
    for (i = 0; i < n; i++) {
      cin >> c;
      if (a[c] != 0) {
        flag = 1;
        ans = min(ans, i + 1 - a[c]);
      }
      a[c] = i + 1;
    }
    if (flag == 0)
      cout << -1 << endl;
    else
      cout << ans + 1 << endl;
  }
  return 0;
}
