// Created on: 2025-01-02 20:40
// Author: Safwan_Ibrahim
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
void solve() {
int n, m; cin >> n >> m;
set<int>a, b;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
a.insert(x);
}
for (int i = 1; i <= m; i++) {
int x; cin >> x;
b.insert(x);
}
vector<int>a2(a.begin(), a.end());
vector<int>b2(b.begin(), b.end());
set<int>check;
for (int i = 1; i <= 31; i++) {
check.insert(i);
}
vector<int>ans;
for (int i = 0; i < a2.size(); i++) {
for (int j = 0; j < b2.size(); j++) {
int num1 = a2[i] * 10 + b2[j], num2 = b2[j] * 10 + a2[i];
if (check.find(num1) != check.end()) {
check.erase(num1);
}
if (check.find(num2) != check.end()) {
check.erase(num2);
}
}
}
cout << (check.size() == 0 ? "YES\n" : "NO\n");
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}