#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool canFormAllDates(int N, int M, int* list1, int* list2) {
bool formedDates[31] = {false};
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
int date = list1[i] * 10 + list2[j];
if (date >= 1 && date <= 31) {
formedDates[date-1] = true;
}
}
}
for (int i = 0; i < 31; i++) {
if (!formedDates[i]) {
return false;
}
}
return true;
}
int main() {
int T;
scanf("%d", &T);
char results[T][4]; // Array to store results (either "YES" or "NO")
for (int t = 0; t < T; t++) {
int N, M;
scanf("%d %d", &N, &M);
int list1[N], list2[M];
for (int i = 0; i < N; i++) {
scanf("%d", &list1[i]);
}
for (int i = 0; i < M; i++) {
scanf("%d", &list2[i]);
}
if (canFormAllDates(N, M, list1, list2)) {
strcpy(results[t], "YES");
} else {
strcpy(results[t], "NO");
}
}
// Print all results, each on a new line
for (int t = 0; t < T; t++) {
printf("%s\n", results[t]);
}
return 0;
}