/*
* Copyright (c) 2025 Emon Thakur
* All rights reserved.
*/
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);
void pbdserase(pbds &t, int v)
{
//normal erase function doesnt work in ordered multiset but this works
int rank = t.order_of_key(v);
auto it = t.find_by_order(rank);
t.erase(it);
}
void solve(int tc)
{
//cout<<"Case "<<tc<<": ";
int n,m; cin >> n >> m;
set<int> s1,s2;
int x;
for(int i=0;i<n;i++) cin>>x, s1.insert(x);
for(int i=0;i<m;i++) cin>>x, s2.insert(x);
for(int i=0;i<=3;i++)
{
if(s1.find(i)==s1.end() || s2.size()<10){
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
}
int main()
{
life_is_a_race
int t=1;
cin>>t;
for(int i=1;i<=t;i++) solve(i);
}