#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <class T>
struct cLtraits_identity {
  using type = T;
};
template <class T>
using cLtraits_try_make_signed =
    typename conditional<is_integral<T>::value, make_signed<T>,
                         cLtraits_identity<T> >::type;
template <class S, class T>
struct cLtraits_common_type {
  using tS = typename cLtraits_try_make_signed<S>::type;
  using tT = typename cLtraits_try_make_signed<T>::type;
  using type = typename common_type<tS, tT>::type;
};
template <class S, class T>
inline auto max_L(S a, T b) -> typename cLtraits_common_type<S, T>::type {
  return (typename cLtraits_common_type<S, T>::type)a >=
                 (typename cLtraits_common_type<S, T>::type)b
             ? a
             : b;
}
inline int my_getchar() {
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if (s == e && e == 1048576) {
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if (s == e) {
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x) {
  int k;
  int m = 0;
  x = 0;
  for (;;) {
    k = my_getchar();
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      x = k - '0';
      break;
    }
  }
  for (;;) {
    k = my_getchar();
    if (k < '0' || k > '9') {
      break;
    }
    x = x * 10 + k - '0';
  }
  if (m) {
    x = -x;
  }
}
inline int rd_int(void) {
  int x;
  rd(x);
  return x;
}
struct MY_WRITER {
  char buf[1048576];
  int s;
  int e;
  MY_WRITER() {
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER() {
    if (s) {
      fwrite(buf, 1, s, stdout);
    }
  }
};
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a) {
  if (MY_WRITER_VAR.s == MY_WRITER_VAR.e) {
    fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a) { my_putchar(a); }
inline void wt_L(int x) {
  int s = 0;
  int m = 0;
  char f[10];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar('-');
  }
  while (s--) {
    my_putchar(f[s] + '0');
  }
}
template <class S, class T>
inline S moddw_L(S a, const T b) {
  a %= b;
  if (a < 0) {
    a += b;
  }
  return a;
}
template <class S, class T>
inline S chmin(S &a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}
int n;
int d;
int a[1000000 + 10];
int ns[1000000 + 10];
int f[1000000 + 10];
int qid[1000000 + 10];
int qsz;
int main() {
  int PiIOrLma;
  int AuJVIghY = rd_int();
  for (PiIOrLma = (0); PiIOrLma < (AuJVIghY); PiIOrLma++) {
    int GN2MVvSr, i;
    rd(n);
    rd(d);
    {
      int djQeACzd;
      for (djQeACzd = (0); djQeACzd < (n); djQeACzd++) {
        rd(a[djQeACzd]);
      }
    }
    for (i = (0); i < (n); i++) {
      ns[i] = 1;
      f[i] = 1073709056;
    }
    for (i = (0); i < (n); i++) {
      chmin(ns[i], (a[i] & a[(moddw_L((i - d), n))]));
    }
    qsz = 0;
    for (i = (0); i < (n); i++) {
      if (a[i] == 0) {
        qid[qsz++] = i;
        f[i] = 0;
      }
    }
    for (GN2MVvSr = (0); GN2MVvSr < (qsz); GN2MVvSr++) {
      auto &i = qid[GN2MVvSr];
      if (f[(moddw_L((i - d), n))] > f[i] + 1) {
        qid[qsz++] = (moddw_L((i - d), n));
        f[(moddw_L((i - d), n))] = f[i] + 1;
      }
    }
    int Btiq50RC;
    int lo08wcHZ;
    if (n == 0) {
      lo08wcHZ = 0;
    } else {
      lo08wcHZ = f[0];
      for (Btiq50RC = (1); Btiq50RC < (n); Btiq50RC++) {
        lo08wcHZ = max_L(lo08wcHZ, f[Btiq50RC]);
      }
    }
    int val = lo08wcHZ;
    if (val >= 1073709056) {
      val = -1;
    }
    wt_L(val);
    wt_L('\n');
  }
  return 0;
}
