#include <bits/stdc++.h>
using namespace std;
int n, a;
vector<vector<int> > v;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    v.clear();
    scanf("%d", &n);
    v.resize(n + 1);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a);
      v[a].push_back(i);
    }
    int mn = -1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j < v[i].size(); j++) {
        if (mn == -1) mn = v[i][j] - v[i][j - 1];
        mn = min(mn, v[i][j] - v[i][j - 1]);
      }
    }
    if (mn == -1) {
      puts("-1");
    } else {
      printf("%d\n", mn + 1);
    }
  }
  return 0;
}
