//sadias
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
int n, m;
while(t--){
cin >> n >> m;
int ar[n], arr[m];
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, y = 0, o = 0;
for(int i = 0; i < n; i++){
cin >> ar[i];
if(ar[i] == 1) a++;
if(ar[i] == 2) b++;
if(ar[i] == 3) c++;
if(ar[i] == 4) d++;
if(ar[i] == 5) e++;
if(ar[i] == 6) f++;
if(ar[i] == 7) g++;
if(ar[i] == 8) h++;
if(ar[i] == 9) y++;
if(ar[i] == 0) o++;
}
bool check = false;
if(a == 0 || b == 0 || c == 0 || d == 0 || e == 0 || f == 0 || g == 0 || h == 0 || y == 0 || o == 0){
check = true;
}
a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, y = 0, o = 0;
for(int i = 0; i < m; i++){
cin >> arr[i];
if(arr[i] == 1) a++;
if(arr[i] == 2) b++;
if(arr[i] == 3) c++;
if(arr[i] == 4) d++;
if(arr[i] == 5) e++;
if(arr[i] == 6) f++;
if(arr[i] == 7) g++;
if(arr[i] == 8) h++;
if(arr[i] == 9) y++;
if(arr[i] == 0) o++;
}
if(a == 0 || b == 0 || c == 0 || d == 0 || e == 0 || f == 0 || g == 0 || h == 0 || y == 0 || o == 0){
check = true;
}
if(check) cout << "NO\n";
else cout << "YES\n";
}
return 0;
}