#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    int n, min = 10e6;
    cin >> n;
    int a[n + 1], b[n];
    for (int j = 0; j < n + 1; ++j) a[j] = -1;
    for (int j = 0; j < n; ++j) cin >> b[j];
    for (int j = 0; j < n; ++j) {
      if (a[b[j]] == -1)
        a[b[j]] = j;
      else {
        if (j - a[b[j]] < min) min = j - a[b[j]];
        a[b[j]] = j;
      }
    }
    if (min == 10e6)
      cout << "-1\n";
    else
      cout << min + 1 << "\n";
  }
}
