#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  unordered_multimap<int, int> mp;
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    mp.insert({y, i});
  }
  int lowest = n;
  int key = mp.begin()->first;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    auto nxt = it;
    nxt++;
    if (nxt == mp.end())
      break;
    else if (nxt->first != key) {
      key = nxt->first;
      break;
    }
    int diff = nxt->second - it->second;
    if (diff < lowest) lowest = diff;
  }
  if (lowest == n)
    cout << -1 << endl;
  else
    cout << lowest + 1 << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
