whats wrong in my code

Question code: SHKSTR

My code:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

struct str
{

string s1;

int index;

};

int fun(str x,str y)
{

if(x.s1.compare(y.s1)<0)
return 1;
else
return 0;

}

int main()
{

int n;
cin>>n;
string a[n];
str s[n];
unordered_map<string,vector<int>> m;

for(int i=0;i<n;i++){

    cin>>s[i].s1;
    a[i]=s[i].s1;
    s[i].index=i;

}

sort(s,s+n,fun);

for(int i=0;i<n;i++){

    for(int j=0;j<s[i].s1.length();j++){

        m[s[i].s1.substr(0,j+1)].push_back(s[i].index);

    }

}

int q;

cin>>q;

while(q--)
{
    int r,f,ctr=0,t=0;

    cin>>r;

    string p;

    cin>>p;

    for(int i=p.length();i>=0;i--){

        f=1;

        ctr=0;

        if(m.find(p.substr(0,i)) != m.end()){

            for(int j=0;j<m[p.substr(0,i)].size();j++){

                if(m[p.substr(0,i)][j]<r){

                    f=0;

                    cout<<a[m[p.substr(0,i)][j]]<<endl;

                    break;

                }

            }

        }

        if(f==0)
        break;
    }

    if(f==1)
    cout<<a[0]<<endl;

}

}