#include <bits/stdc++.h>
using namespace std;
int sol(vector<int> v, unordered_map<int, int> m1, unordered_map<int, int> m2) {
  if (m1.size() == v.size()) return -1;
  int i;
  for (i = 0; i < v.size(); i++) {
    if (m1[v[i]] == 1) {
      m1.erase(v[i]);
      m2.erase(v[i]);
    }
  }
  int res = 200000;
  unordered_map<int, int> m3;
  for (i = 0; i < v.size(); i++) {
    if (m1.find(v[i]) == m1.end()) continue;
    if (m1[v[i]] == m2[v[i]]) {
      m1[v[i]] = i;
      m3[v[i]] = i;
      continue;
    } else {
      m1[v[i]] = i - m3[v[i]] + 1;
      m3[v[i]] = i;
    }
    if (m1[v[i]] < res) res = m1[v[i]];
  }
  return res;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    unordered_map<int, int> m1, m2;
    int i;
    for (i = 0; i < n; i++) {
      cin >> v[i];
      m1[v[i]]++;
      m2[v[i]]++;
    }
    int d = sol(v, m1, m2);
    cout << d << endl;
  }
  return 0;
}
