#include <bits/stdc++.h>
using namespace std;
set<int> st;
int main() {
  int t, n, x;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    scanf("%d", &n);
    map<int, int> tmp;
    map<int, int> minpos;
    map<int, int> post;
    map<int, int> pos;
    map<int, int> occ;
    for (int i = 0; i < n; i++) {
      scanf("%d", &x);
      st.insert(x);
      occ[x]++;
      if (occ[x] == 1) {
        tmp[x] = occ[x];
      }
      if (occ[x] == 1) {
        minpos[x] = i;
        post[x] = i;
      }
      if (occ[x] != tmp[x] && tmp[x] > 0) {
        if (occ[x] == 2) {
          minpos[x] = i + 1;
        }
        if (minpos[x] > abs(post[x] - (i + 1))) {
          minpos[x] = abs(post[x] - (i + 1));
        }
      }
      if (occ[x] > 1) {
        tmp[x] = occ[x];
        post[x] = i;
      }
    }
    int item, dist, tmpp, tmpp2;
    int cnt = 0, cnt2 = 0;
    for (auto k : st) {
      if (cnt == 0) {
        item = k, dist = minpos[k];
        tmpp = dist;
      }
      if (occ[k] > 1) {
        if (cnt2 == 0) {
          tmpp = minpos[k], item = k, dist = minpos[k];
          cnt2++;
        }
        if (tmpp > minpos[k]) {
          tmpp = minpos[k], item = k, dist = minpos[k];
          cnt2++;
        }
      }
      cnt++;
    }
    if (cnt2 == 0) {
      printf("-1\n");
    } else if (cnt2 > 0) {
      printf("%d\n", dist);
    }
  }
  return 0;
}
