#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
inline long long max3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int>> v2;
    for (long long int(i) = 0; (i) < (n); (i)++) {
      long long int temp;
      cin >> temp;
      v2.push_back(make_pair(temp, i + 1));
    }
    long long int flag = 0;
    long long int p = v2.size();
    long long int mini = 1e18;
    sort(v2.begin(), v2.end());
    for (long long int(i) = 0; (i) < (p - 1); (i)++) {
      if (v2[i].first == v2[i + 1].first) {
        flag = 1;
        long long int r = llabs(v2[i].second - v2[i + 1].second);
        mini = ((mini > r) ? r : mini);
      }
    }
    if (flag != 1 || n == 1)
      cout << "-1" << endl;
    else
      cout << mini + 1 << endl;
  }
  return 0;
}
