#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<long long, int> m;
    int a[n + 1];
    a[0] = -1;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int ans = INT_MAX;
    for (int i = 1; i <= n; i++) {
      if (m[a[i]] == 0) {
        m[a[i]] = i;
        continue;
      }
      ans = min(ans, i - m[a[i]] + 1);
      m[a[i]] = i;
    }
    if (ans == INT_MAX)
      cout << "-1\n";
    else
      cout << ans << "\n";
  }
  return 0;
}
