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