#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, inp;
  cin >> n;
  if (n == 1) {
    cin >> inp;
    cout << -1 << "\n";
  } else {
    vector<pair<int, int>> a;
    for (int i = 0; i < n; i++) {
      cin >> inp;
      a.push_back(make_pair(inp, i));
    }
    sort(a.begin(), a.end());
    vector<int> occur;
    bool dom = true;
    int cnt = 1;
    for (int i = 0; i < n - 1; i++) {
      if (a[i].first == a[i + 1].first) {
        cnt++;
      } else {
        occur.push_back(cnt);
        cnt = 1;
      }
    }
    if (cnt > 1) {
      occur.push_back(cnt);
    }
    sort(occur.begin(), occur.end());
    if (occur.size() == 1) {
      dom = true;
    } else if (occur[occur.size() - 1] == occur[occur.size() - 2]) {
      dom = false;
    }
    if (dom) {
      int ans = 99999999;
      for (int i = 0; i < n - 1; i++) {
        if (a[i].first == a[i + 1].first) {
          ans = min(ans, abs(a[i + 1].second - a[i].second));
        }
      }
      cout << ans + 1 << "\n";
    } else {
      cout << -1 << "\n";
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
