#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>

using namespace std;

typedef long long ll;

/*vector<bool> neprost(100000001);
vector<int> prosti;

void eratoster(int limit)
{
	for (int x = 2; x <= limit; x++)
    		if (!neprost[x])
            			for (ll broj = x*(ll)x; broj <= limit; broj += x)
                        				neprost[broj] = true;
                                        	for (int x = 2; x <= limit; x++)
                                            		if (!neprost[x])
                                                    			prosti.push_back(x);
                                                                }*/

                                                                int main()
                                                                {
                                                                	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
                                                                    	//eratoster(100000000);
                                                                        	int t;
                                                                            	cin >> t;
                                                                                	set<ll> a, b, ua;
                                                                                    	
                                                                                        	while (t--)
                                                                                            	{
                                                                                                		int na, nb;
                                                                                                        		cin >> na >> nb;
                                                                                                                		a.clear(); b.clear(); ua.clear();
                                                                                                                        		for (int i = 0; i < na; i++)
                                                                                                                                		{
                                                                                                                                        			ll br;
                                                                                                                                                    			cin >> br;
                                                                                                                                                                			ua.insert(br);
                                                                                                                                                                            		}
                                                                                                                                                                                    		{
                                                                                                                                                                                            			ll delta = *ua.begin();
                                                                                                                                                                                                        			for (auto i = next(ua.begin()); i != ua.end(); i++)
                                                                                                                                                                                                                    				a.insert(*i - delta);
                                                                                                                                                                                                                                    			for (int i = 0; i < nb; i++)
                                                                                                                                                                                                                                                			{
                                                                                                                                                                                                                                                            				ll br;
                                                                                                                                                                                                                                                                            				cin >> br;
                                                                                                                                                                                                                                                                                            				b.insert(br - delta);
                                                                                                                                                                                                                                                                                                            			}
                                                                                                                                                                                                                                                                                                                        		}
                                                                                                                                                                                                                                                                                                                                		ll br = *a.begin();
                                                                                                                                                                                                                                                                                                                                        		a.erase(br);
                                                                                                                                                                                                                                                                                                                                                		#define proveri(x) {\
                                                                                                                                                                                                                                                                                                                                                        		bool jeste = false;\
                                                                                                                                                                                                                                                                                                                                                                		for (auto aa : a)\
                                                                                                                                                                                                                                                                                                                                                                        			if (aa % x)\
                                                                                                                                                                                                                                                                                                                                                                                    			{\
                                                                                                                                                                                                                                                                                                                                                                                                				jeste = true;\
                                                                                                                                                                                                                                                                                                                                                                                                                				break;\
                                                                                                                                                                                                                                                                                                                                                                                                                                			}\
                                                                                                                                                                                                                                                                                                                                                                                                                                            		if (!jeste)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                    		{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                            			for (auto bb : b)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        				if (!(bb % x))\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        				{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        					jeste = true;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            					break;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                			    }\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                			if (!jeste) goto dobar;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            		}}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    		{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            		ll x = br;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    		bool jeste = false;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            		for (auto aa : a)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    			if (aa % x)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                			{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            				jeste = true;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            				break;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            			}\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        		if (!jeste)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                		{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        			for (auto bb : b)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    				if (!(bb % x))\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    				{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    					jeste = true;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        					break;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            			    }\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            			if (!jeste) goto dobar;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        		}}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                		for (int i = min(br - 1, (ll)ceil(sqrt(br))); i; i--)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        			if (!(br % i))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    				{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    				ll x = i;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    		bool jeste = false;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            		for (auto aa : a)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    			if (aa % x)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                			{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            				jeste = true;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            				break;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            			}\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        		if (!jeste)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                		{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        			for (auto bb : b)\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    				if (!(bb % x))\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    				{\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    					jeste = true;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        					break;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            			    }\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            			if (!jeste) goto dobar;\
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        		}}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                		cout << "0\n";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        		continue;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                		dobar:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        		cout << "1\n";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }