#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    long int q;
    cin >> q;
    long int inp[200000];
    long int count[200000] = {0};
    long int _max = -1, _min = 2000001;
    for (long int i = 0; i < q; i++) {
      cin >> inp[i];
      count[inp[i]]++;
      if (inp[i] > _max) _max = inp[i];
      if (inp[i] < _min) _min = inp[i];
    }
    vector<pair<long int, long int> > pos;
    bool flag = false;
    for (long int i = 0; i < q; i++)
      if (count[inp[i]] >= 2) {
        flag = true;
        pos.push_back(make_pair(inp[i], i));
      }
    sort(pos.begin(), pos.end());
    long int _size = 1000000;
    for (long int i = 1; i < pos.size(); i++) {
      if (pos[i].first == pos[i - 1].first)
        if (abs(pos[i].second - pos[i - 1].second + 1) < _size)
          _size = abs(pos[i].second - pos[i - 1].second + 1);
      if (_size == 2) break;
    }
    if (flag == false) _size = -1;
    cout << _size << endl;
  }
}
