#include <bits/stdc++.h>
using namespace std;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int main() {
  fastio();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    map<int, pair<int, int>> m;
    int f = 0;
    int freq[3 * 100005] = {0};
    for (int i = 0; i < n; i++) {
      freq[a[i]]++;
      if (m.find(a[i]) == m.end())
        m[a[i]] = {i + 1, i};
      else {
        f = 1;
        if (freq[a[i]] == 2)
          m[a[i]].first = i - m[a[i]].second + 1;
        else
          m[a[i]].first = min(m[a[i]].first, i - m[a[i]].second + 1);
        m[a[i]].second = i;
      }
    }
    auto it = m.begin();
    int ans = INT_MAX;
    while (it != m.end()) {
      if (freq[it->first] >= 2) ans = min(ans, (it->second).first);
      it++;
    }
    if (f)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
}
