#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    int k = 1000000;
    cin >> n;
    unordered_map<int, int> um;
    for (int i = 1; i <= n; i++) {
      int temp;
      cin >> temp;
      if (!um[temp]) {
        um[temp] = i;
      } else {
        if (k > i - um[temp] + 1) k = i - um[temp] + 1;
        if (k == 2) {
          break;
        }
        um[temp] = i;
      }
    }
    if (k == 1000000)
      cout << -1 << "\n";
    else
      cout << k << "\n";
  }
  return 0;
}
