#include <bits/stdc++.h>
using namespace std;
int a[200005];
int occ[200005];
queue<int> v[200005];
int min1;
bool value;
int main() {
  int t;
  cin >> t;
  while (t--) {
    value = false;
    int n;
    cin >> n;
    int i;
    min1 = n;
    for (i = 0; i < 200005; ++i) {
      a[i] = 0;
      occ[i] = 0;
      if (v[i].size()) v[i].pop();
    }
    for (i = 1; i <= n; ++i) {
      cin >> a[i];
      occ[a[i]]++;
      if (occ[a[i]] >= 2) value = true;
    }
    if (n == 1 || !value)
      cout << -1 << endl;
    else {
      for (i = 1; i <= n; ++i) {
        if (occ[a[i]] >= 2) {
          v[a[i]].push(i);
          if (v[a[i]].size() == 2) {
            min1 = min(min1, 1 + abs(v[a[i]].back() - v[a[i]].front()));
            v[a[i]].pop();
          }
        }
      }
      cout << min1 << endl;
    }
  }
}
