#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int array[n];
    for (int i = 0; i < n; i++) cin >> array[i];
    vector<int> aray[n + 1];
    for (int i = 0; i < n; i++) aray[array[i]].push_back(i);
    int ma = n + 1;
    for (int i = 1; i <= n; i++) {
      if (aray[i].size() <= 1) continue;
      for (int j = 0; j < aray[i].size() - 1; j++)
        ma = min(aray[i][j + 1] - aray[i][j] + 1, ma);
    }
    if (ma == n + 1)
      cout << -1 << "\n";
    else
      cout << ma << "\n";
  }
  return 0;
}
