#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 13;
const double pi = 3.14159265359;
const double e = 2.718281828459045;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> v(n);
    map<int, int> mp, mm;
    for (int i = 0; i < n; ++i) {
      cin >> v[i];
      mp[v[i]]++;
    }
    if (n == 1 || mp.size() == n) {
      cout << "-1\n";
      continue;
    }
    int ans = n;
    int i = 0, j = 2;
    set<int> st;
    st.insert(v[0]);
    if (v[0] == v[1]) {
      cout << "2\n";
      continue;
    } else {
      st.insert(v[1]);
    }
    mm[v[0]]++;
    mm[v[1]]++;
    while (j < n && i != j) {
      st.insert(v[j]);
      mm[v[j]]++;
      while (st.size() != j - i + 1) {
        ans = min(ans, j - i + 1);
        if (mm[v[i]] == 1) {
          st.erase(v[i]);
          mp[v[i]]--;
        } else
          mm[v[i]]--;
        ++i;
      }
      ++j;
    }
    cout << ans << "\n";
  flag:
    ans;
  }
  return 0;
}
