#include <bits/stdc++.h>
using namespace std;
void print(vector<int> v) {
  for (int i = 0; i < (v.size()); i++) cout << v[i] << " ";
  cout << endl;
}
void print(vector<int> v, int si) {
  for (int i = 0; i < (si); i++) cout << v[i] << " ";
  cout << endl;
}
void print(map<int, int> m) {
  for (auto it : m) cout << it.first << " " << it.second << endl;
}
const int mod = 1e9 + 7;
bool A[100000001];
void solve() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < (n); i++) cin >> A[i];
  int ans = 1e6;
  map<int, int> mem;
  vector<int> pos[n + 1];
  int c = 1;
  for (int i = 0; i < (n); i++) {
    if (mem[A[i]] == 0) mem[A[i]] = c++;
    pos[mem[A[i]]].push_back(i);
  }
  for (int i = 0; i < (c); i++) {
    if (pos[i].size() >= 2) {
      for (int j = 0; j < (pos[i].size() - 1); j++) {
        ans = min(ans, pos[i][j + 1] - pos[i][j] + 1);
      }
    }
  }
  if (ans == (1e6))
    cout << "-1\n";
  else
    cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
