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

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4 5\n",
      "10 8 6 4\n",
      "1 2 3 4 5\n",
      "1 4 "
     ]
    }
   ],
   "source": [
    "t = int(input())\n",
    "\n",
    "for _ in range(t):\n",
    "    n, m = map(int, input().split())\n",
    "    a = list(map(int, input().split()))\n",
    "    b = list(map(int, input().split()))\n",
    "    c = []\n",
    "    for i in a:\n",
    "        if i in b:\n",
    "            c.append(i)\n",
    "    for i in b:\n",
    "        if i in a and i not in c:\n",
    "            c.append(i)\n",
    "print(len(c), end = ' ')\n",
    "for i in c:\n",
    "    print(i, end = ' ')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
