#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int len;
    cin >> len;
    vector<int> arr(len);
    for (int i = 0; i < len; i++) cin >> arr[i];
    map<int, int> m;
    int ans = INT_MAX;
    for (int i = 0; i < len; i++) {
      if (m.count(arr[i])) {
        int diff = i - m[arr[i]] + 1;
        if (diff < ans) ans = diff;
      } else {
        m[arr[i]] = i;
      }
    }
    if (ans = INT_MAX)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
