#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int las[N], vis[N];
int main() {
  int T;
  cin >> T;
  while (T--) {
    memset(vis, 0, sizeof vis);
    int n;
    cin >> n;
    for (int i = 1, x; i <= n; ++i) {
      cin >> x;
      if (vis[x]) las[vis[x]] = i;
      vis[x] = i;
    }
    int ans = n + 1;
    for (int i = 1; i <= n; ++i)
      if (las[i]) ans = min(ans, las[i] - i + 1);
    printf("%d\n", ans == n + 1 ? -1 : ans);
  }
}
