#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } void slv() { int n,m; cin >> n >> m; vector a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < m; ++i) { cin >> b[i]; } if (m == 0) { cout << "Yes" << "\n"; for (int i = 0; i < n; ++i) { cout << "Red" << " " << a[i] << "\n"; } return; } if (n == 0) { cout << "Yes" << "\n"; for (int i = 0; i < m; ++i) { cout << "Blue" << " " << b[i] << "\n"; } return; } vector,int>> v; vector R,B; { map mp1,mp2; vector z; for (int i = 0; i < n; ++i) { mp1[a[i]]++; z.push_back(a[i]); } for (int i = 0; i < m; ++i) { mp2[b[i]]++; z.push_back(b[i]); } sort(z.begin(), z.end()); z.erase(unique(z.begin(), z.end()), z.end()); for (int i : z) { int u = mp1[i], uu = mp2[i]; while (abs(u-uu) > 1) { if (u > uu) { R.push_back(i); u--; } else { B.push_back(i); uu--; } } if (min(u,uu) == 0) { if (uu > 0) B.push_back(i); else if (u > 0) R.push_back(i); } else { v.push_back({{u,uu}, i}); } } } if (v.size() == 0) { cout << "No" << "\n"; return; } vector> rr,bb,rb; for (int i = 0; i < v.size(); ++i) { if (v[i].first.first > v[i].first.second) { rr.push_back({v[i].first.second, v[i].second}); } else if (v[i].first.first == v[i].first.second) { rb.push_back({v[i].first.second, v[i].second}); } else { bb.push_back({v[i].first.first, v[i].second}); } } if (rb.size() % 2 == 1) { for (int i : R) { cout << "Red " << i << "\n"; } for (auto p: rr) { cout << "Red " << p.second << "\n"; for (int j = 0; j < p.first; ++j) { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } int t = 0; for (auto p: rb) { for (int j = 0; j < p.first; ++j) { if (t == 0) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } else { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } t ^= 1; } for (auto p: bb) { cout << "Blue " << p.second << "\n"; for (int j = 0; j < p.first; ++j) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } } for (int i : B) { cout << "Blue " << i << "\n"; } } else if (rr.size() + R.size() == 0) { for (int i : B) { cout << "Blue " << i << "\n"; } for (auto p: bb) { cout << "Blue " << p.second << "\n"; for (int j = 0; j < p.first; ++j) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } } int t = 1; for (auto p: rb) { for (int j = 0; j < p.first; ++j) { if (t == 0) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } else { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } t ^= 1; } } else if (B.size() + bb.size() == 0) { for (int i : R) { cout << "Red " << i << "\n"; } for (auto p: rr) { cout << "Red " << p.second << "\n"; for (int j = 0; j < p.first; ++j) { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } int t = 0; for (auto p: rb) { for (int j = 0; j < p.first; ++j) { if (t == 0) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } else { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } t ^= 1; } } else { if (rb.size()) { auto p = rb.back(); rb.pop_back(); p.first--; B.push_back(p.second); rr.push_back(p); } else if (rr.size()) { auto p = rr.back(); rr.pop_back(); rb.push_back(p); R.push_back(p.second); } else { auto p = bb.back(); bb.pop_back(); rb.push_back(p); B.push_back(p.second); } for (int i : R) { cout << "Red " << i << "\n"; } for (auto p: rr) { cout << "Red " << p.second << "\n"; for (int j = 0; j < p.first; ++j) { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } int t = 0; for (auto p: rb) { for (int j = 0; j < p.first; ++j) { if (t == 0) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } else { cout << "Blue " << p.second << "\n"; cout << "Red " << p.second << "\n"; } } t ^= 1; } for (auto p: bb) { cout << "Blue " << p.second << "\n"; for (int j = 0; j < p.first; ++j) { cout << "Red " << p.second << "\n"; cout << "Blue " << p.second << "\n"; } } for (int i : B) { cout << "Blue " << i << "\n"; } } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int q; cin >> q; while (q--) { slv(); } }