#include <bits/stdc++.h>
using namespace std;
bool ok(const vector<int>& l1, const vector<int>& l2)
{
unordered_set<int> d1(l1.begin(), l1.end());
unordered_set<int> d2(l2.begin(), l2.end());
for (int i = 1; i <= 31; ++i)
{
int tens = i / 10;
int units = i % 10;
if (d1.find(tens) == d1.end() || d2.find(units) == d2.end())
{
return false;
}
}
return true;
}
int main()
{
int t ; cin >> t ;
while(t--)
{
int n , m ; cin >> n >> m ;
vector<int> a(n) ,v(m);
for(int i=0;i<n;i++) cin >> a[i];
for(int i=0;i<m;i++) cin >> v[i] ;
if (ok(a,v))
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}