#include <bits/stdc++.h>
using namespace std;
int k, n, a[100000], b[100000], f, s;
int main() {
  cin >> n;
  while (n) {
    cin >> k;
    map<int, pair<int, int>> m;
    for (int i = 0; i < k; i++) {
      cin >> a[i];
      m[a[i]].first++;
      m[a[i]].second = (i + 1) - m[a[i]].second;
    }
    int res = 1e5;
    for (int i = 0; i < m.size(); i++) {
      if (m[a[i]].first >= 2) {
        if (res > m[a[i]].second) {
          res = m[a[i]].second;
          s = a[i];
        }
      }
    }
    if (k <= 1)
      cout << -1 << "\n";
    else if (m[s].first == 2 && s == a[0] && s == a[k - 1])
      cout << res + 1 << "\n";
    else
      cout << res << "\n";
    n--;
  }
}
