#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--){
int n, m, x, y = 0, z = 0, f = 0;
cin >> n >> m;
vector<int> a(10), b(10);
for (int i = 0; i < n; i++){
cin >> x;
a[x] = 1;
}
for (int i = 0; i < m; i++){
cin >> x;
b[x] = 1;
}
y = z = 0;
for (int i = 0; i < 10; i++){
y += b[i];
if (i < 4) z += a[i];
}
if (y == 10 && z == 4) f = 1;
cout << (f? "YES" : "NO") << endl;
}
}