#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    map<int, int> mymap;
    map<int, int> last;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      if (!mymap[arr[i]]) {
        last[arr[i]] = i;
        mymap[arr[i]] = INT_MAX;
      } else {
        mymap[arr[i]] = min(mymap[arr[i]], i - last[arr[i]]);
        last[arr[i]] = i;
      }
    }
    if (n < 2) {
      cout << -1 << endl;
      continue;
    }
    int m = INT_MAX;
    for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++) {
      if (it->second < m && m != 0) m = min(m, it->second);
    }
    if (m == INT_MAX)
      cout << -1 << endl;
    else
      cout << m + 1 << endl;
  }
  return 0;
}
