#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }
    bool b = 0;
    for (auto x : mp) {
      if (x.second > 1) {
        b = 1;
        break;
      }
    }
    if (!b) {
      cout << -1 << "\n";
    } else {
      int minn = n;
      bool ch = 0;
      for (int i = 0; i < n - 2; i++) {
        for (int j = i + 1; j < min(n, i + 1 + minn); j++) {
          if (a[i] == a[j]) {
            minn = min(minn, j - i + 1);
            if (minn == 2) {
              ch = 1;
              break;
            }
          }
        }
        if (ch) {
          break;
        }
      }
      cout << minn << "\n";
    }
  }
}
