#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int a;
    cin >> a;
    int arr[a];
    for (int i = 1; i <= a; i++) cin >> arr[i];
    map<int, int> m;
    vector<int> v;
    for (int i = 1; i <= a; i++) {
      if (m.find(arr[i]) == m.end())
        m[arr[i]] = i;
      else
        v.push_back(i - m[arr[i]]);
      m[arr[i]] = i;
    }
    int ans = 100000000;
    for (int j = 0; j < v.size(); j++)
      if (ans > v[j]) {
        ans = v[j];
      }
    if (a == 1) {
      cout << "-1" << endl;
    }
    if (v.size() == 0)
      cout << "-1" << endl;
    else
      cout << ans + 1 << endl;
  }
}
