#include <bits/stdc++.h>
using namespace std;
char s[300050];
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n, k;
    scanf("%d%d", &n, &k);
    scanf("%s", s);
    bool flag = true;
    for (int i = 0; i < k; i++) {
      int c0 = 0, c1 = 0;
      for (int j = i; j < n; j += k) {
        if (s[j] == '0') c0++;
        if (s[j] == '1') c1++;
      }
      if (c0 && c1) {
        flag = false;
        break;
      }
      if (c0 + c1) {
        char ch = c0 ? '0' : '1';
        for (int j = i; j < n; j += k) s[j] = ch;
      }
    }
    int c0 = 0, c1 = 0;
    for (int i = 0; i < n; i++) {
      if (i >= k) {
        if (s[i - k] == '0') c0--;
        if (s[i - k] == '1') c1--;
      }
      if (s[i] == '0') c0++;
      if (s[i] == '1') c1++;
      if (i >= k - 1 && c0 > k / 2 || c1 > k / 2) {
        flag = false;
        break;
      }
    }
    puts(flag ? "YES" : "NO");
  }
  return 0;
}
