ASTRING - Editorial

int main()
{char a[100005],b[100005];
int t,i,m;
scanf("%d",&t);
for(m=0;m<t;m++)
{int k,len;
scanf("%s",a);
scanf("%d",&k);
len=strlen(a);
int p=len-k;
int j=0;
for(i=0;i<len;i++)
{
while(j!=0 && b[j-1]>a[i] && p!=0)
{
j–;
p–;
}
b[j]=a[i];
j++;
}
b[k]=’\0’;
printf("%s\n",b);

}
return 0;
}

I just sorted the string and took the substring from 0 to k. But it shows wrong answer. Can someone please highlight the mistake in my code.

int main()
{

int t;
cin>>t;
while(t--)
{
    string s;
    cin>>s;
    int k;
    cin>>k;
    sort(s.begin(),s.end());
    string s1=s.substr(0,k);
    cout<<s1<<endl;

}

}