Can you please tell how to traverse?

use s.push_back() and s.pop_back() funtions

i have done some modification
you can see them in modified code below
Modified Code

Not able to pass the test cases

Modified Code

try now

#include<bits/stdc++.h>
using namespace std;

class TrieNodeDict{
public:
char ch;
map<char, TrieNodeDict *> m;
bool terminal;
TrieNodeDict(char c)
{
ch=c;
terminal=false;
}
};

class TrieDict
{
TrieNodeDict *root;
public:

TrieDict()
{
    root=new TrieNodeDict('\0');
}

void insert(char w[])
{
    TrieNodeDict *temp = root;
    for(int i=0;w[i]!='\0';i++)
    {   
        char ch=w[i];
        if(temp->m.count(ch))
            temp = temp->m[ch];
        else
        {
            TrieNodeDict* n = new TrieNodeDict(ch);
            temp->m[ch] = n;
            temp = n;
        }
    }
    temp->terminal = true;
}

void print(TrieNodeDict *temp, string s, vector<string> &v)
{
    if(temp->terminal)
    {
        v.push_back(s);
    }
    /////////////
    for(auto it:temp->m){
        s.push_back(it.first);
        print(it.second,s,v);
        s.pop_back();
    }

}

bool search(char w[], vector<string> &v)
{
    TrieNodeDict* temp = root;
    string s="";
    for(int i=0;w[i]!='\0';i++)
    {
        char ch = w[i];
        if(temp->m.count(ch)==0){
            insert(w);
            return false;
        }
        else
        {
            s.push_back(ch);
            temp = temp->m[ch];
        }
    }
    print(temp, s, v);
    return true;
}

};

int main() {
TrieDict t;
int n,q;
cin>>n;
// char words[1001][1001];
char word[1000000];
for(int i=0;i<n;i++)
{

    cin>>word;
    t.insert(word);
}
cin>>q;
char sword[1001];
for(int i=0;i<q;i++)
{
    cin>>sword;
    vector<string> v;
    if(!t.search(sword, v))
    {
        cout<<"No suggestions\n";
        continue;
    }
    sort(v.begin(),v.end());
    for(auto x:v)
        cout<<x<<endl;
    v.clear();
}
return 0;

}

Here why are we doing insert(w) if the query fails

it is mention in the question

at last line it is mention
And if no such words are available for a given search word, add this word to your dictionary.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.