Help me in solving MXFACS problem

My issue

time limit exceeded. What to do?

My code

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

int main() {
	// your code goes here
int t,n,m,c=0,max=0,p;
cin>>t;
while(t--){
    cin>>n;
    for(int i =2;i<=n;i++){
        if(n%i==0){
     m=n/i;
        for(int j=1;j<=m;j++){
            if(m%j==0){
                c++;
            }
        }
        if(c>max){
            max=c;
            p=i;
        }}
        c=0;
    }
   cout<<p<<endl;
}
}

Learning course: 2000 to 2500 difficulty problems
Problem Link: Maximum Factors Problem Practice Problem in 2000 to 2500 difficulty problems - CodeChef

@aniket_4481
bro its not the right approach
U have to count the prime factors of each number .
and then print the shortest primefactor having maximum frequency.
plzz refer my c++ code for better understanding

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

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        map<int,int> mp;
        while(n%2==0)
        {
            n=n/2;
            mp[2]++;
        }
        for(int i=3;i<=sqrt(n);i+=2)
        {
            if(n%i==0)
            {
                while(n%i==0)
                {
                    n=n/i;
                    mp[i]++;
                }
            }
        }
        if(n>2)
        mp[n]++;
        int mx=0;
        for(auto x:mp)
        {
            mx=max(mx,x.second);
        }
        for(auto x:mp)
        {
            if(x.second==mx)
            {
                cout<<x.first<<endl;
                break;
            }
        }
    }
}

N is 10^9, you can’t loop around N