#include <bits/stdc++.h>
using namespace std;
int minSubarray(vector<int> br, int n) {
  vector<int> pon(10000);
  int mini = INT_MAX;
  for (int i = 0; i < 10000; i++) pon[i] = 0;
  for (int i = 0; i < br.size(); i++) {
    pon[br[i]]++;
    int m;
    if (pon[br[i]] == 2) {
      for (int j = 0; j < i; j++) {
        if (br[j] == br[i]) m = i - j + 1;
      }
      if (m < mini) mini = m;
      pon[br[i]] = 1;
    }
    if (mini == 2) return 2;
  }
  return mini;
}
int main() {
  int t;
  cin >> t;
  vector<int> v(t);
  int n;
  for (int i = 0; i < t; i++) {
    cin >> n;
    vector<int> br(n);
    for (int j = 0; j < n; j++) cin >> br[j];
    if (n < 2)
      v[i] = -1;
    else {
      v[i] = minSubarray(br, n);
    }
  }
  for (int i = 0; i < t; i++) cout << v[i] << endl;
}
