Need help with NAME2

#include
#include
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	{
		string m, w;
		cin >> m >> w;
		string out="";
		if(m.length() == w.length())
		{
			if(m.compare(w)==0)
				cout << "YES" << "\n";
			else
				cout << "NO" << "\n";
		}
		else
		{
			bool flag=false;
			if(m.length() > w.length())
			{
				for(int i=0;i<m.length();i++)
				{
					if(w.find(m.at(i)) != -1)
					{
						out+=m.at(i);
						if(out.compare(w) == 0)
						{
							flag=true;
							break;
						}
					}
				}
			}
			else
			{
				for(int i=0;i<w.length();i++)
				{
					if(m.find(w.at(i)) != -1)
					{
						out+=w.at(i);
						if(out.compare(m) == 0)
						{
							flag=true;
							break;
						}
					}
				}
			}
			if(flag)
				cout << "YES" << "\n";
			else
				cout<< "NO" << "\n";
		}
	}
	return 0;
}

Program passes test cases, and even a few others like reversing the order, etc. (my test cases). Any suggestions as to what I’m missing out? Getting WA always.