#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int pre[maxn], arr[maxn];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    int ans = inf;
    memset(pre, 0, sizeof(pre));
    for (int i = 1; i <= n; i++) {
      cin >> arr[i];
      if (pre[arr[i]] == 0) {
        pre[arr[i]] = i;
      } else {
        ans = min(ans, i - pre[arr[i]]);
        pre[arr[i]] = i;
      }
    }
    if (ans == inf)
      cout << "-1"
           << "\n";
    else
      cout << ans + 1 << "\n";
  }
  return 0;
}
