#include <iostream>
#include <vector>
using namespace std;
bool cD(const vector<int>& x, const vector<int>& y) {
bool t[10] = {false};
bool o[10] = {false};
for (int d : x) t[d] = true;
for (int d : y) o[d] = true;
for (int d = 1; d <= 31; ++d) {
int a = d / 10;
int b = d % 10;
if (!t[a] || !o[b]) return false;
}
return true;
}
int main() {
int T;
cin >> T;
while (T--) {
int N, M;
cin >> N >> M;
vector<int> x(N), y(M);
for (int i = 0; i < N; ++i) cin >> x[i];
for (int i = 0; i < M; ++i) cin >> y[i];
if (cD(x, y)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}