#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, i, j, res;
  int x;
  int t;
  cin >> t;
  while (t--) {
    vector<pair<int, int>> v;
    vector<pair<int, int>>::iterator it;
    cin >> n;
    int arr[n];
    res = 10000000;
    int b[n + 1];
    memset(b, 0, sizeof(b));
    for (i = 0; i < n; i++) {
      cin >> arr[i];
      v.push_back({arr[i], i});
    }
    sort(v.begin(), v.end());
    for (it = v.begin(); (it + 1) != v.end(); ++it) {
      if (it->first == (it + 1)->first)
        res = min(res, (it + 1)->second - it->second + 1);
    }
    if (res == 10000000)
      cout << "-1" << endl;
    else
      cout << res << endl;
  }
  return 0;
}
