#include <bits/stdc++.h>
using namespace std;
int64_t gcd(int64_t a, int64_t b) {
  if (a == 0) return b;
  if (b == 0) return a;
  return gcd(b, a % b);
}
int64_t power(int64_t a, int64_t b) {
  int64_t ans = 1;
  int64_t k = a;
  while (b > 0) {
    if (b & 1) {
      ans *= k;
      ans %= 1000000007;
    }
    k = (k * k) % 1000000007;
    b = b >> 1;
  }
  return ans;
}
int64_t add_mod(int64_t a, int64_t b, int64_t m) {
  return ((a % m) + (b % m)) % m;
}
int64_t sub_mod(int64_t a, int64_t b, int64_t m) {
  return ((a % m) - (b % m) + m) % m;
}
int64_t mul_mod(int64_t a, int64_t b, int64_t m) {
  return ((a % m) * (b % m)) % m;
}
void solve() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int *arr = new int[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    if (n == 1) {
      cout << "-1" << endl;
      continue;
    }
    int mini = INT_MAX;
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++) {
      if (m.find(arr[i]) != m.end()) {
        int diff = i - m[arr[i]] + 1;
        mini = min(diff, mini);
        m[arr[i]] = i;
      } else {
        m[arr[i]] = i;
      }
    }
    cout << mini << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
