Problem: 2061D - Kevin and Numbers
Language: java

Solution 1 (runtime: 609 ms):
import java.io.*;
import java.util.*;
import java.util.function.Predicate;
public class P3 {
    static void solve() {
        int n = sc.nextInt();
        int m = sc.nextInt();
        long arr[]=sc.nextLongArray(n);
        long brr[]=sc.nextLongArray(m);
        Arrays.sort(brr);
        HashMap<Long,Integer> map=new HashMap<>();
        for(int i=0;i<n;i++)
            map.merge(arr[i],1,Integer::sum);
        String res="Yes";
        for(int i=0;i<m;i++){
            if(!poss(brr[i],map)){
                res="No";
                break;
            }
        }
        for(Long k:map.keySet()){
            if(map.get(k)>0){
                res="No";
                break;
            }
        }
        out.print(res);
    }
        public static boolean poss(long val,HashMap<Long,Integer> map){
            if(map.getOrDefault(val,0)>0){
                map.merge(val,-1,Integer::sum);
                return true;
            }
            else if(val==1)
                return false;
            else
                return poss((val+1)/2, map) && poss(val/2, map);
            
        }
       public static void main(String[] args) {
        out.preCompute(false, false, false, false, out.E7, out.MOD);
        int t = 1;
        t = sc.nextInt(); 
        out.res.setLength(0);
        while (t-- > 0) {
            solve();
            out.nline();
        }
        System.out.print(out.res);
    }

    // Utility Object Initialization
    static Mix out = new Mix(); // For CP utility methods or print anything
    static Scanner sc = new Scanner(); // for input anything
    // static Graph graph = new Graph(); // for graph utility methods

    // Utility Object Declaration

    // static SegmentTree sg;
    // static Dsu dsu;

    static class Pair {
        public int x, y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public boolean equals(Object obj) {
            return obj instanceof Pair && ((Pair) obj).x == x && ((Pair) obj).y == y;
        }

        @Override
        public String toString() {
            return x + " " + y;
        }
    }

    static class Trie {
        private TrieNode root;
    
        public Trie() {
            root = new TrieNode();
        }

        public void insertAll(String[] words){
            for (String word : words) {
                insert(word);
            }
        }
        public void insertAll(List<String> words){
            for (String word : words) {
                insert(word);
            }
        }
        
        public void insert(String word) {
            TrieNode trieNode = root;
            for(char c: word.toCharArray()){
                if(!trieNode.contains(c)){
                    trieNode.put(c, new TrieNode());
                }
                trieNode = trieNode.get(c);
            }
            trieNode.isEnd=true;
        }
        
        public boolean search(String word) {
            TrieNode trieNode = root;
            for(char c: word.toCharArray()){
                trieNode = trieNode.get(c);
                if(trieNode==null)return false;
            }
            return trieNode.isEnd;
        }
        
        public boolean startsWith(String prefix) {
            TrieNode trieNode = root;
            for(char c: prefix.toCharArray()){
                trieNode = trieNode.get(c);
                if(trieNode==null)return false;
            }
            return true;
        }

        class TrieNode{
            TrieNode[] child;
            boolean isEnd;
            public TrieNode(){
                child = new TrieNode[26];
                isEnd=false;
            }
        
            public boolean contains(char c){
                return child[c-'a'] != null;
            }
        
            public void put(char c, TrieNode node){
                child[c-'a']=node;
            }
        
            public TrieNode get(char c){
                return child[c-'a'];
            }
        }    
    }
        

    static class Scanner {
        BufferedReader br;
        StringTokenizer st;

        public Scanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        double[] nextDoubleArray(int n) {
            double[] arr = new double[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextDouble();
            }
            return arr;
        }

        char[] nextToCharArray() {
            return next().toCharArray();
        }

        char[] nextCharArray(int n) {
            char[] arr = new char[n];
            for (int i = 0; i < n; i++) {
                arr[i] = next().charAt(0);
            }
            return arr;
        }

