description: You are given an array a[0 … n - 1] = [a_0, a_1, …, a_{n - 1}] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.
  
  In one step, the array a is replaced by another array of length n according to the following rules: 
  
    1. First, a new array a^{→ d} is defined as a cyclic shift of the array a to the right by d cells. The elements of this array can be defined as a^{→ d}_i = a_{(i + n - d) mod n}, where (i + n - d) mod n is the remainder of integer division of i + n - d by n. 
  
  It means that the whole array a^{→ d} can be represented as a sequence $$$a^{→ d} = [a_{n - d}, a_{n - d + 1}, …, a_{n - 1}, a_0, a_1, …, a_{n - d - 1}]$$$
  
    2. Then each element of the array a_i is replaced by a_i  \&  a^{→ d}_i, where \& is a logical "AND" operator. 
  
  
  
  For example, if a = [0, 0, 1, 1] and d = 1, then a^{→ d} = [1, 0, 0, 1] and the value of a after the first step will be [0  \&  1, 0  \&  0, 1  \&  0, 1  \&  1], that is [0, 0, 0, 1].
  
  The process ends when the array stops changing. For a given array a, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.
  
  Input
  
  The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases.
  
  The next 2t lines contain descriptions of the test cases. 
  
  The first line of each test case description contains two integers: n (1 ≤ n ≤ 10^6) — array size and d (1 ≤ d ≤ n) — cyclic shift offset. The second line of the description contains n space-separated integers a_i (0 ≤ a_i ≤ 1) — elements of the array.
  
  It is guaranteed that the sum of n over all test cases does not exceed 10^6.
  
  Output
  
  Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to 1 in the array after the end of the process, print -1.
  
  Example
  
  Input
  
  
  5
  2 1
  0 1
  3 2
  0 1 0
  5 2
  1 1 0 1 0
  4 2
  0 1 0 1
  1 1
  0
  
  
  Output
  
  
  1
  1
  3
  -1
  0
  
  Note
  
  In the third sample test case the array will change as follows: 
  
    1. At the beginning a = [1, 1, 0, 1, 0], and a^{→ 2} = [1, 0, 1, 1, 0]. Their element-by-element "AND" is equal to $$$[1  \&  1, 1  \&  0, 0  \&  1, 1  \&  1, 0  \&  0] = [1, 0, 0, 1, 0]$$$ 
    2. Now a = [1, 0, 0, 1, 0], then a^{→ 2} = [1, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1  \&  1, 0  \&  0, 0  \&  1, 1  \&  0, 0  \&  0] = [1, 0, 0, 0, 0]$$$ 
    3. And finally, when a = [1, 0, 0, 0, 0] we get a^{→ 2} = [0, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1  \&  0, 0  \&  0, 0  \&  1, 0  \&  0, 0  \&  0] = [0, 0, 0, 0, 0]$$$ 
  
  Thus, the answer is 3 steps.
  
  In the fourth sample test case, the array will not change as it shifts by 2 to the right, so each element will be calculated as 0  \&  0 or 1  \&  1 thus not changing its value. So the answer is -1, the array will never contain only zeros.
