#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, tmp;
    cin >> n;
    unordered_map<int, int[2]> map;
    bool fin = false;
    int min = 1000000;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      if (!fin) {
        if (map.find(tmp) == map.end()) {
          map[tmp][0] = -1;
          map[tmp][1] = -1;
        }
        if (map[tmp][0] == -1)
          map[tmp][0] = i;
        else if (map[tmp][1] == -1)
          map[tmp][1] = i;
        else {
          map[tmp][0] = map[tmp][1];
          map[tmp][1] = i;
        }
        if (map[tmp][0] != -1 && map[tmp][1] != -1 &&
            map[tmp][1] - map[tmp][0] < min)
          min = map[tmp][1] - map[tmp][0];
        if (min == 1) fin = true;
      }
    }
    if (min == 1000000)
      min = -1;
    else
      min++;
    printf("%d\n", min);
  }
  return 0;
}
