Need help for solving this problem

https://www.codechef.com/JAN20B/problems/ENGLISH/

#include<bits/stdc++.h>
using namespace std;
#define f(i,j,k) for(ll i=j;i<k;i++)
typedef long long int ll;
string w[100005];
ll cc[100005];
#define mp(a,b) make_pair(a,b)

ll clc(string a,string b)
{
ll p=min(a.size(),b.size());ll k=0;
for(k=0;k<p;k++)
{

    if(a[k]!=b[k]) return k;
    }
return k;
}

ll stkfk(ll cc[],ll n)
{stack<pair<ll,bool>> stk;ll ans=0;
stk.push(mp(cc[0],0));ll idx=1;
while(idx<=n)
{ if(stk.size()==0) {stk.push(mp(0,0));stk.push(mp(cc[idx],1));idx++;continue;}
pair<ll,bool> t=stk.top();

    if(cc[idx]> t.first) {stk.push(mp(cc[idx],1));idx++;continue;};
    if(cc[idx]<t.first && !t.second) {stk.push(mp(cc[idx],0));idx++;continue;};
    if(cc[idx]==t.first && !t.second) {stk.push(mp(cc[idx],1));idx++;continue;};
    if(cc[idx]<=t.first && t.second)  {
        pair<ll,bool> curr={cc[idx],0};
        while( stk.size()>1 && curr.first<=stk.top().first && stk.top().second  )
        {   ans+=stk.top().first*stk.top().first;
            stk.pop();
            pair<ll,bool> scnd=stk.top();
        stk.pop();
        if(curr.first>scnd.first) {curr=scnd;};
        
        
        };
         if(stk.size()==0)     {stk.push(mp(0,0)); stk.push(mp(curr.first,1));}  // push zero then 1 bool
    else{    if(curr.first<stk.top().first) {stk.push(mp(curr.first,0));}
            else stk.push(mp(curr.first,1));     };
}

idx++;}

return ans;}

int main()
{int t;
cin>>t;
while(t–)
{ios::sync_with_stdio(0);cin.tie(NULL);
ll n;
cin>>n;
f(i,0,n) {cin>>w[i];};

sort(w,w+n);

// f(i,0,n) cout<<w[i]<<endl;

 cc[0]=0;
 
 
f(i,1,n)
{cc[i]=clc(w[i],w[i-1]);
    };
cc[n]=0;


cout<<stkfk(cc,n)<<"\n";


}
}

code is not properly oriented but i need to discuss the situation in which it is failing
*this is for palindromes only