        int[][] nextIntMatrix(int n, int m) {
            int[][] arr = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = nextInt();
                }
            }
            return arr;
        }

        long[][] nextLongMatrix(int n, int m) {
            long[][] arr = new long[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = nextLong();
                }
            }
            return arr;
        }

        double[][] nextDoubleMatrix(int n, int m) {
            double[][] arr = new double[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = nextDouble();
                }
            }
            return arr;
        }

        char[][] nextCharMatrix(int n, int m) {
            char[][] arr = new char[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = next().charAt(0);
                }
            }
            return arr;
        }

        char[][] nextCharToMatrix(int n, int m) {
            char[][] arr = new char[n][m];
            for (int i = 0; i < n; i++) {
                arr[i] = next().toCharArray();
            }
            return arr;
        }

    }

    static class MultiSet<E> {
        private final TreeMap<E, Long> map;
        private int size;

        public MultiSet() {
            map = new TreeMap<>();
        }

        public MultiSet(Comparator<? super E> comparator) {
            map = new TreeMap<>(comparator);
        }

        public void add(E ele) {
            map.merge(ele, 1L, Long::sum);
            ++size;
        }

        public void remove(E ele) {
            if (!map.containsKey(ele))
                return;

            if (map.get(ele) == 1L)
                map.remove(ele);
            else
                map.merge(ele, -1L, Long::sum);

            --size;
        }

        public E lower(E ele) {
            return map.lowerKey(ele);
        }

        public E ceiling(E ele) {
            return map.ceilingKey(ele);
        }

        public E floor(E ele) {
            return map.floorKey(ele);
        }

        public E higher(E ele) {
            return map.higherKey(ele);
        }

        public E first() {
            return map.firstKey();
        }

        public E last() {
            return map.lastKey();
        }

        public int size() {
            return size;
        }

        public boolean isEmpty() {
            return size==0;
        }

        public boolean contains(Object o) {
            return map.containsKey(o);
        }

        public void clear() {
            size=0;
            map.clear();
        }

        public E pollFirst() {
            E first=first();
            this.remove(first);
            return first;
        }

        public E pollLast() {
            E last=last();
            this.remove(last);
            return last;
        }

        @Override
        public String toString() {
            return map.toString();
        }
    }
    

    static class Mix {

        public int E5 = (int) 1e5;
        public int E6 = (int) 1e6;
        public int E7 = (int) 1e7;
        public int E9 = (int) 1e9;
        public long MOD = (long) 1e9 + 7;
        public StringBuilder res = new StringBuilder();

        /*----------------------- */
        public List<Integer> primeList;
        public boolean[] primes;
        public long[] inverseNumbers;
        public long[] inverseFactorials;
        public long[] factorials;

        /**
         * Use this for debugging
         * Pass any number of argument and any type of argument
         */
        public void debug(Object... obj) {
            System.out.println(Arrays.deepToString(obj));
        }

        public void preCompute(boolean primes, boolean primesList, boolean factorials, boolean inverseFactorials, int n, long mod) {
            if(primes)sieve(n);
            if(primesList)getPrimes(n);
            if(factorials)factorials(n, mod);
            if(inverseFactorials)inverseFactorials(n, mod);
        }

        public long nCr(int n, int r, long mod){
            return out.modMultiply(mod, factorials[n], inverseFactorials[r], inverseFactorials[n-r]);
        }

        public long[] factorials(int n, long mod) {
            factorials = new long[n];
            factorials[0] = 1;
            for (int i = 1; i < n; i++) {
                factorials[i] = out.modMultiply(mod, factorials[i-1], i);
            }
            return factorials;
        }
    
        public long[] inverseFactorials(int n, long mod) {
            modInverseAll(n, mod);
            inverseFactorials = new long[n];
            inverseFactorials[0] = inverseFactorials[1] = 1;
            for (int i = 1; i < n; i++){
                inverseFactorials[i] = out.modMultiply(mod, inverseFactorials[i - 1], inverseNumbers[i]);
            }
            return inverseFactorials;
        }
    
        
        public long[] modInverseAll(int n, long mod) {
            inverseNumbers = new long[n];
            inverseNumbers[0] = inverseNumbers[1] = 1;
            for (int i = 2; i < n; i++) {
                inverseNumbers[i] = inverseNumbers[(int)(mod%i)] * (mod-mod/i)%mod; 
            }
            return inverseNumbers;
        }

        // Moduler Invserse of Number can also be calculated as num^(mod-2);
        public long modInverse(long n, long mod){
            return modIntPower(n, mod-2);
        }


        // Print yes or no
        public Mix yes() {
            res.append("YES");
            return out;
        }

        public Mix no() {
            res.append("NO");
            return out;
        }

        // Print new line
        public Mix nline() {
            res.append("\n");
            return out;
        }

        // Print element
        public void print(String x) {
            res.append(x);
        }

        public void print(int x) {
            res.append(x);
        }

        public void print(long x) {
            res.append(x);
        }

        public void print(char x) {
            res.append(x);
        }

        // Print element with new line
        public void println(String x) {
            res.append(x).append("\n");
        }

        public void println(int x) {
            res.append(x).append("\n");
        }

        public void println(long x) {
            res.append(x).append("\n");
        }

        public void println(char x) {
            res.append(x).append("\n");
        }

        // Print whole array or any number of argument with " " separated
        public void print(int... arr) {
            for (int e : arr)
                res.append(e).append(" ");
        }

        public void print(long... arr) {
            for (long e : arr)
                res.append(e).append(" ");
        }

        // Print array from index l to r
        public void print(int l, int r, int[] arr) {
            while (l <= r)
                res.append(arr[l++]).append(" ");
        }

        public void print(int l, int r, long[] arr) {
            while (l <= r)
                res.append(arr[l++]).append(" ");
        }

        public void print(int l, int r, double[] arr) {
            while (l <= r)
                res.append(arr[l++]).append(" ");
        }

        // Get Max of any number of arguments or array.
        public int max(int... arr) {
            int max = Integer.MIN_VALUE;
            for (int e : arr)
                max = Math.max(max, e);
            return max;
        }

        public long max(long... arr) {
            long max = Long.MIN_VALUE;
            for (long e : arr)
                max = Math.max(max, e);
            return max;
        }

        // Get Min of any number of arguments or array.
        public int min(int... arr) {
            int min = Integer.MAX_VALUE;
            for (int e : arr)
                min = Math.min(min, e);
            return min;
        }

        // Get Min of any number of arguments or array.
        public char min(char... arr) {
            char min = 'z';
            for (char e : arr)
                if(e<min) min = e;
            return min;
        }


        public long min(long... arr) {
            long min = Long.MAX_VALUE;
            for (long e : arr)
                min = Math.min(min, e);
            return min;
        }

        // Get Sum of any number of arguments or array.
        public long sum(int... arr) {
            long sum = 0;
            for (int e : arr)
                sum += e;
            return sum;
        }

        public long sum(long... arr) {
            long sum = 0;
            for (long e : arr)
                sum += e;
            return sum;
        }

        public char intToLowerChar(int ind){
            return (char)(ind + 'a');
        }
        public char intToUpperChar(int ind){
            return (char)(ind + 'A');
        }

        // Get Prefix Array for 1-D array
        public long[] prefix(int[] arr) {
            int n = arr.length;
            long[] pre = new long[n + 1];
            for (int i = 1; i <= n; i++) {
                pre[i] = pre[i - 1] + arr[i - 1];
            }
            return pre;
        }

        // Get Prefix Array for 2-D array
        public long[][] prefix(int[][] arr, int n, int m) {
            long[][] pre = new long[n + 1][m + 1];
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    pre[i][j] = pre[i - 1][j] + pre[i][j - 1] + arr[i - 1][j - 1] - pre[i - 1][j - 1];
                }
            }
            return pre;
        }

        public long prefixSum(int i, int j, int n, int m, long[][] prefix) {
            return prefixSum(i, j, n, m, prefix, true);
        }

        public long prefixSum(int i, int j, int n, int m, long[][] prefix, boolean zeroBased) {
            if (zeroBased) {
                ++i;
                ++j;
                ++n;
                ++m;
            }
            long topRight = prefix[i - 1][m], bottomLeft = prefix[n][j - 1], topLeft = prefix[i - 1][j - 1];
            return prefix[n][m] - topRight - bottomLeft + topLeft;
        }

        // Check given array is sorted or not from i to j
        public boolean isSorted(int[] arr, int i, int j) {
            int n = arr.length;
            while (i != j) {
                if (arr[i % n] > arr[(i + 1) % n])
                    return false;
                i++;
                i %= n;
            }
            return true;
        }

        // Swap element in array
        public void swap(int i, int j, int[] arr) {
            arr[i] ^= arr[j];
            arr[j] ^= arr[i];
            arr[i] ^= arr[j];
        }

        public void swap(int i, int j, long[] arr) {
            arr[i] ^= arr[j];
            arr[j] ^= arr[i];
            arr[i] ^= arr[j];
        }

        public void swap(int i, int j, char[] arr) {
            arr[i] ^= arr[j];
            arr[j] ^= arr[i];
            arr[i] ^= arr[j];
        }

        // Reverse array from i to j
        public void reverse(int[] arr, int i, int j) {
            while (i < j) {
                swap(i, j, arr);
                i++; j--;
            }
        }

        public void reverse(char[] arr, int i, int j) {
            while (i < j) {
                swap(i, j, arr);
                i++; j--;
            }
        }

        public void reverse(long[] arr, int i, int j) {
            while (i < j) {
                swap(i, j, arr);
                i++; j--;
            }
        }

        public long reverse(long n) {
            long rev = 0;
            while (n > 0) {
                rev = rev * 10 + n % 10;
                n /= 10;
            }
            return rev;
        }

        public int reverse(int n) {
            return (int)reverse((long)n);
        }

        // Check if its remainder when divided by 1 is 0
        public boolean isInteger(double n) {
            return n % 1 == 0;
        }

        public boolean isApproximatelyInteger(double number) {
            final double TOLERANCE = 1e-10; // Adjust tolerance as needed
            return Math.abs(number - Math.round(number)) < TOLERANCE;
        }

        // Check given is palindrome or not
        public boolean isPalindrome(String s, int i, int j) {
            while (i < j) {
                if (s.charAt(i++) != s.charAt(j--))
                    return false;
            }
            return true;
        }

        public boolean isPalindrome(char[] s, int i, int j) {
            while (i < j) {
                if (s[i++] != s[j--])
                    return false;
            }
            return true;
        }

        public boolean isPalindrome(long n) {
            return n == reverse(n);
        }

        /**
         * Get all palindrome number till n.
         * Note : Number of Palindrome up to n = 2*sqrt(n).
         * for n = 1e5, num of palindrome is 632
         */
        public List<Long> getPalindromeNumbers(long n) {
            List<Long> list = new ArrayList<>();
            for (long i = 0; i <= n; i++) {
                if (isPalindrome(i))
                    list.add(i);
            }
            return list;
        }

        // Check given character is vowel or not
        public boolean isVowel(char c) {
            return "aeiouAEIOU".indexOf(c) != -1;
        }

        // Check given character is consonant or not
        public boolean isConsonant(char c) {
            return !isVowel(c);
        }

        // Check i, j are inside matrix or not
        public boolean isSafe(int i, int j, int n, int m) {
            return i >= 0 && i < n && j >= 0 & j < m;
        }


        // x + y with mod 1e9+7
        public long modIntSum(long x, long y) {
            return modSum(x, y, MOD);
        }

        public long modSum(long x, long y, long MOD) {
            return (x % MOD + y % MOD) % MOD;
        }

        // x multiply y with mod 1e9+7
        public long modIntMultiply(long x, long y) {
            return modMultiply(MOD, x, y);
        }

        // x power y with mod 1e9+7
        public long modIntPower(long x, long y) {
            return modPower(x, y, MOD);
        }

        // Factorial of n with MOD 1e9+7
        public long modIntFactorial(long n) {
            return modFactorial(n, MOD);
        }

        // (A%MOD*B%MOD*C%MOD*D%MOD...)%MOD
        public long modMultiply(long mod, long...nums) {
            long ans = 1;
            for (long num : nums) {
                ans *= (num%mod);
                ans %= mod;
            }
            return ans;
        }

        // x power y with mod = (X^Y)%MOD
        public int modPower(int x, int y, int mod) {
            return (int)modPower((long)x, y, mod);
        }

        public long modPower(long x, long y, long p) {
            long res = 1;
            x = x % p;
            if (x == 0)
                return 0;
            while (y > 0) {
                if ((y & 1) != 0)
                    res = (res * x) % p;
                y = y >> 1;
                x = (x * x) % p;
            }
            res %= p;
            if (res < 0)
                res += p;
            return res;
        }

        // x power y = (X^Y)
        public long power(int N, int K) {
            long result = 1;
            for (int i = 0; i < K; i++) {
                result *= N;
            }
            return result;
        }

        // Factorial of n with MOD
        public long modFactorial(long n, long mod) {
            if (n == 0)
                return 0;
            long ans = 1;
            for (long i = 2; i <= n; i++) {
                ans *= i;
                ans %= mod;
            }
            return ans;
        }

        // Log base 2 of n
        public double log2(double n) {
            return Math.log(n) / Math.log(2.0);
        }

        // Lower Bound - Smallest Index i satisfy given predicate condition
        public int lowerBound(List<Integer> arr, int n, Predicate<Integer> predicate) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (predicate.test(arr.get(m))) {
                    h = m - 1;
                } else
                    l = m + 1;
            }
            return l;
        }

        // Lower Bound - Smallest Index i a[i]>=x
        public int lowerBound(List<Integer> arr, int n, int x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr.get(m) >= x) {
                    h = m - 1;
                } else
                    l = m + 1;
            }
            return l;
        }

        public int lowerBound(List<Long> arr, int n, long x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr.get(m) >= x) {
                    h = m - 1;
                } else
                    l = m + 1;
            }
            return l;
        }

        public int lowerBound(int[] arr, int n, int x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr[m] >= x) {
                    h = m - 1;
                } else
                    l = m + 1;
            }
            return l;
        }

        public int lowerBound(long[] arr, int n, long x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr[m] >= x) {
                    h = m - 1;
                } else
                    l = m + 1;
            }
            return l;
        }

        public int lowerBound(double[] arr, int n, double x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr[m] >= x) {
                    h = m - 1;
                } else
                    l = m + 1;
            }
            return l;
        }

        // Upper Bound - Largest Index i a[i]<=x
        public int upperBound(List<Integer> arr, int n, int x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr.get(m) <= x) {
                    l = m + 1;
                } else
                    h = m - 1;
            }
            return h;
        }

        public int upperBound(int[] arr, int n, int x) {
            int l = 0, h = n - 1;
            while (l <= h) {
                int m = l + (h - l) / 2;
                if (arr[m] <= x) {
                    l = m + 1;
                } else
                    h = m - 1;
            }
            return h;
        }

        // Sieve of Eratosthenes
        // TC - N(log(logN)) -> ~O(N)
        public boolean[] sieve(int n) {
            primes = new boolean[n + 1];

            // Mark all primes true
            for (int i = 2; i <= n; i++)
                primes[i] = true;

            // Mark false for multiple of primes
            for (int p = 2; p * p <= n; p++) {
                if (primes[p]) {
                    for (int i = p * p; i <= n; i += p) {
                        primes[i] = false;
                    }
                }
            }
            return primes;
        }

        // Check For Prime
        public boolean isPrime(int n) {
            if (n <= 1)
                return true;

            for (int i = 2; i * i <= n; i++) {
                if (n % i == 0)
                    return false;
            }
            return true;
        }

        // Calculate gcd of a,b
        public int gcd(int a, int b) {
            return (int)gcd((long)a, b);
        }

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

        public int lcm(int a, int b) {
            return (int) lcm((long)a, b);
        }

        public long lcm(long a, long b) {
            return Math.abs(a*b)/gcd(a,b);
        }

        // Get all factors/divisors of N
        public List<Integer> getPrimes(int n) {
            if(primes==null)sieve(n);
            primeList = new ArrayList<>();
            for (int i = 2; i <= n; i++) {
                if (primes[i]) {
                    primeList.add(i);
                }
            }
            return primeList;
        }

        // Get all factors/divisors of N
        public List<Integer> getFactors(int n) {
            List<Integer> factorList = new ArrayList<>();

            for (int i = 1; i * i <= n; i++) {
                if (n % i == 0) {
                    factorList.add(i);
                    if (i != n / i)
                    factorList.add(n / i);
                }
            }
            return factorList;
        }

        public List<Long> getFactors(long n) {
            List<Long> factorList = new ArrayList<>();

            for (long i = 1; i * i <= n; i++) {
                if (n % i == 0) {
                    factorList.add(i);
                    if (i != n / i)
                    factorList.add(n / i);
                }
            }
            return factorList;
        }

        // Count all factors/divisors of N
        public int countFactors(int n) {
            int cnt = 0;

            for (int i = 1; i * i <= n; i++) {
                if (n % i == 0) {
                    cnt++;
                    if (i != n / i)
                        cnt++;
                }
            }
            return cnt;
        }

        public long countFactors(long n) {
            long cnt = 0;

            for (long i = 1; i * i <= n; i++) {
                if (n % i == 0) {
                    cnt++;
                    if (i != n / i)
                        cnt++;
                }
            }
            return cnt;
        }

        // Get All Prime factors of n with their frequency
        public Map<Long, Integer> primeFactorization(long n) {
            Map<Long, Integer> map = new HashMap<>();
            while (n % 2 == 0) {
                map.merge(2L, 1, Integer::sum);
                n /= 2;
            }
            for (long i = 3; i * i <= n; i += 2) {
                while (n % i == 0) {
                    map.merge(i, 1, Integer::sum);
                    n /= i;
                }
            }
            if (n > 2)
                map.merge(n, 1, Integer::sum);
            return map;
        }

        // Calculate total number of factors of any number from their primeFactorization
        public long calculateTotalNumberOfFactors(Map<Long, Integer> primeFactors) {
            long totalFactors = 1;
            for (int exponent : primeFactors.values())
                totalFactors *= (exponent + 1);
            return totalFactors;
        }

        // Calculate total number of factors of any number n
        public long calculateTotalNumberOfFactors(long n) {
            Map<Long, Integer> primeFactors = primeFactorization(n);
            return calculateTotalNumberOfFactors(primeFactors);
        }

        // Get all composite numbers till n
        public List<Integer> getCompositeNumbers(int n) {
            List<Integer> list = new ArrayList<>();
            sieve(n);
            for (int i = 0; i < n; i++) {
                if (!primes[i])
                    list.add(i);
            }
            return list;
        }

        // Decimal to binary
        public String DtoB(int n) {
            StringBuilder sb = new StringBuilder();
            while (n > 0) {
                sb.append(n % 2);
                n >>= 1;
            }
            return sb.reverse().toString();
        }

        public String DtoB(long n) {
            StringBuilder sb = new StringBuilder();
            while (n > 0) {
                sb.append(n % 2);
                n >>= 1;
            }
            return sb.reverse().toString();
        }

        // Binary to Decimal
        public int BtoD(String s) {
            int n = 0, k = s.length() - 1;
            for (char c : s.toCharArray()) {
                if (c == '1')
                    n += 1 << k;
                k--;
            }
            return n;
        }

        // Check ith bit is set or not
        public boolean isBitSet(int n, int i) {
            return (n & (1 << i)) != 0;
        }

        // Check N is power of 2
        public boolean isPowerOf2(int n) {
            return (n & (n - 1)) == 0;
        }

        public boolean isPowerOf2(long n) {
            return (n & (n - 1)) == 0;
        }

        // Check N is power of 4 or not
        public boolean isPowerOf4(int n) {
            return n % 3 == 1 && (n & (n - 1)) == 0;
        }

        // Count Set bit in N | TC - O(no. of set bits)
        public int cntSetBit(int n) {
            int cnt = 0;
            while (n > 0) {
                n = n & (n - 1);
                cnt++;
            }
            return cnt;
        }

        // Check all bits are set in given num | 7-> 111
        public boolean allBitSet(int n) {
            return (n & (n + 1)) == 0;
        }

        // Check bits in num having in alternative form | 101010 | 010101
        public boolean isAlternativeBit(int n) {
            // 101010 ^ 010101 (n>>1) = 111111
            return allBitSet(n ^ (n >> 1));
        }

        // Unset Left most set bit 0101100 -> 0001100
        public long unsetLSB(long n) {
            return n & -n;
        }

        // Unset right most set bit 1101100 -> 1101000
        public long unsetRSB(long n) {
            return n & ~(n ^ (n - 1));
        }
    }


    /**
     * Segment Tree with Lazy propagation Template
     * Get Sum in range.
     */
    static class SegmentTree {
        long[] segArr, lazy;
        int N;

        SegmentTree(int n) {
            N = n;
            segArr = new long[4 * n + 1];
            lazy = new long[4 * n + 1];
        }

        void build(long[] arr) {
            build(0, 0, N - 1, arr);
        }

        private void build(int ind, int low, int high, long[] arr) {
            if (low == high) {
                segArr[ind] = arr[low];
                return;
            }
            int mid = low + (high - low) / 2;

            build(2 * ind + 1, low, mid, arr);
            build(2 * ind + 2, mid + 1, high, arr);

            segArr[ind] = segArr[2 * ind + 

Solution 2 (runtime: 718 ms):

import java.io.*;
import java.util.*;

public class ComTemp2 {

    private static final long MINA = 1;
    private static final long MAXA = 1000000000000L;
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static PrintWriter writer;

    static int MOD = (int) (1e9 + 7);
    static final int MOD1 = 998244353;

    public static Scanner scanner = new Scanner(System.in);

    public static boolean flag1 = true, flag2 = true;

    public static void main(String[] args) throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        tokenizer = new StringTokenizer("");

        int n = nextInt();
//        int n = 1;
        while (n-- > 0) {
            solve();
        }

        writer.close();
    }


    static int fun(int a1, int a2, int a3, int a4, int a5) {
        int count = 0;
        if (a3 == a1 + a2) count++;
        if (a4 == a2 + a3) count++;
        if (a5 == a3 + a4) count++;
        return count;
    }

    public static void solve() throws IOException {
        int n = nextInt(), m = nextInt();
        int[] arr = nextIntArray(n), brr = nextIntArray(m);
        PriorityQueue<Long> a = new PriorityQueue<>((x,y)->Long.compare(y,x)), b = new PriorityQueue<>((x,y)->Long.compare(y,x));
        for (int i = 0; i < n; i++) {
            a.add((long) arr[i]);
        }
        for (int i = 0; i < m; i++) {
            b.add((long) brr[i]);
        }
//        writer.println(a+" "+b);

        while (!a.isEmpty() && !b.isEmpty()) {
            if (Objects.equals(a.peek(), b.peek())){
                a.poll();
                b.poll();
            }else {
               long e = b.poll();
               if (e<a.peek() || a.size() <= b.size()){
                   writer.println("NO");
                   return;
               }
               b.add(e/2);
               if (e%2==0){
                   e/=2;
               }else {
                   e/=2;
                   e++;
               }
               b.add(e);
            }
        }

        if (!a.isEmpty() || !b.isEmpty()){
            writer.println("NO");
            return;
        }

        writer.println("YES");
    }

    public static void solv() throws IOException {
        int n = nextInt();
        for (int i = 0; i < 2; i++) {
            writer.print(1 + " ");
        }
        for (int i = 2; i < n - 1; i++) {
            writer.print(i + " ");
        }
        writer.println(1);
    }

    public static boolean isPowerLogarithmic(int x, int y) {
        if (x <= 0 || y <= 0 || y == 1) return false; // Edge cases

        double logResult = Math.log(x) / Math.log(y);
        return Math.abs(logResult - Math.round(logResult)) < 1e-10;
    }


/*
14
10100001000101
15
001110100100001
14
10100101111101

11
01100101101
14
01001111101001
 */

    private static int ask(int l, int r) throws IOException {
        if (r - l + 1 <= 1)
            return 0;
        writer.println(String.valueOf("? ") + String.valueOf(l) + " " + String.valueOf(r));
        writer.flush();
        return nextInt();
    }

    public static int leftmostBit(int n) {
        return (int) Math.pow(2, (int) (Math.log(n) / Math.log(2)));
    }

    public static int leftmostBit(long n) {
        return (int) Math.pow(2, (long) (Math.log(n) / Math.log(2)));
    }

    public static long gcdOfArray(int[] arr) {
        long result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            result = gcd(result, arr[i]);
            if (result == 1)
                break; // Early exit if GCD is 1
        }
        return result;
    }

    static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public static List<Integer> findDivisors(int n) {
        List<Integer> divisors = new ArrayList<>();

        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                divisors.add(i); // Add the divisor
                if (i != n / i) { // Avoid duplicate for perfect squares
                    divisors.add(n / i);
                }
            }
        }

        // Optional: Sort the divisors if needed
        divisors.sort(Integer::compareTo);
        return divisors;
    }

    public static long[] unique(long[] arr) {
        int n = arr.length;
        ArrayList<Long> l1 = new ArrayList<>();
        l1.add(arr[0]);
        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1]) {
                continue;
            } else {
                l1.add(arr[i]);
            }
        }
        n = l1.size();
        long[] tem = new long[n];
        for (int i = 0; i < n; i++) {
            tem[i] = l1.get(i);
        }
        return tem;
    }

    public static void reverseArray(long[] array) {
        int start = 0, end = array.length - 1;
        while (start < end) {
            long temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
    }

    public static <T> void printArr(int[] arr) {
        for (int e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    public static void printArr(long[] arr) {
        for (long e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    private static int lowerBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    // Binary search for the last index where arr[index] <= target
    private static int upperBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
//        if (arr[right]>target)return -1;
        return right;
    }

    public static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Pair{" + "key=" + key + ", value=" + value + '}';
        }
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static String nextLine() throws IOException {
        return reader.readLine();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    // Function to read the next array of longs
    static long[] nextLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}