#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m = INT16_MAX;
    cin >> n;
    vector<int> a(n), b(n + 2, -1);
    for (int &it : a) cin >> it;
    int g = n + 1;
    for (int i = 0, d = n + 1; i < n; ++i) {
      if (b[a[i]] != -1) {
        d = i - b[a[i]];
        g = min(d, g);
      }
      b[a[i]] = i;
    }
    if (g == n + 1)
      cout << -1 << endl;
    else
      cout << g + 1 << endl;
  }
}
