#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    int n, c = 0, ans = INT_MAX;
    cin >> n;
    int a[n];
    vector<int> arr(n + 1, -1);
    unordered_map<int, int> ump;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      ump[a[i]]++;
    }
    for (auto i : ump) {
      if (i.second == 1) c++;
    }
    if (c == n)
      cout << -1 << endl;
    else {
      for (int i = 0; i < n; i++) {
        if (arr[a[i]] != -1) ans = min(ans, i - arr[a[i]] + 1);
        arr[a[i]] = i;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
