import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {

	
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(new File("DEBRUIJN.IN"));
		int T = in.nextInt();
		for(int t=0;t<T;t++){
			int K = in.nextInt();
			String s=in.next();
			int [][]m=new int[100100][100100];
			int []n = new int[100100];
			String pref="";
			int j=0;
			Map<String, Integer> map = new HashMap<String, Integer>();
			for(int i=0;i<=s.length()-K;i++){
				String a = s.substring(i,i+K);
				int pi;
				if(!map.containsKey(a)){
					map.put(a,j);
					pi=j;
					j++;
				}else{
					pi=map.get(a).intValue();
				}				
				if(!pref.equals("")){
					int x = map.get(pref).intValue();
					int y = pi;
					if(m[x][y]==0){
						n[x]++;
					}
					m[x][y]=2;					
				}
				pref=a;
			}
			int count=0;
			for(int i=0;i<100100;i++){
				if(n[i]>1){
					count++;
				}
			}
			System.out.println(count);
		}
		
	}
}
