#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, i, j;
  cin >> t;
  while (t--) {
    int n, a[200010], ans = 10000000;
    map<int, set<int>> m;
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      m[a[i]].insert(i);
    }
    for (auto it = m.begin(); it != m.end(); it++) {
      int l = it->second.size(), ele = it->first;
      if (l != 1) {
        int c = 0;
        int last;
        for (int x : it->second) {
          if (c > 0) ans = min(ans, x - last + 1);
          last = x;
          c++;
        }
      }
    }
    if (ans == 10000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
