import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
int m = sc.nextInt();
sc.nextLine();
String str = sc.nextLine();
// 模拟循环
String doubleStr = str + str;
char[] chars = doubleStr.toCharArray();
int ans = Integer.MAX_VALUE;
for (int i = 0; i < chars.length; i++) {
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] == chars[j]) {
ans = Math.min(ans, j - i - 1);
break;
}
}
}
if (ans >= m - 1) {
// 原本的字符串最远距离应该是m-2
System.out.println("-1");
} else {
System.out.println(ans);
}
}
}
}