1
2 2
1 1
1 2
 1
1 3
3
1 2 3
 1
1 1
1000
1000
 1
2 2
2 2
2 2
 5
4 5
10 8 6 4
1 2 3 4 5
1 1
3
3
1 1
3
2
5 3
1000 2 2 2 3
3 1 5
5 5
1 2 3 4 5
1 2 3 4 5
 1
4 4
1 1 1 1
1 2 3 4
 1
2 3
1 1
1 2 3

YES
1 1
 YES
1 3
 YES
1 1000
 YES
1 2
 YES
1 4
YES
1 3
NO
YES
1 3
YES
1 1
 YES
1 1
 YES
1 1

#include <bits/stdc++.h>
using namespace std;
void fun(int *a, int *b, int n, int t) {
  int arr[n + 1][t + 1];
  int i, j;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= t; j++) {
      if (i == 0 || j == 0) {
        arr[i][j] = 0;
      } else {
        if (a[i - 1] == b[j - 1]) {
          arr[i][j] = arr[i - 1][j - 1] + 1;
        } else {
          arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
        }
      }
    }
  }
  int in[n][t];
  int f[arr[n][t] + 1];
  f[arr[n][t]] = 0;
  int g = arr[n][t] - 1;
  int x, y;
  x = n;
  y = t;
  while (x > 0 && y > 0) {
    if (a[x - 1] == b[y - 1]) {
      f[g] = a[x - 1];
      x--;
      y--;
      g--;
    } else {
      if (arr[x - 1][y] > arr[x][j - 1]) {
        x--;
      } else {
        y--;
      }
    }
  }
  if (arr[n][t] == 0)
    cout << "NO";
  else {
    cout << "YES\n";
    cout << arr[n][t] << " ";
    for (int h = 0; h < arr[n][t]; h++) {
      cout << f[h] << " ";
    }
  }
  cout << "\n";
}
int main() {
  int l;
  cin >> l;
  while (l--) {
    int n, t;
    cin >> n >> t;
    int a[n], b[t];
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < t; i++) cin >> b[i];
    fun(a, b, n, t);
  }
}
