#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
void init() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int last_idx[N];
int main() {
  init();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    memset(last_idx, -1, sizeof(int) * (n + 5));
    int last_update = -1;
    int ans = -1;
    for (int i = 0; i < (int)(n); ++i) {
      int num;
      cin >> num;
      if (last_idx[num] != -1) {
        if (last_update == -1) {
          last_update = last_idx[num];
          ans = i + 1 - last_idx[num];
        } else if (last_idx[num] > last_update) {
          last_update = last_idx[num];
          ans = min(ans, i + 1 - last_idx[num]);
        }
      }
      last_idx[num] = i;
    }
    cout << ans << "\n";
  }
  return 0;
}
