Problem: 2061D - Kevin and Numbers
Language: c++17 

Solution 1 (runtime: 577 ms):
#include <bits/stdc++.h>
#define ll long long
#define ui unsigned int
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define rforn(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define fll(v, x) for (int i = 0; i < (int)v.size(); i++) v[i] = x;
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
//#define __DEBUG
using namespace std;
int main() {
	#ifndef __DEBUG
    ios::sync_with_stdio(false);
    cin.tie(0);
    #endif
    int t;
    cin >> t;
    while (t--) {
		int n, m;
		cin >> n >> m;
		multiset<int> a, b;
		int x;
		forn(i, n) {
			cin >> x;
			a.insert(x);
		}
		forn(i, m) {
			cin >> x;
			b.insert(x);
		}
		bool ok = true;
		for (auto it = b.begin(); it != b.end() && ok; ++it) {
			multiset<int> tmp;
			tmp.insert(*it);
			while (tmp.size() > 0) {
				multiset<int> tmp2;
				bool f = false;
				for (auto x : tmp) {
					auto it2 = a.find(x); 
					if (it2 == a.end()) {
						if (x > 0) {
							tmp2.insert(x / 2);
							tmp2.insert((x + 1) / 2);
						} else {
							f = true;
							break;
						}
					} else {
						a.erase(it2);
					}
				}
				tmp = tmp2;
				if (f || tmp.size() > a.size()) {
					ok = false;
					break;
				}
			}
		}
		cout << (ok && a.size() == 0 ? "Yes" : "No") << "\n";
	}
}


Solution 2 (runtime: 796 ms):
// Problem: D. Кевин и числа
// Contest: Codeforces - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2061/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <utility>
#include <limits>
#include <map>
#include <queue>
 
using namespace std;
 
#define ll long long
#define F first
#define S second
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define Yes cout << "Yes\n"
#define No cout << "No\n"
#define yes cout << "yes\n"
#define no cout << "no\n"
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fix(a) fixed << setprecision (a)
#define pb(a) push_back(a)
#define mp(a, b) make_pair (a, b)

ll gcd (ll a, ll b) {
	if (b == 0)
		return a;
	return gcd (b, a % b);
}

ll lcm (ll a, ll b) {
	return a * b / gcd (a, b);
}

ll binpow (ll a, ll n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow (a, n - 1) * a;
	else {
		ll b = binpow (a, n / 2);
		return b * b;
	}
}

bool isPrime(ll num) {
    if (num <= 1) {
        return false;
    }
    if (num <= 3) {
        return true;
    }
    if (num % 2 == 0 || num % 3 == 0) {
        return false;
    }
    for (ll i = 5; i * i <= num; i += 6) {
        if (num % i == 0 || num % (i + 2) == 0) {
            return false;
        }
    }
    return true;
}

ll modInverse(ll A, ll M)
{
    ll m0 = M;
    ll y = 0, x = 1;

    if (M == 1)
        return 0;

    while (A > 1) {
        // q is quotient
        ll q = A / M;
        ll t = M;

        // m is remainder now, process same as
        // Euclid's algo
        M = A % M, A = t;
        t = y;

        // Update y and x
        y = x - q * y;
        x = t;
    }

    // Make x positive
    if (x < 0)
        x += m0;

    return x;
}

void solve () {
	ll n, m;
	cin >> n >> m;
	set <ll> a;
	set <ll> b;
	map <ll,ll> mpa, mpb;
	for (ll i = 0; i < n; i++) {
		ll v;
		cin >> v;
		a.insert (v);
		mpa[v]++;
	}
	for (ll i = 0; i < m; i++) {
		ll v;
		cin >> v;
		b.insert (v);
		mpb[v]++;
	}
	while (1) {
		if (a.size() == 0 or b.size() == 0) {
			break;
		}
		auto itb = b.rbegin();
		auto ita = a.rbegin();
		if (*itb == *ita) {
			if (mpb[*itb] == mpa[*ita]) {
				mpb.erase (*itb);
				mpa.erase (*ita);
				b.erase (*itb);
				a.erase (*ita);
			} else if (mpb[*itb] < mpa[*ita]) {
				NO;
				return;
			} else {
				mpb[*itb] -= mpa[*ita];
				mpa.erase (*ita);
				a.erase (*ita);
			}
		} else if (*itb < *ita) {
			NO;
			return;
		} else if (*itb > *ita) {
			ll v1 = *itb / 2;
			ll v2 = *itb - v1;
			mpb[v1] += mpb[*itb];
			mpb[v2] += mpb[*itb];
			mpb.erase (*itb);
			b.erase (*itb);
			b.insert (v1);
			b.insert (v2);
		}
	}
	if (a.size() != b.size()) {
		NO;
	} else {
		auto ita = a.begin();
		auto itb = b.begin();
		while (ita != a.end()) {
			if (mpa[*ita] != mpb[*itb]) {
				NO;
				return;
			}
		}
		YES;
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll tt = 1;
	cin >> tt;
	while (tt--)
		solve();
}