using System;
using System.Collections.Generic;

namespace godine
{
    class Program
    {
        static void Main(string[] args)
        {
            long t = long.Parse(Console.ReadLine());
            long[] u = new long[t];
            long[] n; long[] m;
            string s;
            int index, q = 0;
            long[] x = new long[t];
            long[] y = new long[t];

            for (int i = 0; i < t; i++)
            {
                u[i] = 1;
                s = Console.ReadLine();
                long[] a = new long[t];
                long[] b = new long[t];
                index = s.IndexOf(" ");
                int ni = int.Parse(s.Substring(0, index));
                int n1 = ni;
                int mi = int.Parse(s.Substring(index + 1));
                int m1 = mi;
                n = new long[ni];
                m = new long[mi];
                s = Console.ReadLine();

                for (int j = 0; j < ni - 1; j++)
                {
                    index = s.IndexOf(" ");
                    n[j] = long.Parse(s.Substring(0, index));
                    s = s.Remove(0, index + 1);
                }
                index = s.Length - 1;
                n[ni - 1] = long.Parse(s.Substring(index));
                Array.Sort(n);
                while (n1 - q >= 1)
                {
                    n[n1 - 1] -= n[q];
                    n1--;
                }
                while (n[ni - 1] != 0 && n[ni - 2] != 0)
                {
                    if (n[ni - 1] > n[ni - 2])
                    {
                        n[ni - 1] = n[ni - 1] - n[ni - 2];
                    }
                    else
                    {
                        n[ni - 2] = n[ni - 2] - n[ni - 1];
                    }
                }
                if (n[ni - 1] != 0)
                {
                    x[i] = n[ni - 1];
                }
                else
                {
                    x[i] = n[ni - 2];
                }

                s = Console.ReadLine();
                for (int k = 0; k < mi - 1; k++)
                {
                    index = s.IndexOf(" ");
                    m[k] = int.Parse(s.Substring(0, index));
                    s = s.Remove(0, index + 1);
                }
                index = s.Length - 1;
                m[mi - 1] = int.Parse(s.Substring(index));

                while (m1 - q >= 1)
                {
                    m[m1 - 1] -= n[q];
                    m1--;
                }
                while (m[mi - 1] != 0 && m[mi - 2] != 0)
                {
                    if (m[mi - 1] > m[mi - 2])
                    {
                        m[mi - 1] = m[mi - 1] - m[mi - 2];
                    }
                    else
                    {
                        m[mi - 2] = m[mi - 2] - m[mi - 1];
                    }
                }
                if (m[mi - 1] != 0)
                {
                    y[i] = m[mi - 1];
                }
                else
                {
                    y[i] = m[mi - 2];
                }
                if (x[i] / y[i] ==0 || y[i] / x[i] == 0)
                {
                    u[i] = 0;
                }
                else
                {
                    
                        u[i] = 1;
                    
                }
            }
            
            for (int p = 0; p < t; p++)
            {
                Console.WriteLine(u[p]);
            }
        }
    }
}