#include <bits/stdc++.h>
using namespace std;
int c[100001];
vector<int> d;
int main() {
  int n, t, b[100000];
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      c[b[i]]++;
    }
    int start = 0, end = 1, mnx = 1e6;
    while (start < end && end < n) {
      if (n < 2) break;
      if (c[b[end]] > 0) {
        c[b[end]]--;
        for (int i = start; i < end; i++) {
          if (b[i] == b[end]) {
            mnx = min(mnx, end - i + 1);
            start++;
            while (c[b[start]] == 0 && start < end) start++;
            break;
          }
        }
      }
      end++;
    }
    if (mnx == 1e6)
      cout << -1 << endl;
    else
      cout << mnx << endl;
  }
}
