#include <stdio.h>
int contains(int arr[], int size, int value) {
for (int i = 0; i < size; i++) {
if (arr[i] == value) {
return 1;
}
}
return 0;
}
int can_form_all_dates(int X[], int N, int Y[], int M) {
for (int date = 1; date <= 31; date++) {
int tens = date / 10;
int ones = date % 10;
if (!contains(X, N, tens) || !contains(Y, M, ones)) {
return 0;
}
}
return 1;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N, M;
scanf("%d %d", &N, &M);
int X[N], Y[M];
for (int i = 0; i < N; i++) {
scanf("%d", &X[i]);
}
for (int i = 0; i < M; i++) {
scanf("%d", &Y[i]);
}
if (can_form_all_dates(X, N, Y, M)) {
printf("YES\n\n");
} else {
printf("NO\n\n");
}
}
return 0;
}