#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, int> cnt;
    vector<int> r(N);
    int ans = 2e5 + 5;
    for (int i = 0; i < n; ++i) {
      int x;
      cin >> x;
      cnt[x]++;
      if (cnt[x] == 1)
        r[x] = i;
      else if (cnt[x] > 1) {
        if (i - r[x] + 1 < ans) {
          ans = i - r[x] + 1;
        }
        r[x] = i;
      }
    }
    if (ans == 2e5 + 5) ans = -1;
    cout << ans << "\n";
  }
  return 0;
}
