//sadias
#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while(t--){
int n, m; cin >> n >> m;
set<int> st1, st2;
int num;
for(int i = 0; i < n; i++) {
cin >> num;
st1.insert(num);
}
for(int i = 0; i < m; i++) {
cin >> num;
st2.insert(num);
}
//first digit set must have values 0-3 and second digit set must have values (0-9)
bool check = false;
for(int i = 0; i <= 3; i++) {
if(st1.find(i) == st1.end() || st2.size() < 10) {
cout << "NO\n";
check = true;
break;
}
}
//After checking all conditions in the for loop if check != true then all ok
if(!check) cout << "YES\n";
}
return 0;
}