#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> tab(n);
    for (int i = 0; i < n; i++) cin >> tab[i];
    if (n == 1) {
      cout << "-1" << endl;
      continue;
    }
    int i1 = 0, i2 = 1;
    unordered_set<int> S;
    S.insert(tab[0]);
    int ret = n;
    bool flag = false;
    while (i2 < n) {
      while (S.find(tab[i2]) != S.end()) {
        flag = true;
        ret = min(ret, i2 - i1 + 1);
        S.erase(tab[i1++]);
      }
      S.insert(tab[i2++]);
    }
    if (flag) {
      cout << ret << endl;
    } else
      cout << "-1" << endl;
  }
  return 0;
}
