#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long int n;
    cin >> n;
    long int a[n], b[n];
    memset(b, 0, sizeof(b));
    long int i, j, k;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      b[a[i] - 1]++;
    }
    k = 10000000;
    long int l = 0;
    for (i = 1; i < n; i++) {
      if (b[a[i] - 1] >= 2) {
        for (j = i - 1; j >= l; j--) {
          if (i - j + 1 > k) break;
          if (a[i] == a[j]) {
            if (k > i - j + 1) k = i - j + 1;
            l = i;
            break;
          }
        }
      }
    }
    if (n == 1 || k == 10000000)
      cout << "-1\n";
    else
      cout << k << endl;
  }
  return 0;
}