solution: import java.io.*;
  import java.util.*;
  
  
  
  import java.math.*;
  import java.math.BigInteger;
  
  
  public final class A
  {    
  	static PrintWriter out = new PrintWriter(System.out); 
  	static StringBuilder ans=new StringBuilder();
  	static FastReader in=new FastReader();
  	static ArrayList<Integer> g[];
  	static long mod=(long)1e9+7,INF=Long.MAX_VALUE;
  	static boolean set[],col[]; 
  	static int par[],tot[],partial[];
  	static int Days[],P[][];
  	static int dp[][],sum=0,size[];
  	static ArrayList<Long> seg[];
  	static ArrayList<Long> A;
  	//	static HashSet<Integer> set;
  	//		static node1 seg[];
  	//static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)};
  	public static void main(String args[])throws IOException
  	{		
  		int T=i();
  		outer:while(T-->0)
  		{
  			int N=i(),d=i();
  			d=N-d;
  			int A[]=input(N);
  			g=new ArrayList[N+5];
  			boolean equal=false;
  			for(int i=0; i<=N; i++)g[i]=new ArrayList<>();
  			for(int i=0; i<N; i++)
  			{
  				int next=(i+d)%N;
  				if(i==N) 
  				{
  					equal=true;
  					break;
  				}
  				g[i].add(next);
  			}
  			if(equal)
  			{
  				for(int a:A)
  				{
  					if(a==1)
  					{
  						out.println(-1);
  						continue outer;
  					}
  				}
  				out.println(0);
  			}
  			else
  			{
  				int D[]=new int[N+1];
  				Arrays.fill(D, Integer.MAX_VALUE);
  				set=new boolean[N+1];
  				for(int i=0; i<N; i++)
  				{
  					if(A[i]==0 && !set[i])
  					{
  						int next=g[i].get(0);
  						int dist=0;
  						D[i]=0;
  						while(next!=i)
  						{
  							set[next]=true;
  							if(A[next]==0)
  							{
  								dist=0;
  							}
  							else dist++;
  							D[next]=Math.min(D[next], dist);
  							next=g[next].get(0);
  						}
  					}		
  				}
  //				print(D);
  				int max=0;
  				for(int i=0; i<N; i++)max=Math.max(D[i], max);
  				if(max==Integer.MAX_VALUE)max=-1;
  				out.println(max);
  			}
  
  		}
  		out.println(ans);
  		out.close();
  	}
  	static int ask(int v,int tl,int tr,int l,int r,long x)
  	{
  		if(l>r)return 0;
  		if(tl==l && tr==r)
  		{
  			return f(seg[v],x)+1;
  		}
  		int tm=(tl+tr)/2;
  		return ask(v*2,tl,tm,l,Math.min(tm, r),x)+ask(v*2+1,tm+1,tr,Math.max(tm+1,l),r,x);
  	}
  	static void build(int v,int tl,int tr,long A[])
  	{
  		if(tl==tr)
  		{
  			seg[v].add(A[tl]);
  			return;
  		}
  		int tm=(tl+tr)/2;
  		build(v*2,tl,tm,A);
  		build(v*2+1,tm+1,tr,A);
  		merge(v);
  		//System.out.println(seg[v]);
  	}
  	static void merge(int v)
  	{
  		int i=0,j=0;
  		while(i<seg[v*2].size() && j<seg[v*2+1].size())
  		{
  			if(seg[v*2].get(i)<seg[v*2+1].get(j))
  				seg[v].add(seg[v*2].get(i++));
  			else seg[v].add(seg[v*2+1].get(j++));
  		}
  		while(i<seg[v*2].size())
  		{
  
  			seg[v].add(seg[v*2].get(i++));
  
  		}
  		while( j<seg[v*2+1].size())
  		{
  
  			seg[v].add(seg[v*2+1].get(j++));
  		}
  	}
  	static int f(ArrayList<Long> X,long x)
  	{
  		int l=-1,r=X.size();
  		while(r-l>1)
  		{
  			int m=(l+r)/2;
  			long a=X.get(m);
  			if(a<=x)l=m;
  			else r=m;
  		}
  		return l;
  	}
  	static int f(char X[][],int i,int j,int N,int M)
  	{
  		int c1=0,c2=0;
  		int x=i,y=j;
  		while(i>=0 && j>=0 && X[i][j]=='*')
  		{
  			c1++;
  			i--;
  			j--;
  		}
  		i=x;
  		j=y;
  		while(i>=0 && j<M && X[i][j]=='*')
  		{
  			c2++;
  			i--;
  			j++;
  		}	
  		return Math.min(c1,c2)-1;
  	}
  	static void update(char X[][],int min[][],int i,int j,int N,int M,int d)
  	{
  		int x=i,y=j;
  		while(i>=0 && j>=0 && X[i][j]=='*')
  		{
  			min[i][j]=Math.max(min[i][j],d);
  			i--;
  			j--;
  		}
  		i=x;
  		j=y;
  		while(i>=0 && j<M && X[i][j]=='*')
  		{
  			min[i][j]=Math.max(min[i][j],d);
  			i--;
  			j++;
  		}	
  	}
  
  	static long and(int i,int j)
  	{
  		System.out.println("and "+i+" "+j);
  		return l();
  	}
  	static long or(int i,int j)
  	{
  		System.out.println("or "+i+" "+j);
  		return l();
  	}
  	static boolean is_Sorted(int A[])
  	{
  		int N=A.length;
  		for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
  		return true;
  	}
  	static boolean f(StringBuilder sb,String Y,String order)
  	{
  		StringBuilder res=new StringBuilder(sb.toString());
  		HashSet<Character> set=new HashSet<>();
  		for(char ch:order.toCharArray())
  		{
  			set.add(ch);
  			for(int i=0; i<sb.length(); i++)
  			{
  				char x=sb.charAt(i);
  				if(set.contains(x))continue;
  				res.append(x);
  			}
  		}
  
  		String str=res.toString();
  		return str.equals(Y);
  	}
  	static boolean all_Zero(int f[])
  	{
  		for(int a:f)if(a!=0)return false;
  		return true;
  	}
  	static long form(int a,int l)
  	{
  		long x=0;
  		while(l-->0)
  		{
  			x*=10;
  			x+=a;
  		}
  		return x;
  	}
  	static int count(String X)
  	{
  		HashSet<Integer> set=new HashSet<>();
  		for(char x:X.toCharArray())set.add(x-'0');
  		return set.size();
  	}
  
  
  	//	static void build(int v,int tl,int tr,long A[])
  	//	{
  	//		if(tl==tr)
  	//		{
  	//			seg[v]=A[tl];
  	//		}
  	//		else
  	//		{
  	//			int tm=(tl+tr)/2;
  	//			build(v*2,tl,tm,A);
  	//			build(v*2+1,tm+1,tr,A);
  	//			seg[v]=Math.min(seg[v*2], seg[v*2+1]);
  	//		}
  	//	}
  
  	static int [] sub(int A[],int B[])
  	{
  		int N=A.length;
  		int f[]=new int[N];
  		for(int i=N-1; i>=0; i--)
  		{
  			if(B[i]<A[i])
  			{
  				B[i]+=26;
  				B[i-1]-=1;
  			}
  			f[i]=B[i]-A[i];
  		}
  		for(int i=0; i<N; i++)
  		{
  			if(f[i]%2!=0)f[i+1]+=26;
  			f[i]/=2;
  		}
  		return f;
  	}
  
  
  
  
  
  	static int max(int a ,int b,int c,int d)
  	{
  		a=Math.max(a, b);
  		c=Math.max(c,d);
  		return Math.max(a, c);
  	}
  	static int min(int a ,int b,int c,int d)
  	{
  		a=Math.min(a, b);
  		c=Math.min(c,d);
  		return Math.min(a, c);
  	}
  
  
  
  	static HashMap<Integer,Integer> Hash(int A[])
  	{
  		HashMap<Integer,Integer> mp=new HashMap<>();
  		for(int a:A)
  		{
  			int f=mp.getOrDefault(a,0)+1;
  			mp.put(a, f);
  		}
  		return mp;
  	}
  	static long mul(long a, long b)
  	{
  		return ( a %mod * 1L * b%mod )%mod;
  	}
  	static void swap(int A[],int a,int b)
  	{
  		int t=A[a];
  		A[a]=A[b];
  		A[b]=t;
  	}
  
  
  	static int find(int a)
  	{
  		if(par[a]<0)return a;
  		return par[a]=find(par[a]);
  	}
  	static void union(int a,int b)
  	{
  		a=find(a);
  		b=find(b);
  		if(a!=b)
  		{
  			par[a]+=par[b];
  			par[b]=a;
  		}
  	}
  	static boolean isSorted(int A[])
  	{
  		for(int i=1; i<A.length; i++)
  		{
  			if(A[i]<A[i-1])return false;
  		}
  		return true;
  	}
  
  	static boolean isDivisible(StringBuilder X,int i,long num)
  	{
  		long r=0;
  		for(; i<X.length(); i++)
  		{
  			r=r*10+(X.charAt(i)-'0');
  			r=r%num;
  		}
  		return r==0;
  	}    
  
  
  
  	static int lower_Bound(int A[],int low,int high, int x) 
  	{ 
  		if (low > high) 
  			if (x >= A[high]) 
  				return A[high]; 
  
  		int mid = (low + high) / 2; 
  
  		if (A[mid] == x) 
  			return A[mid]; 
  
  		if (mid > 0 && A[mid - 1] <= x && x < A[mid]) 
  			return A[mid - 1]; 
  
  		if (x < A[mid]) 
  			return lower_Bound( A, low, mid - 1, x); 
  
  		return lower_Bound(A, mid + 1, high, x); 
  	} 
  
  	static String f(String A)
  	{
  		String X="";
  		for(int i=A.length()-1; i>=0; i--)
  		{
  			int c=A.charAt(i)-'0';
  			X+=(c+1)%2;
  		}
  		return X;
  	}
  
  	static void sort(long[] a) //check for long
  	{
  		ArrayList<Long> l=new ArrayList<>();
  		for (long i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
  
  	static String swap(String X,int i,int j)
  	{
  		char ch[]=X.toCharArray();
  		char a=ch[i];
  		ch[i]=ch[j];
  		ch[j]=a;
  		return new String(ch);
  	}
  
  	static int sD(long n)  
  	{  
  
  		if (n % 2 == 0 )  
  			return 2;  
  
  		for (int i = 3; i * i <= n; i += 2) {  
  			if (n % i == 0 )  
  				return i;  
  		}  
  
  		return (int)n;  
  	}  
  
  	static void setGraph(int N)
  	{
  		tot=new int[N+1];
  		partial=new int[N+1];
  		Days=new int[N+1];
  		P=new int[N+1][(int)(Math.log(N)+10)];
  		set=new boolean[N+1];
  		g=new ArrayList[N+1];
  		for(int i=0; i<=N; i++)
  		{
  
  			g[i]=new ArrayList<>();		
  			Days[i]=-1;
  			//D2[i]=INF;
  		}
  	}
  
  
  
  	static  long pow(long a,long b)
  	{
  		//long mod=1000000007;
  		long pow=1;
  		long x=a;
  		while(b!=0)
  		{
  			if((b&1)!=0)pow=(pow*x)%mod;
  			x=(x*x)%mod;
  			b/=2;
  		}
  		return pow;
  	}
  
  	static long toggleBits(long x)//one's complement || Toggle bits
  	{
  		int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
  
  		return ((1<<n)-1)^x;
  	}
  
  	static int countBits(long a)
  	{
  		return (int)(Math.log(a)/Math.log(2)+1);
  	}
  
  	static long fact(long N)
  	{ 
  		long n=2; 
  		if(N<=1)return 1;
  		else
  		{
  			for(int i=3; i<=N; i++)n=(n*i)%mod;
  		}
  		return n;
  	}
  
  	static int kadane(int A[])
  	{
  		int lsum=A[0],gsum=A[0];
  		for(int i=1; i<A.length; i++)
  		{
  			lsum=Math.max(lsum+A[i],A[i]);
  			gsum=Math.max(gsum,lsum);
  		}
  		return gsum;
  	}
  
  	static void sort(int[] a) {
  		ArrayList<Integer> l=new ArrayList<>();
  		for (int i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
  
  	static boolean isPrime(long N)
  	{
  		if (N<=1)  return false; 
  		if (N<=3)  return true; 
  		if (N%2 == 0 || N%3 == 0) return false; 
  		for (int i=5; i*i<=N; i=i+6) 
  			if (N%i == 0 || N%(i+2) == 0) 
  				return false; 
  		return true; 
  	}
  	static void print(char A[])
  	{
  		for(char c:A)System.out.print(c+" ");
  		System.out.println();
  	}
  	static void print(boolean A[])
  	{
  		for(boolean c:A)System.out.print(c+" ");
  		System.out.println();
  	}
  	static void print(int A[])
  	{
  		for(int a:A)System.out.print(a+" ");
  		System.out.println();	
  	}
  	static void print(long A[])
  	{
  		for(long i:A)System.out.print(i+ " ");
  		System.out.println();
  
  	}
  	static void print(boolean A[][])
  	{
  		for(boolean a[]:A)print(a);
  	}
  	static void print(long A[][])
  	{
  		for(long a[]:A)print(a);
  	}
  	static void print(int A[][])
  	{
  		for(int a[]:A)print(a);
  	}
  	static void print(ArrayList<Integer> A)
  	{
  		for(int a:A)System.out.print(a+" ");
  		System.out.println();
  	}
  
  	static int i()
  	{
  		return in.nextInt();
  	}
  
  	static long l()
  	{
  		return in.nextLong();
  	}
  
  	static int[] input(int N){
  		int A[]=new int[N];
  		for(int i=0; i<N; i++)
  		{
  			A[i]=in.nextInt();
  		}
  		return A;
  	}
  
  	static long[] inputLong(int N)     {
  		long A[]=new long[N];
  		for(int i=0; i<A.length; i++)A[i]=in.nextLong();
  		return A;
  	}
  
  	static long GCD(long a,long b) 
  	{
  		if(b==0)
  		{
  			return a;
  		}
  		else return GCD(b,a%b );
  	}
  
  }
  class Pair23 implements Comparable<Pair23>
  {
  	int a,i;
  	Pair23(int a,int f)
  	{
  		this.i=f;
  		this.a=a;
  	}
  	public int compareTo(Pair23 X)
  	{
  		return X.a-this.a;
  	}
  }
  
  
  //Code For FastReader
  //Code For FastReader
  //Code For FastReader
  //Code For FastReader
  class FastReader
  {
  	BufferedReader br;
  	StringTokenizer st;
  	public FastReader()
  	{
  		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;
  	}
  
  }