#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, weight;
};
void solve();
int main() {
  int times = 1;
  cin >> times;
  for (int i = 1; i <= times; i++) {
    solve();
    cout << '\n';
  }
  return 0;
}
vector<edge> V[300000 + 50];
int a, b, c, d, e, f;
int arr[300000 + 50], arr1[300000 + 50], arr2[300000 + 50];
string str, str1, str2;
void solve() {
  cin >> a;
  for (int i = 0; i < a; i++) cin >> arr[i];
  if (a == 1) {
    cout << "-1";
    return;
  }
  int ans = 999999999;
  int pos[200050];
  for (int i = 0; i <= 200000; i++) pos[i] = -1;
  for (int i = 0; i < a; i++) {
    if (pos[arr[i]] == -1)
      pos[arr[i]] = i;
    else {
      ans = min(ans, i - pos[arr[i]] + 1);
      pos[arr[i]] = i;
    }
  }
  if (ans == 999999999)
    cout << "-1";
  else
    cout << ans;
}
