#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 200;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int mini = 1000000;
    int a[n], last[n], ans[n];
    bool check[n], mark[n];
    fill(last, last + n, 0);
    fill(check, check + n, false);
    fill(ans, ans + n, 0);
    fill(mark, mark + n, false);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
      if (!check[a[i]]) {
        last[a[i]] = i + 1;
        check[a[i]] = true;
      } else {
        ans[a[i]] = i + 1 - last[a[i]] + 1;
        last[a[i]] = i + 1;
        mark[a[i]] = true;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (mark[a[i]]) mini = min(mini, ans[a[i]]);
    }
    if (mini == 1000000) {
      cout << -1;
    } else {
      cout << mini;
    }
    cout << endl;
  }
}
