#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n + 1];
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
    }
    vector<long long> v[n + 1];
    set<long long> s;
    for (long long j = 0; j < n; j++) {
      v[arr[j]].push_back(j + 1);
      s.insert(arr[j]);
    }
    vector<long long> m;
    for (auto y = s.begin(); y != s.end(); y++) {
      m.push_back(*y);
    }
    long long ans = INT_MAX;
    long long as;
    long long b[2000 + 1];
    memset(b, 0, sizeof(b));
    for (long long g = 0; g < long long(m.size()); g++) {
      as = INT_MAX;
      for (long long e = 1; e < long long(v[m[g]].size()); e++) {
        as = min(as, long long(v[m[g]][e] - v[m[g]][e - 1]));
      }
      if (ans == as && ans != INT_MAX) {
        cout << ans << "\n";
        b[ans] += 1;
      }
      ans = min(as, ans);
    }
    if (ans == INT_MAX) {
      cout << "-1"
           << "\n";
    } else if (ans != INT_MAX && b[ans] != 0) {
      cout << "-1"
           << "\n";
    } else {
      cout << ans + 1 << "\n";
    }
  }
}
