import java.util.Arrays;
import java.util.Scanner;


public class JavaApplication75 {

    
    static int gcd(int a, int b) { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
      static int findGCD(int arr[], int n) { 
        int result = arr[0]; 
        for (int i = 1; i < n; i++) {
            result = gcd(arr[i], result); 
        // System.out.println(arr[i] + " "+ result);
        }
        return result; 
    } 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x=in.nextInt();
        int p=in.nextInt();
        int v=in.nextInt();
        int re=0;
        int gcd=0;
        int n;
        int r[]=new int[p];
        int e[]=new int[v];
        for(int y=0;y<x;y++){
        for(int i=0;i<p;i++){
            r[i]=in.nextInt();
        }
        for(int i=0;i<v;i++){
            e[i]=in.nextInt();
        }
        int s[];
        Arrays.sort(r);
        
        if(p>2){
            s=new int[p-1];
            for(int i=0;i<p-1;i++){
             s[i]=r[i+1]-r[0];
            }
        }
        else{
            s=r;
        }
        n = s.length; 
        gcd=findGCD(s, n); 
        if(gcd != 0 && gcd != 1){
            for(int d=0;d<v;d++){
                if(e[d] % gcd !=0){
                    re=1;
                }
            }
        }
        System.out.println(re);
        re=0;
        if(y != x-1){
        p=in.nextInt();
        v=in.nextInt();
    }
    }
    }
}