#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, a[200010], cnt = 0, mx = 1e9;
  pair<pair<int, int>, int> p[20010];
  int b[200010];
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      p[i].first.first = a[i];
      b[a[i]]++;
      p[i].first.second = i;
      p[i].second = b[a[i]];
    }
    sort(p + 1, p + n + 1);
    for (int i = 1; i <= n; i++) {
      bool ok = false;
      for (int j = i + 1; j <= n; j++) {
        if (p[i].first.first == p[j].first.first) {
          if (p[j].first.second - p[i].first.second < mx)
            mx = p[j].first.second - p[i].first.second;
        }
      }
    }
    if (mx == 1e9)
      cout << -1 << endl;
    else
      cout << mx + 1 << endl;
  }
  return 0;
}
