Please provide me hint to solve this question( question link in description )

https://mycode.prepbytes.com/problems/strings/AMANSTR

My code for this problem :-

#include<iostream>
using namespace std; 
#include<bits/stdc++.h>
int main()
{
    
    int t ; cin>>t ;
    for(int i=0;i<t;i++)
    {
        string str ; cin>>str ;
        int curr=0;  int ans=0 ;
        for(int j=0;j<str.length();j++)
        {   //cout<<"j is "<<j<<" "<<str[j]<<endl;
            curr=0;
            if(str[j]=='a')
                curr=1;
            for(int k=j+1;k<str.length();k++)
            {
                if(curr==0&&str[k]=='a')
                    curr=1; 
                else if(curr==1&&str[k]=='m')
                    curr=2;
                else if(curr==2&&str[k]=='a')
                    curr=3; 
                else if(curr==3&&str[k]=='n')
                    curr=4; 
                else if(str[k]=='a')
                    curr=1;
                else 
                    curr=0;
                if(curr==4) {ans+=str.length()-k;break;}; 
                
                //cout<<"j is "<<j<<"  "<<"k is "<<str[k]<<endl;
                //cout<<"curr is "<<curr<<endl;
            
                
            }
            //cout<<"ans is "<<ans<<endl;
            
        }
        cout<<ans<<endl;
    }
    
}

Did you check if the variable ans overflows?