#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long N = 4e5 + 3;
const long long md = 1e9 + 7;
const long long inf = 1e18;
const int MAX_CHARS = 256;
bool cmp(string a, string b) {
  string s2 = a.append(b);
  string s3 = b.append(a);
  long long z = stoi(s2);
  long long z1 = stoi(s3);
  return z < z1 ? 1 : 0;
}
string first(string str);
int main() {
  long long t;
  cin >> t;
  while (t--) {
    std::vector<long long> arr1;
    long long n, mini = INT_MAX, maxi = INT_MIN, diff, z, ans = INT_MAX;
    cin >> n;
    vector<int> l(n + 1, -1);
    long long arr[n];
    long long freq[200001];
    memset(freq, 0, sizeof(freq));
    for (long long i = 0; i < (n); i++) {
      cin >> arr[i];
    }
    for (int i = 0; i < n; i++) {
      if (l[arr[i]] != -1) {
        long long z = i - l[arr[i]] + 1;
        ans = min(ans, z);
      }
      l[arr[i]] = i;
    }
    if (ans > n) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}
