WA , what am i doing wrong in logic?

Problem link : CodeChef
Submission link : CodeChef

(Only 2 testcases are passed 4 shows wrong answer)

//my code
#include<bits/stdc++.h>

#define fo(a,b) for(int i=a;i<b;i++)
#define printArr(arr,start,end) for(int i=start;i<end;i++) cout<<arr[i]<<" ";

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin>>t;
while(t--)
{   
	int n,m;
	bool flag=0;
	cin>>n>>m;
	multiset<int>odd;
	multiset<int>even;
	for(int i=0;i<n;i++)
	{
	    for(int j=0;j<m;j++)
	    {
	        int x;
	        cin>>x;
	        if((i+j)%2==0)
	            even.insert(x);
            else
                odd.insert(x);
	    }
	}
	for(int i=0;i<n;i++)
	{
	    for(int j=0;j<m;j++)
	    {
	        int x;
	        cin>>x;
	        if((i+j)%2==0)
	        {
	            if(even.find(x)!=even.end())
	            {
	                even.erase(even.find(x));
	            }
	            else
	            {
	                flag=1;
	            }
	        }
            else
            {
                if(odd.find(x)!=odd.end())
	            {
	                odd.erase(odd.find(x));
	            }
	            else
	            {
	                flag=1;
	            }
            }
	    }
	}
	if(!odd.empty()||!even.empty())
	    flag=1;
	    
    if(flag)
        cout<<"no\n";
    else
        cout<<"yes\n";
	
}

}