#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (n == 1) {
      cout << "-1\n";
      continue;
    }
    map<int, int> m;
    int p = 0, q = 1;
    int len = INT_MAX, f = 0;
    while (q < n || p < n) {
      m.clear();
      for (int i = p; i < q; i++) {
        m[a[i]] = 1;
      }
      if (m.count(a[q])) {
        f = 1;
        len = min(len, (q - p + 1));
        p = q;
        q++;
      } else {
        if (q < n)
          q++;
        else
          p++;
      }
    }
    if (f == 0) {
      cout << "-1\n";
    } else {
      cout << len << endl;
    }
  }
}
