Wrong answer

Can anyone help me out for a test case where my program is giving the “wrong answer”?

var arr = '';
process.stdin.on('data',function(chunk){
   arr += chunk; 
});
process.stdin.on('end',function(){
   arr = arr.split('\n');
   let t = parseInt(arr[0]),line=1,i,n,k,nk,str,first,second,sub,regex,ctr,l;
   while(t--){
        nk = arr[line++].split(' ');
        n = parseInt(nk[0]);
        k = parseInt(nk[1]);
        str = arr[line++];
        first='';second='';sub='';ctr=0;
        for(i=0;i<n;i++){
            if(str[i] == 'R'){
                j = Math.min(n,(i+k-1));
                sub = str.substr(0,i);
                if(i===0) first = str.substr(i,j+1);
                else first = str.substr(i,j);
                second = str.substr(j+1,n);
                regex = new RegExp('R','g');
                first = first.replace(regex,'L');
                regex = new RegExp('G','g');
                first = first.replace(regex,'R');
                regex = new RegExp('L','g');
                first = first.replace(regex,'G');
                str = sub+first+second;
                ctr++;
            }
        }
        console.log(ctr);
    }
});