#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>

typedef long long ll;

using namespace std;

// !!!aho
struct aho
{
	static const int A = 26;
	struct vert
	{
		int p;
		int ch[A];
		char c;
		int l;
		int cnt;
		int cnt1;
		//string s;
		void init(){
			p = 0;
			l = -1;
			c = -1;
			cnt = 0;
			for (int i=0; i<A; ++i)
				ch[i] = -1;
		}
	};
	vector<vert> verts;
	aho()
	{
		clear();
	}
	void clear()
	{
		verts.resize(1);
		verts[0].init();
		verts[0].p = 0;
	}
	void add(const string &s, int w)
	{
		int v = 0, tmp;
		for (int i=0; i<s.length(); ++i)
		{
			//printf("%d ", v);
			char c = s[i]-'a';
			tmp = verts[v].ch[c];
			if (tmp < 0)
			{
				tmp = verts.size();
				verts.resize(tmp+1);
				verts[tmp].init();
				verts[tmp].p = v;
				verts[tmp].c = c;
				verts[v].ch[c] = tmp;
			}
			v = tmp;
		}
		//printf("%d!\n", v);
		verts[v].cnt += w;
		//verts[v].s = s;
	}
	vector<bool> was;
	void move(int v, char c)
	{
		if (verts[v].ch[c] < 0)
		{
			//printf("move %d %d %d\n", v, (int)c, verts[v].l);
			if (v == 0)
			{
				verts[v].ch[c] = 0;
			}
			else
			{
				link(v);
				int l = verts[v].l;
				move(l, c);
				verts[v].ch[c] = verts[l].ch[c];
			}
		}
	}
	void link(int v)
	{
		//printf("link %d\n", v);
		if (verts[v].l < 0)
		{
			if (verts[v].p == 0)
			{
				verts[v].l = 0;
				return;
			}
			char c = verts[v].c;
			int p = verts[v].p;
			link(p);
			int l = verts[p].l;
			move(l, c);
			verts[v].l = verts[l].ch[c];
		}
	}
	void build(int v)
	{
		if (!was[v])
		{
			was[v] = true;
			link(v);
			for (int i=0; i<A; ++i)
			{
				move(v,i);
				build(verts[v].ch[i]);
			}
			int z = v;
			while (z != 0)
			{
				verts[v].cnt1 += verts[z].cnt;
				z = verts[z].l;
			}
		}
		/*if (verts[v].l < 0)
		{
			int l;
			if (verts[v].p == 0)
			{
				l = 0;
			}
			else
			{
				l = verts[verts[v].p].l;
				build(l);
				l = verts[l].ch[verts[v].c];
			}
			printf("link v=%d p=%d c=%d %d\n", v, verts[v].p, int(verts[v].c), l);
			verts[v].l = l;
			if (v == 0)
			{
				for (int i=0; i<A; ++i)
				{
					if (verts[0].ch[i] < 0)
						verts[0].ch[i] = 0;
				}
			}
			else
			{
				for (int i=0; i<A; ++i)
				{
					if (verts[v].ch[i] < 0)
					{
						verts[v].ch[i] = verts[l].ch[i];
						//printf("%d %d %d!\n", v, i, verts[v].ch[i]);
					}
				}
			}
			verts[v].cnt += verts[l].cnt;
			for (int i=0; i<A; ++i)
				build(verts[v].ch[i]);
		}*/
	}
	void print()
	{
		for (int v=0; v<verts.size(); ++v)
		{
			for (int i=0; i<A; ++i)
				printf("%d %d %d\n", v, i, verts[v].ch[i]);
		}
	}
};

void solve()
{
	vector<aho> ah(19);
	vector<pair<int, string>> q;
	int Q;
	scanf("%d", &Q);
	for (int i=0; i<Q; ++i)
	{
		int x;
		string s;
		cin >> x >> s;
		if (x == 3)
		{
			ll z = 0;
			int qn = q.size();
			for (int p=18; p>=0; --p)
			{
				if (qn & (1<<p))
				{
					int v = 0;
					aho &a = ah[p];
					for (int i=0; i<s.length(); ++i)
					{
						char c = s[i]-'a';
						v = a.verts[v].ch[c];
						//printf("%d %d\n", v, a.verts[v].cnt);
						z += a.verts[v].cnt;
						//if (a.verts[v].cnt)
						//	printf("%d %d\n", p, a.verts[v].cnt);
					}
				}
			}
			printf("%I64d\n", z);
			fflush(stdout);
		}
		else
		{
			q.push_back(pair<int, string>(x, s));
			int z = 0;
			int qn = q.size();
			for (int p=18; p>=0; --p)
			{
				if (qn & (1<<p))
				{
					if (!((qn&(qn-1))&(1<<p)))
					{
						//printf("build %d\n", p);
						aho &a = ah[p];
						a.clear();
						int nn = (1<<p);
						for (int i=0; i<nn; ++i)
						{
							a.add(q[z+i].second, q[z+i].first==1?1:-1);
						}
						a.was.resize(a.verts.size());
						fill(a.was.begin(), a.was.end(), false);
						a.build(0);
					}
				}
				else
				{
					//printf("clear %d\n", p);
					ah[p].clear();
				}
				z += qn & (1<<p);
			}
		}
	}
	/*string s;
	aho a;
	for (;;)
	{
		cin >> s;
		if (s == string("*"))
			break;
		a.add(s);
	}
	printf("build");
	a.build(0);
	a.print();
	cin >> s;
	int v = 0;
	for (int i=0; i<s.length(); ++i)
	{
		char c = s[i]-'a';
		v = a.verts[v].ch[c];
		printf("%d\n", v);
		int tmp = v;
		while (tmp != 0)
		{
			if (a.verts[tmp].cnt)
				printf("%d %s\n",i,a.verts[tmp].s.c_str());
			tmp = a.verts[tmp].l;
		}
	}*/
}

int main()
{
	solve();
	return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std;
const int gen=500;
int trie[300000][26],cp[300001],cnt,num_node;
string str[1001];
int cs[300001],num_q,flag,ans,nxt[300001];
int main()
{
	int i,j,s,p,q,id;
	scanf("%d",&num_q);
	cnt=0;
	num_node=1;
	memset(cp,0,sizeof(cp));
	while(num_q--)
	{
		cin>>flag>>str[cnt];
		if(flag!=3&&str[cnt].size()>gen)
		{
			if(flag==1)
			   cs[cnt]=1;
            else
               cs[cnt]=-1;
		    cnt++;
		}
		else if(flag!=3)
        {
        	id=0;
        	for(j=0;j<str[cnt].size();j++)
        	{
	        	if(trie[id][str[cnt][j]-'a']==0)
	        	   trie[id][str[cnt][j]-'a']=num_node++;
      	        id=trie[id][str[cnt][j]-'a'];
	        }
	        if(flag==1)
	           cp[id]=1;
            else
               cp[id]=0;
        }
        else
        {
        	ans=0;
        	for(j=0;j<str[cnt].size();j++)
        	{
	        	id=0;
	        	for(i=j;i<str[cnt].size();i++)
	        	{
	        		id=trie[id][str[cnt][i]-'a'];
	        		if(id==0)
	        		   break;
  		            ans+=cp[id];
	        	}
	        	if(j==0)
	        	  nxt[j]=-1;
       	        else
       	        {
        	        i=j-1;
					while(i>=0)
					{
						if(str[cnt][nxt[i]+1]==str[cnt][j])
						{
							nxt[j]=nxt[i]+1;
							break;
						}
						i=nxt[i];
					}
					if(i<0)
						nxt[j]=-1;
    	        }
	        }
	        for(i=0;i<cnt;i++)
	        {
	        	s=0;
        		for(j=0;j<str[cnt].size();j++)
        		{
		        	if(s<str[i].size()&&str[cnt][j]==str[i][s])
		        	    s++;
				    else
      	            {
      	            	 if(s==str[i].size())
      	            	    ans+=cs[i];
            	      	 if(s!=0)
            	      	 {
            	      	    s=nxt[s-1]+1;
            	      	    j--;
						 }
					}
		        }
		        if(s==str[i].size())
		           ans+=cs[i];
        	}
        	printf("%d\n",ans);
        	 fflush(stdout);
        }
	}
	return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std;
const int gen=500;
int trie[300000][26],cp[300001],cnt,num_node;
string str[1001];
int cs[300001],num_q,flag,ans,nxt[300001];
int main()
{
	int i,j,s,p,q,id;
	scanf("%d",&num_q);
	cnt=0;
	num_node=1;
	memset(cp,0,sizeof(cp));
	while(num_q--)
	{
		cin>>flag>>str[cnt];
		if(flag!=3&&str[cnt].size()>gen)
		{
			if(flag==1)
			   cs[cnt]=1;
            else
               cs[cnt]=-1;
		    cnt++;
		}
		else if(flag!=3)
        {
        	id=0;
        	for(j=0;j<str[cnt].size();j++)
        	{
	        	if(trie[id][str[cnt][j]-'a']==0)
	        	   trie[id][str[cnt][j]-'a']=num_node++;
      	        id=trie[id][str[cnt][j]-'a'];
	        }
	        if(flag==1)
	           cp[id]=1;
            else
               cp[id]=0;
        }
        else
        {
        	ans=0;
        	for(j=0;j<str[cnt].size();j++)
        	{
	        	id=0;
	        	for(i=j;i<str[cnt].size();i++)
	        	{
	        		id=trie[id][str[cnt][i]-'a'];
	        		if(id==0)
	        		   break;
  		            ans+=cp[id];
	        	}
	        	if(j==0)
	        	  nxt[j]=-1;
       	        else
       	        {
        	        i=j-1;
					while(i>=0)
					{
						if(str[cnt][nxt[i]+1]==str[cnt][j])
						{
							nxt[j]=nxt[i]+1;
							break;
						}
						i=nxt[i];
					}
					if(i<0)
						nxt[j]=-1;
    	        }
	        }
	        for(i=0;i<cnt;i++)
	        {
	        	s=0;
        		for(j=0;j<str[cnt].size();j++)
        		{
		        	if(s<str[i].size()&&str[cnt][j]==str[i][s])
		        	    s++;
				    else
      	            {
      	            	 if(s==str[i].size())
      	            	    ans+=cs[i];
            	      	 if(s!=0)
            	      	 {
            	      	    s=nxt[s-1]+1;
            	      	    j--;
						 }
					}
		        }
		        if(s==str[i].size())
		           ans+=cs[i];
        	}
        	printf("%d\n",ans);
        }
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int MAXN = 300000 + 10;
const int CASE_SIZE = 26;
const int S_SIZE = 1000;
const int B_SIZE = MAXN / S_SIZE + 10;
struct Trie{
	int child[MAXN][CASE_SIZE], value[MAXN],trieN,root;
	void init(){
		trieN = root = 0; value[root] = 0;
		memset(child[root], -1, sizeof(child[root]));
	}
	int newnode(){
		trieN++; value[trieN] = 0;
		memset(child[trieN], -1, sizeof(child[trieN]));
		return trieN;
	}
	void insert(char *s,int val){
		int x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				child[x][d] = newnode();
			}
			x = child[x][d];
		}
		value[x] += val;
	}
	int search(char *s){
		int sum = 0, x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				break;
			}
			x = child[x][d];
			sum += value[x];
		}
		return sum;
	}
}trie;
struct KMP{
	int Next[B_SIZE];
	void getNext(string s,int len){
		memset(Next, 0, sizeof(Next));
		int i = 0, j = -1; Next[0] = -1;
		while (i < len){
			if (j == -1 || s[i] == s[j]){
				++i; ++j;
				Next[i] = j;
			}
			else{
				j = Next[j];
			}
		}
	}
	int search(string s,char *str){
		int sum = 0, j = 0, lens = s.length(),lenstr=strlen(str);
		getNext(s, s.length());
		for (int i = 0; i < lenstr; i++){
			while (j>0 && s[j] != str[i]){
				j = Next[j];
			}
			if (s[j] == str[i]){
				j++;
			}
			if (j == lens){
				sum++; j = Next[j];
			}
		}
		return sum;
	}
}kmp;
string Bstr[B_SIZE];
char str[MAXN];
int cnt, Bvalue[B_SIZE];
int main() {
//#ifdef kirito
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
//#endif
//	int start = clock();
	int t, m,len,val;
	while (~scanf("%d", &m)) {
		trie.init(); cnt = 0; memset(Bvalue, 0, sizeof(Bvalue));
		for (int i = 1; i <= m; i++){
			scanf("%d%s", &t, str);
			len = strlen(str);
			if (t == 1 || t == 2){
				val = (t == 1 ? 1 : -1);
				if (len <= S_SIZE){
					trie.insert(str, val);
				}
				else{
					Bstr[cnt] = string(str);
					Bvalue[cnt++] = val;
				}
			}
			else{
				LL ans = 0;
				if (len <= S_SIZE){
					for (int i = 0; i < len; i++){
						ans += trie.search(str + i);
					}
				}
				else{
					for (int i = 0; i < cnt; i++){
						ans += kmp.search(Bstr[i], str)*Bvalue[i];
					}
				}
				printf("%I64d\n", ans);
				fflush(stdout);
			}
		}
	}
//#ifdef LOCAL_TIME
//	cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int MAXN = 300000 + 10;
const int CASE_SIZE = 26;
const int S_SIZE = 1000;
const int B_SIZE = MAXN / S_SIZE + 10;
struct Trie{
	int child[MAXN][CASE_SIZE], value[MAXN],trieN,root;
	void init(){
		trieN = root = 0; value[root] = 0;
		memset(child[root], -1, sizeof(child[root]));
	}
	int newnode(){
		trieN++; value[trieN] = 0;
		memset(child[trieN], -1, sizeof(child[trieN]));
		return trieN;
	}
	void insert(char *s,int val){
		int x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				child[x][d] = newnode();
			}
			x = child[x][d];
		}
		value[x] += val;
	}
	int search(char *s){
		int sum = 0, x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				break;
			}
			x = child[x][d];
			sum += value[x];
		}
		return sum;
	}
}trie;
struct KMP{
	int Next[MAXN];
	void getNext(string s,int len){
		memset(Next, 0, sizeof(Next));
		int i = 0, j = -1; Next[0] = -1;
		while (i < len){
			if (j == -1 || s[i] == s[j]){
				++i; ++j;
				Next[i] = j;
			}
			else{
				j = Next[j];
			}
		}
	}
	int search(string s,char *str){
		int sum = 0, j = 0, lens = s.length(),lenstr=strlen(str);
		getNext(s, s.length());
		for (int i = 0; i < lenstr; i++){
			while (j>0 && s[j] != str[i]){
				j = Next[j];
			}
			if (s[j] == str[i]){
				j++;
			}
			if (j == lens){
				sum++; j = Next[j];
			}
		}
		return sum;
	}
}kmp;
string Bstr[B_SIZE];
char str[MAXN];
int cnt, Bvalue[B_SIZE];
int main() {
//#ifdef kirito
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
//#endif
//	int start = clock();
	int t, m,len,val;
	while (~scanf("%d", &m)) {
		trie.init(); cnt = 0; memset(Bvalue, 0, sizeof(Bvalue));
		for (int i = 1; i <= m; i++){
			scanf("%d%s", &t, str);
			len = strlen(str);
			if (t == 1 || t == 2){
				val = (t == 1 ? 1 : -1);
				if (len <= S_SIZE){
					trie.insert(str, val);
				}
				else{
					Bstr[cnt] = string(str);
					Bvalue[cnt++] = val;
				}
			}
			else{
				LL ans = 0;
				for (int i = 0; i < len; i++){
					ans += trie.search(str + i);
				}
				for (int i = 0; i < cnt; i++){
					if (Bstr[i].length() > len){ continue; }
					ans += kmp.search(Bstr[i], str)*Bvalue[i];
				}
				printf("%I64d\n", ans);
				fflush(stdout);
			}
		}
	}
//#ifdef LOCAL_TIME
//	cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int MAXN = 300000 + 10;
const int CASE_SIZE = 26;
const int S_SIZE = 2000;
const int B_SIZE = MAXN / S_SIZE + 10;
struct Trie{
	int child[MAXN][CASE_SIZE], value[MAXN],trieN,root;
	void init(){
		trieN = root = 0; value[root] = 0;
		memset(child[root], -1, sizeof(child[root]));
	}
	int newnode(){
		trieN++; value[trieN] = 0;
		memset(child[trieN], -1, sizeof(child[trieN]));
		return trieN;
	}
	void insert(char *s,int val){
		int x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				child[x][d] = newnode();
			}
			x = child[x][d];
		}
		value[x] += val;
	}
	int search(char *s){
		int sum = 0, x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				break;
			}
			x = child[x][d];
			sum += value[x];
		}
		return sum;
	}
}trie;
struct KMP{
	int Next[B_SIZE];
	void getNext(string s,int len){
		memset(Next, 0, sizeof(Next));
		int i = 0, j = -1; Next[0] = -1;
		while (i < len){
			if (j == -1 || s[i] == s[j]){
				++i; ++j;
				Next[i] = j;
			}
			else{
				j = Next[j];
			}
		}
	}
	int search(string s,char *str){
		int sum = 0, j = 0, lens = s.length(),lenstr=strlen(str);
		getNext(s, s.length());
		for (int i = 0; i < lenstr; i++){
			while (j>0 && s[j] != str[i]){
				j = Next[j];
			}
			if (s[j] == str[i]){
				j++;
			}
			if (j == lens){
				sum++; j = Next[j];
			}
		}
		return sum;
	}
}kmp;
string Bstr[B_SIZE];
char str[MAXN];
int cnt, Bvalue[B_SIZE];
int main() {
//#ifdef kirito
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
//#endif
//	int start = clock();
	int t, m,len,val;
	while (~scanf("%d", &m)) {
		trie.init(); cnt = 0; memset(Bvalue, 0, sizeof(Bvalue));
		for (int i = 1; i <= m; i++){
			scanf("%d%s", &t, str);
			len = strlen(str);
			if (t == 1 || t == 2){
				val = (t == 1 ? 1 : -1);
				if (len <= S_SIZE){
					trie.insert(str, val);
				}
				else{
					Bstr[cnt] = string(str);
					Bvalue[cnt++] = val;
				}
			}
			else{
				LL ans = 0;
				for (int i = 0; i < len; i++){
					ans += trie.search(str + i);
				}
				for (int i = 0; i < cnt; i++){
					ans += kmp.search(Bstr[i], str)*Bvalue[i];
				}
				printf("%I64d\n", ans);
				fflush(stdout);
			}
		}
	}
//#ifdef LOCAL_TIME
//	cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<iostream>
using namespace std;
typedef long long int LL;
const int MAXN = 300000 + 10;
const int CASE_SIZE = 26;
const int MAX_SIZE = 2000;
struct trie{
	int root, trieN;
	int child[MAXN][CASE_SIZE], value[MAXN], fail[MAXN];
	int NewTrie(){
		value[trieN] = 0; fail[trieN] = 0;
		memset(child[trieN], 0, sizeof(child[trieN]));
		return trieN++;
	}
	void init(){
		trieN = 0;
		root = NewTrie();
	}
	void Insert(char *s){
		int x = root;
		for (int i = 0; s[i]; i++){
			if (!child[x][s[i] - 'a']){
				child[x][s[i] - 'a'] = NewTrie();
			}
			x = child[x][s[i] - 'a'];
		}
		value[x]++;
	}
	void Build(){
		int x; fail[root] = 0;  queue<int> q;
		for (int i = 0; i < CASE_SIZE; i++){
			if (child[root][i]){
				fail[child[root][i]] = root;
				q.push(child[root][i]);
			}
		}
		while (!q.empty()){
			x = q.front(); q.pop();
			for (int i = 0; i<CASE_SIZE; i++){
				if (child[x][i]){
					int f = fail[x];
					while (f&&!child[f][i]){
						f = fail[f];
					}
					fail[child[x][i]] = child[f][i];
					q.push(child[x][i]);
				}
			}
		}
	}
	int query(char *s){
		int x = root, temp,f=0;
		int ans = 0;
		for (int i = 0; s[i]; i++){
			while (x&&!child[x][s[i]-'a']){
				x = fail[x];
			}
			x = child[x][s[i] - 'a']; temp = x;
			while (temp){
				if (value[temp]){
					ans += value[temp];
				}
				temp = fail[temp];
			}
		}
		return ans;
	}
}ac, acbuf,Dac,Dacbuf;
void dfs(trie AC,int acR, trie BUF,int bufR){
	for (int i = 0; i < CASE_SIZE; i++){
		if (BUF.child[bufR][i]==-1){
			if (!AC.child[acR][i]){
				AC.child[acR][i] = AC.NewTrie();
			}
			int idx = AC.child[acR][i];
			AC.value[idx] += BUF.value[BUF.child[bufR][i]];
			dfs(AC,AC.child[acR][i], BUF,BUF.child[bufR][i]);
		}
	}
}
void Merge(trie AC,trie BUF){
	dfs(AC,AC.root,BUF,BUF.root);
	BUF.init(); AC.Build();
}
char str[MAXN];
int main() {
//#ifdef kirito
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
//#endif
//	int start = clock();
	int t, m;
	while (~scanf("%d", &m)) {
		ac.init(); acbuf.init(); Dac.init(); Dacbuf.init();
		for (int i = 0; i < m; i++){
			scanf("%d%s", &t, str);
			if (t == 1){
				acbuf.Insert(str);
				if (acbuf.trieN > MAX_SIZE){
					Merge(ac, acbuf);
				}
				else{
					acbuf.Build();
				}
			}
			else if (t == 2){
				Dacbuf.Insert(str);
				if (Dacbuf.trieN > MAX_SIZE){
					Merge(Dac, Dacbuf);
				}
				else{
					Dacbuf.Build();
				}
			}
			else{
				printf("%d\n", ac.query(str) + acbuf.query(str) - Dac.query(str) - Dacbuf.query(str));
				fflush(stdout);
			}
		}
	}
//#ifdef LOCAL_TIME
//	cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<iostream>
using namespace std;
typedef long long int LL;
const int MAXN = 300000 + 10;
const int CASE_SIZE = 26;
const int MAX_SIZE = 2000;
struct trie{
	int root, trieN;
	int child[MAXN][CASE_SIZE], value[MAXN], fail[MAXN];
	int NewTrie(){
		value[trieN] = 0; fail[trieN] = 0;
		memset(child[trieN], 0, sizeof(child[trieN]));
		return trieN++;
	}
	void init(){
		trieN = 0;
		memset(value, 0, sizeof(value));
		root = NewTrie();
	}
	void Insert(char *s){
		int x = root;
		for (int i = 0; s[i]; i++){
			if (!child[x][s[i] - 'a']){
				child[x][s[i] - 'a'] = NewTrie();
			}
			x = child[x][s[i] - 'a'];
		}
		value[x]++;
	}
	void Build(){
		int x; fail[root] = 0;  queue<int> q;
		for (int i = 0; i < CASE_SIZE; i++){
			if (child[root][i]){
				fail[child[root][i]] = root;
				q.push(child[root][i]);
			}
		}
		while (!q.empty()){
			x = q.front(); q.pop();
			for (int i = 0; i<CASE_SIZE; i++){
				if (child[x][i]){
					int f = fail[x];
					while (f&&!child[f][i]){
						f = fail[f];
					}
					fail[child[x][i]] = child[f][i];
					q.push(child[x][i]);
				}
			}
		}
	}
	int query(char *s){
		int x = root, temp,f=0;
		int ans = 0;
		for (int i = 0; s[i]; i++){
			while (x&&!child[x][s[i]-'a']){
				x = fail[x];
			}
			x = child[x][s[i] - 'a']; temp = x;
			while (temp){
				if (value[temp]){
					ans += value[temp];
				}
				temp = fail[temp];
			}
		}
		return ans;
	}
}ac, acbuf,Dac,Dacbuf;
void dfs(trie AC,int acR, trie BUF,int bufR){
	for (int i = 0; i < CASE_SIZE; i++){
		if (BUF.child[bufR][i]){
			if (!AC.child[acR][i]){
				AC.child[acR][i] = AC.NewTrie();
			}
			int idx = AC.child[acR][i];
			AC.value[idx] += BUF.value[BUF.child[bufR][i]];
			dfs(AC,AC.child[acR][i], BUF,BUF.child[bufR][i]);
		}
	}
}
void Merge(trie AC,trie BUF){
	dfs(AC,AC.root,BUF,BUF.root);
	BUF.init(); AC.Build();
}
char str[MAXN];
int main() {
	int t, m; scanf("%d", &m);
	ac.init(); acbuf.init(); Dac.init(); Dacbuf.init();
	for (int i = 0; i < m; i++){
		scanf("%d%s", &t, str);
		if (t == 1){
			acbuf.Insert(str);
			if (acbuf.trieN > MAX_SIZE){
				Merge(ac, acbuf);
			}
			else{
				acbuf.Build();
			}
		}
		else if (t == 2){
			Dacbuf.Insert(str);
			if (Dacbuf.trieN > MAX_SIZE){
				Merge(Dac, Dacbuf);
			}
			else{
				Dacbuf.Build();
			}
		}
		else{
			printf("%d\n", ac.query(str) + acbuf.query(str) - (Dac.query(str) + Dacbuf.query(str)));
			fflush(stdout);
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int MAXN = 300000 + 10;
const int CASE_SIZE = 26;
const int S_SIZE = 20;
const int B_SIZE = MAXN / S_SIZE + 10;
struct Trie{
	int child[MAXN][CASE_SIZE], value[MAXN],trieN,root;
	void init(){
		trieN = root = 0; value[root] = 0;
		memset(child[root], -1, sizeof(child[root]));
	}
	int newnode(){
		trieN++; value[trieN] = 0;
		memset(child[trieN], -1, sizeof(child[trieN]));
		return trieN;
	}
	void insert(char *s,int val){
		int x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				child[x][d] = newnode();
			}
			x = child[x][d];
		}
		value[x] += val;
	}
	int search(char *s){
		int sum = 0, x = root;
		for (int i = 0; s[i]; i++){
			int d = s[i] - 'a';
			if (child[x][d] == -1){
				break;
			}
			x = child[x][d];
			sum += value[x];
		}
		return sum;
	}
}trie;
struct KMP{
	int Next[B_SIZE];
	void getNext(string s,int len){
		memset(Next, 0, sizeof(Next));
		int i = 0, j = -1; Next[0] = -1;
		while (i < len){
			if (j == -1 || s[i] == s[j]){
				++i; ++j;
				Next[i] = j;
			}
			else{
				j = Next[j];
			}
		}
	}
	int search(string s,char *str){
		int sum = 0, j = 0, lens = s.length(),lenstr=strlen(str);
		getNext(s, s.length());
		for (int i = 0; i < lenstr; i++){
			while (j>0 && s[j] != str[i]){
				j = Next[j];
			}
			if (s[j] == str[i]){
				j++;
			}
			if (j == lens){
				sum++; j = Next[j];
			}
		}
		return sum;
	}
}kmp;
string Bstr[B_SIZE];
char str[MAXN];
int cnt, Bvalue[B_SIZE];
int main() {
//#ifdef kirito
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
//#endif
//	int start = clock();
	int t, m,len,val;
	while (~scanf("%d", &m)) {
		trie.init(); cnt = 0; memset(Bvalue, 0, sizeof(Bvalue));
		for (int i = 1; i <= m; i++){
			scanf("%d%s", &t, str);
			len = strlen(str);
			if (t == 1 || t == 2){
				val = (t == 1 ? 1 : -1);
				if (len <= S_SIZE){
					trie.insert(str, val);
				}
				else{
					Bstr[cnt] = string(str);
					Bvalue[cnt++] = val;
				}
			}
			else{
				LL ans = 0;
				for (int i = 0; i < len; i++){
					ans += trie.search(str + i);
				}
				for (int i = 0; i < cnt; i++){
					ans += kmp.search(Bstr[i], str)*Bvalue[i];
				}
				printf("%I64d\n", ans);
				fflush(stdout);
			}
		}
	}
//#ifdef LOCAL_TIME
//	cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k=2,fl=1,st=-1;
        cin>>n>>k;
        // n=300000;
        string str="";
        cin>>str;
        // for(int i=0;i<n;i++)str+='?';
        // cout<<str.length();
        unordered_map<char,int>mp,mp2;
        int ct=0,i=0;
        while(i<k)
        {
            char c='?';
            for(int j=i;j<n;j+=k)
            {
                // cout<<j<<endl;
                // cout<<ct++<<" ";
                if(c=='?' && str[j]!='?')c=str[j];
                if(c!='?' && str[j]!='?' && str[j]!=c)
                {

                    fl=0;
                    break;
                }
            }
            if(i==1)break;
            if(!fl)
            break;
            if(c!='?')
            {
                for(int j=i;j<n;j+=k)str[j]=c;
            }
            i++;
        }
        if(fl)
        {
            mp.clear();
            for(int i=0;i<n;i++)
            {
                mp[str[i]]++;
                if(i-k>=0)mp[str[i-k]]--;
                if(i>=k-1)
                {
                    if((mp['0']+mp['1']==k && mp['0']!=mp['1']) || (mp['1']>k/2 ||mp['0']>k/2))
                    {
                        fl=0;
                        break;
                    }
                }
            }
        }
        if(fl)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    for(int kk=0;kk<t;kk++)
    // while(t--)
    {
        int n,k=2,fl=1,st=-1;
        cin>>n>>k;
        // n=300000;
        string str="";
        cin>>str;
        if(kk==299)cout<<str<<endl;
        // for(int i=0;i<n;i++)str+='?';
        // cout<<str.length();
        unordered_map<char,int>mp,mp2;
        int ct=0,i=0;
        while(i<k)
        {
            char c='?';
            for(int j=i;j<n;j+=k)
            {
                // cout<<j<<endl;
                // cout<<ct++<<" ";
                if(c=='?' && str[j]!='?')c=str[j];
                if(c!='?' && str[j]!='?' && str[j]!=c)
                {

                    fl=0;
                    break;
                }
            }
            if(i==1)break;
            if(!fl)
            break;
            if(c!='?')
            {
                for(int j=i;j<n;j+=k)str[j]=c;
            }
            i++;
        }
        if(fl)
        {
            mp.clear();
            for(int i=0;i<n;i++)
            {
                mp[str[i]]++;
                if(i-k>=0)mp[str[i-k]]--;
                if(i>=k-1)
                {
                    if((mp['0']+mp['1']==k && mp['0']!=mp['1']) || (mp['1']>k/2 ||mp['0']>k/2))
                    {
                        fl=0;
                        break;
                    }
                }
            }
        }
        if(fl)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i < k; i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j = j + k) {
				if (s[j] == '?') {
					s[j] = s[i];
				}
				else if (s[j] != '?' && s[j] != s[i]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}
	for (ll i = 0; i < k; i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j = j + k) {
				if (s[j] != '?') {

					s[i] = s[j];
					break;
				}
			}
		}
	}

	ll z = 0, o = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z += 1;
		}
		if (s[i] == '1') {
			o += 1;
		}
	}

	//cout << z << " " << o << "\n";

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}
	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i < s.length(); i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] != '?') {
					s[i] = s[j];
					break;
				}
			}
		}
	}

	for (ll i = 0; i < s.length(); i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] == '?') {
					s[j] = s[i];
					break;
				}
				else if (s[i] != s[j]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}
	ll z = 0, o = 0, q = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z++;
		}
		if (s[i] == '1') {
			o++;
		}
		if (s[i] == '?') {
			q++;
		}
	}

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}

	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i <k; i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] != '?') {
					s[i] = s[j];
					break;
				}
			}
		}
	}

	for (ll i = 0; i < s.length(); i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] == '?') {
					s[j] = s[i];
					break;
				}
				else if (s[i] != s[j]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}
	ll z = 0, o = 0, q = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z++;
		}
		else if (s[i] == '1') {
			o++;
		}
	}

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}

	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i < k; i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j = j + k) {
				if (s[j] != '?') {
					s[i] = s[j];
					break;
				}
			}
		}
	}

	for (ll i = 0; i < k; i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j = j + k) {
				if (s[j] == '?') {
					s[j] = s[i];
				}
				else if (s[j] != '?' && s[j] != s[i]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}

//	cout << s << "\n";

	ll z = 0, o = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z += 1;
		}
		if (s[i] == '1') {
			o += 1;
		}
	}

	//cout << z << " " << o << "\n";

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}
	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i <k; i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] != '?') {
					s[i] = s[j];
					break;
				}
			}
		}
	}

	for (ll i = 0; i < k; i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] == '?') {
					s[j] = s[i];
					break;
				}
				else if (s[i] != s[j]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}
	ll z = 0, o = 0, q = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z++;
		}
		else if (s[i] == '1') {
			o++;
		}
	}

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}

	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i <k; i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] != '?') {
					s[i] = s[j];
					break;
				}
			}
		}
	}

	for (ll i = 0; i <k; i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] == '?') {
					s[j] = s[i];
					break;
				}
				else if (s[i] != s[j]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}
	ll z = 0, o = 0, q = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z++;
		}
		else if (s[i] == '1') {
			o++;
		}
	}

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}

	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  vi vector<long long int>
#define  set1 set<long long int>
#define  pb push_back
#define  map1 map<long long int,long long int>
#define  ll    long long int
#define  mp make_pairstd::vector<char> v;
#define  inf INT_MAX
#define  minf INT_MIN
#define  setbits(x) __builtin_popcountll(x)
#define  pres(x,y) fixed<<setprecision(y)<<x
#define  pair1 pair<long long int,long long int>
#define  mk(arr,n,type) type *arr=new type[n];
#define  mod 1000000007
#define  pi  3.1415926535897932384626433832
#define  ins insert
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//find_by_order() --> returns an iterator to the k-th largest element (counting from zero)
//order_of_key()  --> the number of items in a set that are strictly smaller than our item.
#define  w(t) long long int t;cin>>t;while(t--)

void solve() {

	ll n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	for (ll i = 0; i < s.length(); i++) {
		if (s[i] == '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] != '?') {
					s[i] = s[j];
					break;
				}
			}
		}
	}

	for (ll i = 0; i < s.length(); i++) {
		if (s[i] != '?') {
			for (ll j = i + k; j < n; j += k) {
				if (s[j] == '?') {
					s[j] = s[i];
					break;
				}
				else if (s[i] != s[j]) {
					cout << "NO" << "\n";
					return;
				}
			}
		}
	}
	ll z = 0, o = 0, q = 0;
	for (ll i = 0; i < k; i++) {
		if (s[i] == '0') {
			z++;
		}
		else if (s[i] == '1') {
			o++;
		}
	}

	if (z <= k / 2 && o <= k / 2) {
		cout << "YES" << "\n";
		return;
	}

	cout << "NO" << "\n";

}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	w(t) {
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i;
		   while(j<n)
		     {
			      if(s[j]!='?')
				     s[i]=s[j];
				   j+=k;

			 }
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   if(abs(c1-c2)==c3)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   bool check = true;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i+k;
		   while(j<n)
		     {
			      if(s[j]!='?'){
				    if(s[j-k]!='?'&&s[j-k]!=s[j])
					   check = false;
				    s[j-k]=s[j];
				  }
				   j+=k;

			 }
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   if(c1+c2+c3==k&&check)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   bool check = true;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i;
		   vector<char> coll;
		   while(j<n)
		     {
			    if(s[j]!='?')
				  coll.push_back(s[j]);
				  j+=k;

			 }
			 for(auto i:coll)
			   if(i!=coll[0])
			      check=false;
			 if(coll.size())
			    s[i] = coll[0];
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   int com = c3-abs(c1-c2);
	   if(com>=0&&com%2==0&&check)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   bool check = true;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i+k;
		   while(j<n)
		     {
			      if(s[j]!='?'){
				    if(s[j-k]!='?'&&s[j-k]!=s[j])
					   check = false;
				    s[j-k]=s[j];
				  }
				   j+=k;

			 }
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   int com = c3-abs(c1-c2);
	   if(com>=0&&com%2==0&&check)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   bool check = true;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i+k;
		   while(j<n)
		     {
			      if(s[j]!='?'){
				    if(s[j-k]!='?'&&s[j-k]!=s[j])
					   check = false;
				    s[j-k]=s[j];
				  }
				   j+=k;

			 }
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   if(abs(c1-c2)==c3&&check)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   bool check = true;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i;
		   vector<char> coll;
		   while(j<n)
		     {
			    if(s[j]!='?')
				  coll.push_back(s[j]);

			 }
			 for(auto i:coll)
			   if(i!=coll[0])
			      check=false;
			 if(coll.size())
			    s[i] = coll[0];
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   int com = c3-abs(c1-c2);
	   if(com>=0&&com%2==0&&check)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
   int t;
   cin >>t;
   while(t--){
	   int n,k;
	   cin >> n >> k;
	   string s;
	   cin >>s;
	   bool check = true;
	   int c1 = 0,c2 = 0,c3=0;
	   for(int i=0;i<k;i++){
		   int j = i+k;
		   while(j<n)
		     {
			      if(s[j]!='?'){
				    if(s[j-k]!='?'&&s[j-k]!=s[j])
					   check = false;
				    s[i]=s[j];
				  }
				   j+=k;

			 }
			 if(s[i]=='0')
			    c1++;
		    if(s[i]=='1')
			   c2++;
			if(s[i]=='?')
			  c3++;
	   }
	   int com = c3-abs(c1-c2);
	   if(com>=0&&com%2==0&&check)
	     cout << "YES" << endl;
	   else
	      cout << "NO" << endl;

   }
}


 /*                    (
                     (  ) (
                      )    )
         |||||||     (  ( (
        ( O   O )        )
 ____oOO___(_)___OOo____(
(_______________________)
           JOINT           ░█─░█ ░█▀▀▀ ▀█▀ ░█▀▀▀█ ░█▀▀▀ ░█▄─░█ ░█▀▀█ ░█▀▀▀ ░█▀▀█ ░█▀▀█ █ ░█▀▀▀█ 　 ░█▀▀█ ░█▀▀▀█ ░█▀▀▄ ░█▀▀▀
                           ░█▀▀█ ░█▀▀▀ ░█─ ─▀▀▀▄▄ ░█▀▀▀ ░█░█░█ ░█▀▀▄ ░█▀▀▀ ░█▄▄▀ ░█─▄▄ ─ ─▀▀▀▄▄ 　 ░█─── ░█──░█ ░█─░█ ░█▀▀▀
                           ░█─░█ ░█▄▄▄ ▄█▄ ░█▄▄▄█ ░█▄▄▄ ░█──▀█ ░█▄▄█ ░█▄▄▄ ░█─░█ ░█▄▄█ ─ ░█▄▄▄█ 　 ░█▄▄█ ░█▄▄▄█ ░█▄▄▀ ░█▄▄▄*/

#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//________________( ﾒ ▼_▼)_______<THE QUESTION IS NOT WHO'S GOING TO LET ME , IT'S WHO IS GOING TO STOP ME!>________( ▼_▼ ﾒ )___________________________________________________________________

signed main()
{
test{
 int n,k;
 cin>>n>>k;
 string s;
 cin>>s;
 set<char> se[k];
 loop(i,0,k){
 for(int j=i;j<n;j+=k){
        if(s[j]!='?')se[i].insert(s[j]);
 }
 }
 int f=0;
 loop(i,0,k){
 if(se[i].size()==2)f=1;
 }
 if(f)cout<<"NO"<<endl;
 else{
    loop(i,0,k){
    for(int j=i;j<n;j+=k){
        if(s[j]=='?'&&se[i].size()==1){
          auto val=*(se[i].begin());
          s[j]=val;
        }
   }
    }
    int poss=0,o=0;
    loop(i,0,k){
    if(s[i]=='1')o++;
    else if(s[i]=='?')poss++;
    }
    if(o>(k/2))cout<<"NO"<<endl;
    else if(o<=(k/2)&&((k/2)-o)<=poss)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
 }
}
}


// HEISENBERG'S CODE
#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//___________________________________<THE QUESTION IS NOT WHO'S GOING TO LET ME IT'S WHO IS GOING TO STOP ME!>_________________________________________________________________________

signed main()
{
test{
 int n,k;
 cin>>n>>k;
 string s;
 cin>>s;
 set<char> se[k];
 loop(i,0,k){
 for(int j=i;j<n;j+=k){
        if(s[j]!='?')se[i].insert(s[j]);
 }
 }
 int f=0;
 loop(i,0,k){
 if(se[i].size()==2)f=1;
 }
 if(f)cout<<"NO"<<endl;
 else{
    loop(i,0,k){
    for(int j=i;j<n;j+=k){
        if(s[j]=='?'&&se[i].size()==1){
          auto val=*(se[i].begin());
          s[j]=val;
        }
   }
    }
    int poss=0,o=0;
    loop(i,0,k){
    if(s[i]=='1')o++;
    else if(s[i]=='?')poss++;
    }
    if(o>(k/2))cout<<"NO"<<endl;
    else if(o<=(k/2)&&((k/2)-o)<=poss)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
 }
}
}


 /*                    (
                     (  ) (
                      )    )
         |||||||     (  ( (
        ( O   O )        )
 ____oOO___(_)___OOo____(
(_______________________)
           JOINT           ░█─░█ ░█▀▀▀ ▀█▀ ░█▀▀▀█ ░█▀▀▀ ░█▄─░█ ░█▀▀█ ░█▀▀▀ ░█▀▀█ ░█▀▀█ █ ░█▀▀▀█ 　 ░█▀▀█ ░█▀▀▀█ ░█▀▀▄ ░█▀▀▀
                           ░█▀▀█ ░█▀▀▀ ░█─ ─▀▀▀▄▄ ░█▀▀▀ ░█░█░█ ░█▀▀▄ ░█▀▀▀ ░█▄▄▀ ░█─▄▄ ─ ─▀▀▀▄▄ 　 ░█─── ░█──░█ ░█─░█ ░█▀▀▀
                           ░█─░█ ░█▄▄▄ ▄█▄ ░█▄▄▄█ ░█▄▄▄ ░█──▀█ ░█▄▄█ ░█▄▄▄ ░█─░█ ░█▄▄█ ─ ░█▄▄▄█ 　 ░█▄▄█ ░█▄▄▄█ ░█▄▄▀ ░█▄▄▄*/

#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//________________________( ﾒ ▼_▼)_______<THE QUESTION IS NOT WHO'S GOING TO LET ME , IT'S WHO IS GOING TO STOP ME!>________( ▼_▼ ﾒ )___________________________________________________________________

signed main()
{
test{
 int n,k;
 cin>>n>>k;
 string s;
 cin>>s;
 set<char> se[k];
 loop(i,0,k){
 for(int j=i;j<n;j+=k){
        if(s[j]!='?')se[i].insert(s[j]);
 }
 }
 int f=0;
 loop(i,0,k){
 if(se[i].size()==2)f=1;
 }
 if(f)cout<<"NO"<<endl;
 else{
    loop(i,0,k){
    for(int j=i;j<n;j+=k){
        if(s[j]=='?'&&se[i].size()==1){
          auto val=*(se[i].begin());
          s[j]=val;
        }
   }
    }
    int poss=0,o=0;
    loop(i,0,k){
    if(s[i]=='1')o++;
    else if(s[i]=='?')poss++;
    }
    if(o>(k/2))cout<<"NO"<<endl;
    else if(o<=(k/2)&&((k/2)-o)<=poss)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
 }
}
}


 /*                    (
                     (  ) (
                      )    )
         |||||||     (  ( (
        ( O   O )        )
 ____oOO___(_)___OOo____(
(_______________________)
           JOINT           ░█─░█ ░█▀▀▀ ▀█▀ ░█▀▀▀█ ░█▀▀▀ ░█▄─░█ ░█▀▀█ ░█▀▀▀ ░█▀▀█ ░█▀▀█ █ ░█▀▀▀█ 　 ░█▀▀█ ░█▀▀▀█ ░█▀▀▄ ░█▀▀▀
                           ░█▀▀█ ░█▀▀▀ ░█─ ─▀▀▀▄▄ ░█▀▀▀ ░█░█░█ ░█▀▀▄ ░█▀▀▀ ░█▄▄▀ ░█─▄▄ ─ ─▀▀▀▄▄ 　 ░█─── ░█──░█ ░█─░█ ░█▀▀▀
                           ░█─░█ ░█▄▄▄ ▄█▄ ░█▄▄▄█ ░█▄▄▄ ░█──▀█ ░█▄▄█ ░█▄▄▄ ░█─░█ ░█▄▄█ ─ ░█▄▄▄█ 　 ░█▄▄█ ░█▄▄▄█ ░█▄▄▀ ░█▄▄▄*/

#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//___________________________________<THE QUESTION IS NOT WHO'S GOING TO LET ME , IT'S WHO IS GOING TO STOP ME!>_________________________________________________________________________

signed main()
{
test{
 int n,k;
 cin>>n>>k;
 string s;
 cin>>s;
 set<char> se[k];
 loop(i,0,k){
 for(int j=i;j<n;j+=k){
        if(s[j]!='?')se[i].insert(s[j]);
 }
 }
 int f=0;
 loop(i,0,k){
 if(se[i].size()==2)f=1;
 }
 if(f)cout<<"NO"<<endl;
 else{
    loop(i,0,k){
    for(int j=i;j<n;j+=k){
        if(s[j]=='?'&&se[i].size()==1){
          auto val=*(se[i].begin());
          s[j]=val;
        }
   }
    }
    int poss=0,o=0;
    loop(i,0,k){
    if(s[i]=='1')o++;
    else if(s[i]=='?')poss++;
    }
    if(o>(k/2))cout<<"NO"<<endl;
    else if(o<=(k/2)&&((k/2)-o)<=poss)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
 }
}
}


// HEISENBERG'S CODE
#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//___________________________________<THE QUESTION IS NOT WHO'S GOING TO LET ME IT'S WHO IS GOING TO STOP ME!>_________________________________________________________________________

signed main()
{
test{
 int n,k;
 cin>>n>>k;
 string s;
 cin>>s;
 set<char> se[k];
 loop(i,0,k){
 for(int j=i;j<n;j+=k){
        if(s[j]!='?')se[i].insert(s[j]);
 }
 }
 int f=0;
 loop(i,0,k){
 if(se[i].size()==2)f=1;
 }
 if(f)cout<<"NO"<<endl;
 else cout<<"YES"<<endl;
}
}


//      ░█─░█ ░█▀▀▀ ▀█▀ ░█▀▀▀█ ░█▀▀▀ ░█▄─░█ ░█▀▀█ ░█▀▀▀ ░█▀▀█ ░█▀▀█ █ ░█▀▀▀█ 　 ░█▀▀█ ░█▀▀▀█ ░█▀▀▄ ░█▀▀▀
//      ░█▀▀█ ░█▀▀▀ ░█─ ─▀▀▀▄▄ ░█▀▀▀ ░█░█░█ ░█▀▀▄ ░█▀▀▀ ░█▄▄▀ ░█─▄▄ ─ ─▀▀▀▄▄ 　 ░█─── ░█──░█ ░█─░█ ░█▀▀▀
//      ░█─░█ ░█▄▄▄ ▄█▄ ░█▄▄▄█ ░█▄▄▄ ░█──▀█ ░█▄▄█ ░█▄▄▄ ░█─░█ ░█▄▄█ ─ ░█▄▄▄█ 　 ░█▄▄█ ░█▄▄▄█ ░█▄▄▀ ░█▄▄▄

#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//___________________________________<THE QUESTION IS NOT WHO'S GOING TO LET ME , IT'S WHO IS GOING TO STOP ME!>_________________________________________________________________________

signed main()
{
test{
 int n,k;
 cin>>n>>k;
 string s;
 cin>>s;
 set<char> se[k];
 loop(i,0,k){
 for(int j=i;j<n;j+=k){
        if(s[j]!='?')se[i].insert(s[j]);
 }
 }
 int f=0;
 loop(i,0,k){
 if(se[i].size()==2)f=1;
 }
 if(f)cout<<"NO"<<endl;
 else{
    loop(i,0,k){
    for(int j=i;j<n;j+=k){
        if(s[j]=='?'&&se[i].size()==1){
          auto val=*(se[i].begin());
          s[j]=val;
        }
   }
    }
    int poss=0,o=0;
    loop(i,0,k){
    if(s[i]=='1')o++;
    else if(s[i]=='?')poss++;
    }
    if(o>(k/2))cout<<"NO"<<endl;
    else if(o<=(k/2)&&((k/2)-o)<=poss)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
 }
}
}


#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef long double ld;
const ld pi = 4*atan((ld)1);
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll nax = 0;

ll t, k, n;
string s;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	cin >> t;
	while(t--){
		cin >> n >> k >> s;
		ll one = 0, zero = 0;
		for(ll i = 0; i < k; i++){
			ll cek1 = 0, cek0 = 0;
			for(ll j = i; j < n; j += k){
				if(s[j] == '1'){
					cek1++;
				}
				else if(s[j] == '0'){
					cek0++;
				}
			}
			if(cek1 > 0 && cek0 > 0){
				cout << "NO\n";
				goto done;
			}
			else if(cek1 || cek0){
				for(ll j = i; j < n; j += k){
					if(cek1){
						s[j] = '1';
					}
					else{
						s[j] = '0';
					}
				}
			}
		}
		for(ll i = 0; i < n; i++){
			if(s[i] == '1'){
				one++;
			}
			else if(s[i] == '0'){
				zero++;
			}
			if(i >= k - 1){
				if(i > k - 1){
					if(s[i-k] == '1'){
						one--;
					}
					else if(s[i-k] == '0'){
						zero--;
					}
				}
			}
			if(one > k / 2 || zero > k / 2){
				cout << "NO\n";
				goto done;
			}
		}
		cout << "YES\n";
		done:;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef long double ld;
const ld pi = 4*atan((ld)1);
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll nax = 0;

ll t, k, n;
string s;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	cin >> t;
	while(t--){
		cin >> n >> k >> s;
		for(ll i = 0; i < k; i++){
			ll cek1 = 0, cek0 = 0;
			for(ll j = i; j < n; j += k){
				if(s[j] == '1'){
					cek1++;
				}
				else if(s[j] == '0'){
					cek0++;
				}
			}
			if(cek1 > 0 && cek0 > 0){
				cout << "NO\n";
				goto done;
			}
			else if(cek1 || cek0){
				for(ll j = i; j < n; j += k){
					if(cek1){
						s[j] = '1';
					}
					else{
						s[j] = '0';
					}
				}
			}
		}
		for(ll i = k - 1; i < n; i++){
			ll one = 0, zero = 0;
			for(ll j = i - k + 1; j <= i; j++){
				if(s[j] == '1'){
					one++;
				}
				else if(s[j] == '0'){
					zero++;
				}
			}
			if(one > k / 2 || zero > k / 2){
				cout << "NO\n";
				goto done;
			}
		}
		cout << "YES\n";
		done:;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef long double ld;
const ld pi = 4*atan((ld)1);
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll nax = 0;

ll t, k, n;
string s;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	cin >> t;
	while(t--){
		cin >> n >> k >> s;
		ll cek = 1;
		while(cek > 0){
			cek = 0;
			for(ll i = k - 1; i < n; i++){
				ll one = 0, zero = 0;
				for(ll j = i - k + 1; j <= i; j++){
					if(s[j] == '1'){
						one++;
					}
					else if(s[j] == '0'){
						zero++;
					}
				}
				if(one > k / 2 || zero > k / 2){
					cout << "NO\n";
					goto done;
				}
				else if(one == k / 2 && zero < k / 2){
					cek++;
					for(ll j = i - k + 1; j <= i; j++){
						if(s[j] == '?'){
							s[j] = '0';
						}
					}
				}
				else if(zero == k / 2 && one < k / 2){
					cek++;
					for(ll j = i - k + 1; j <= i; j++){
						if(s[j] == '?'){
							s[j] = '1';
						}
					}
				}
			}
		}
		cout << "YES\n";
		done:;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef long double ld;
const ld pi = 4*atan((ld)1);
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll nax = 0;

ll t, k, n;
string s;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	cin >> t;
	while(t--){
		cin >> n >> k >> s;
		ll cek = 1;
		while(cek > 0){
			cek = 0;
			for(ll i = k - 1; i < n; i++){
				ll one = 0, zero = 0;
				for(ll j = i - k + 1; j <= i; j++){
					if(s[j] == '1'){
						one++;
					}
					else if(s[j] == '0'){
						zero++;
					}
				}
				if(one > k / 2 || zero > k / 2){
					cout << "NO\n";
					goto done;
				}
				else if(one == k / 2 && zero < k / 2){
					cek++;
					for(ll j = i - k + 1; j <= i; j++){
						if(s[j] == '?'){
							s[j] = '0';
						}
					}
				}
				else if(zero == k / 2 && one < k / 2){
					cek++;
					for(ll j = i - k + 1; j <= i; j++){
						if(s[j] == '?'){
							s[j] = '1';
						}
					}
				}
			}
		}
		for(ll i = 0; i < s.size() - k; i++){
			if(s[i] == '1' && s[i+k] == '0'){
				cout << "NO\n";
				goto done;
			}
			else if(s[i] == '0' && s[i+k] == '1'){
				cout << "NO\n";
				goto done;
			}
		}
		cout << "YES\n";
		done:;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef long double ld;
const ld pi = 4*atan((ld)1);
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll nax = 0;

ll t, k, n;
string s;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	cin >> t;
	while(t--){
		cin >> n >> k >> s;
		for(ll i = k - 1; i < n; i++){
			ll one = 0, zero = 0;
			for(ll j = i - k + 1; j <= i; j++){
				if(s[j] == '1'){
					one++;
				}
				else if(s[j] == '0'){
					zero++;
				}
			}
			if(one >= k / 2){
				for(ll j = i - k + 1; j <= i; j++){
					if(s[j] == '?'){
						s[j] = '0';
					}
				}
			}
			else if(zero >= k / 2){
				for(ll j = i - k + 1; j <= i; j++){
					if(s[j] == '?'){
						s[j] = '1';
					}
				}
			}
		}

		for(ll i = k - 1; i < n; i++){
			ll one = 0, zero = 0;
			for(ll j = i - k + 1; j <= i; j++){
				if(s[j] == '1'){
					one++;
				}
				else if(s[j] == '0'){
					zero++;
				}
			}
			if(one > k / 2 || zero > k / 2){
				cout << "NO\n";
				goto done;
			}
		}
		cout << "YES\n";
		done:;
	}
}


// </> : towhid1zaman

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define endl "\n"
#define sqr(a) ((a)*(a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define deb(x) cout<<#x <<"="<<x<<endl;
#define present(v,x) ((v).find(x)!=(v).end())
#define _ ios::sync_with_stdio(0);cin.tie(0);
#define sp(k) cout<<setprecision(k)<<fixed;
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) (v).begin(),(v).end()

#define rep(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=(a);i<=(b);++i)
#define irep(i,b,a) for(int i=(b);i>=(a);--i)

#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)),(v).erase(unique(all(v)),(v).end())
#define yn(k) if(k)puts("YES");else puts("NO");

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f;// (int)3e18;
const int maxn = 200005;

/*
 *
 */

void task(){
	int n, k; cin >> n >> k;
	string input; cin >> input;
	bool flag = true;
		for(int i = k; i < n; ++i)
			if(input[i] != '?') {
				if(input[i % k] != '?' && input[i] != input[i % k])
					{ flag = false; break; }
				input[i % k] = input[i];
			}
		if(flag) {
			int cnt0 = 0, cnt1 = 0;
			for(int i = 0; i < k; ++i) {
				if(input[i] == '0') ++cnt0;
				else if(input[i] == '1') ++cnt1;
			}
			flag = flag && cnt0 <= k / 2 && cnt1 <= k / 2;
		}

	yn(flag);
}

int main(){
        _
        #ifdef OJ
        freopen("inputf.in", "r", stdin);
        freopen("outputf.in", "w", stdout);
        #endif

        int T = 1; cin >> T;
        while(T--){
            task();
        }

return 0;
}


// </> : towhid1zaman

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define endl "\n"
#define sqr(a) ((a)*(a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define deb(x) cout<<#x <<"="<<x<<endl;
#define present(v,x) ((v).find(x)!=(v).end())
#define _ ios::sync_with_stdio(0);cin.tie(0);
#define sp(k) cout<<setprecision(k)<<fixed;
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) (v).begin(),(v).end()

#define rep(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=(a);i<=(b);++i)
#define irep(i,b,a) for(int i=(b);i>=(a);--i)

#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)),(v).erase(unique(all(v)),(v).end())
#define yn(k) if(k)puts("YES");else puts("NO");

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f;// (int)3e18;
const int maxn = 200005;

/*
 *
 */

void task(){
	int n, k; cin >> n >> k;
	string input; cin >> input;
	bool flag = true;
		for(int i = k; i < n; ++i)
			if(input[i] != '?') {
				if(input[i % k] != '?' && input[i] != input[i % k])
					{ flag = false; break; }
				input[i % k] = input[i];
			}
		if(flag) {
			int cnt0 = 0, cnt1 = 0;
			for(int i = 0; i < k; ++i) {
				if(input[i] == '0') ++cnt0;
				else if(input[i] == '1') ++cnt1;
			}
			flag = flag && cnt0 <= k / 2 && cnt1 <= k / 2;
		}
	yn(flag);
}

int main(){
        _
        #ifdef OJ
        freopen("inputf.in", "r", stdin);
        freopen("outputf.in", "w", stdout);
        #endif

        int T = 1; cin >> T;
        while(T--){
            task();
        }

return 0;
}


// </> : towhid1zaman

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define endl "\n"
#define sqr(a) ((a)*(a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define deb(x) cout<<#x <<"="<<x<<endl;
#define present(v,x) ((v).find(x)!=(v).end())
#define _ ios::sync_with_stdio(0);cin.tie(0);
#define sp(k) cout<<setprecision(k)<<fixed;
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) (v).begin(),(v).end()

#define rep(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=(a);i<=(b);++i)
#define irep(i,b,a) for(int i=(b);i>=(a);--i)

#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)),(v).erase(unique(all(v)),(v).end())
#define yn(k) if(k)puts("YES");else puts("NO");

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f;// (int)3e18;
const int maxn = 200005;

/*
 *
 */

void task(){
	int n, k; cin >> n >> k;
	string input; cin >> input;
	bool f = true;
	for(int i = k; i<n; i++){
	if(input[i] != '?') {
			if(input[i % k] != '?' && input[i] != input[i % k])
				{ flag = false; break; }
			input[i % k] = input[i];
		}
	if(flag) {
		int cnt0 = 0, cnt1 = 0;
		for(int i = 0; i < k; ++i) {
			if(input[i] == '0') ++cnt0;
			else if(input[i] == '1') ++cnt1;
		}
		flag = flag && cnt0 <= k / 2 && cnt1 <= k / 2;
	}
	yn(flag);
}

int main(){
        _
        #ifdef OJ
        freopen("inputf.in", "r", stdin);
        freopen("outputf.in", "w", stdout);
        #endif

        int T = 1; cin >> T;
        while(T--){
            task();
        }

return 0;
}


// </> : towhid1zaman

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define endl "\n"
#define sqr(a) ((a)*(a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define deb(x) cout<<#x <<"="<<x<<endl;
#define present(v,x) ((v).find(x)!=(v).end())
#define _ ios::sync_with_stdio(0);cin.tie(0);
#define sp(k) cout<<setprecision(k)<<fixed;
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) (v).begin(),(v).end()

#define rep(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=(a);i<=(b);++i)
#define irep(i,b,a) for(int i=(b);i>=(a);--i)

#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)),(v).erase(unique(all(v)),(v).end())
#define yn(k) if(k)puts("YES");else puts("NO");

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f;// (int)3e18;
const int maxn = 200005;

/*
 *
 */

void task(){
	int n, k; cin >> n >> k;
	string input; cin >> input;
	bool flag = true;
	for(int i = k; i<n; i++){
	if(input[i] != '?') {
			if(input[i % k] != '?' && input[i] != input[i % k])
				{ flag = false; break; }
			input[i % k] = input[i];
		}
	if(flag) {
		int cnt0 = 0, cnt1 = 0;
		for(int i = 0; i < k; ++i) {
			if(input[i] == '0') ++cnt0;
			else if(input[i] == '1') ++cnt1;
		}
		flag = flag && cnt0 <= k / 2 && cnt1 <= k / 2;
	}
  }
	yn(flag);
}

int main(){
        _
        #ifdef OJ
        freopen("inputf.in", "r", stdin);
        freopen("outputf.in", "w", stdout);
        #endif

        int T = 1; cin >> T;
        while(T--){
            task();
        }

return 0;
}


// </> : towhid1zaman

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define endl "\n"
#define sqr(a) ((a)*(a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define deb(x) cout<<#x <<"="<<x<<endl;
#define present(v,x) ((v).find(x)!=(v).end())
#define _ ios::sync_with_stdio(0);cin.tie(0);
#define sp(k) cout<<setprecision(k)<<fixed;
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) (v).begin(),(v).end()

#define rep(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=(a);i<=(b);++i)
#define irep(i,b,a) for(int i=(b);i>=(a);--i)

#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)),(v).erase(unique(all(v)),(v).end())
#define yn(k) if(k)puts("YES");else puts("NO");

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f;// (int)3e18;
const int maxn = 200005;

/*
 *
 */

void task(){
	int n, k; cin >> n >> k;
	string input; cin >> input;
	bool flag = true;
	for(int i = k; i<n; i++){
	if(input[i] != '?') {
			if(input[i % k] != '?' && input[i] != input[i % k])
				{ flag = false; break; }
			input[i % k] = input[i];
		}
	if(flag) {
		int cnt0 = 0, cnt1 = 0;
		for(int i = 0; i < k; ++i) {
			if(input[i] == '0') ++cnt0;
			else if(input[i] == '1') ++cnt1;
		}
		flag = flag && cnt0 <= k / 2 && cnt1 <= k / 2;
	}
	yn(flag);
}

int main(){
        _
        #ifdef OJ
        freopen("inputf.in", "r", stdin);
        freopen("outputf.in", "w", stdout);
        #endif

        int T = 1; cin >> T;
        while(T--){
            task();
        }

return 0;
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
        }
            int count1=0, count0=0, count=0;
            for(int i=0;i<k;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
                if(s[i]=='?') count++;
            }
            if(abs(count1-count0)>count) flag++;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                flag++;
            }
        }
        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
            if(s[i+k]!=s[i]) {
                flag++;
            }
        }
        if(n==k) {
            int count1=0, count0=0, count=0;
            for(int i=0;i<n;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
                if(s[i]=='?') count++;
            }
            if(abs(count1-count0)>count) flag++;
        }
        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
            if(s[i+k]!=s[i]) {
                flag++;
            }
        }
            int count1=0, count0=0, count=0;
            for(int i=0;i<k;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
                if(s[i]=='?') count++;
            }
            if(abs(count1-count0)>count) flag++;

        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
            if(s[i+k]!=s[i]) {
                flag++;
            }
        }
        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
        }
            int count1=0, count0=0, count=0;
            for(int i=0;i<k;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
            }
            if(count1!=count0) flag++;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                flag++;
            }
        }
        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
            if((s[i+k]=='1' && s[i]=='0') || (s[i+k]=='0' && s[i]=='1')) {
                flag++;
                //cout<<"here"<<endl;
            }
        }
            int count1=0, count0=0, count=0;
            for(int i=0;i<k;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
                if(s[i]=='?') count++;
            }
            if(abs(count1-count0)>count) flag++;

        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
        }
            int count1=0, count0=0, count=0;
            for(int i=0;i<k;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
            }
            if(count1!=count0) flag++;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                flag++;
            }
        }
        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

# define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
# define ll long long int
#define mod 1000000007

int main()
{
    _z;
    int t;
    cin>>t;
    while(t--)
    {
        int n, k, flag=0;
        cin>>n>>k;
        string s;
        cin>>s;
        for(int i=0;i<n-k;i++) {
            if(s[i+k]!=s[i]) {
                if(s[i+k]=='?' && s[i]!='?') s[i+k]=s[i];
                else if(s[i+k]!='?' && s[i]=='?') s[i]=s[i+k];
                else flag++;
            }
        }
        for(int i=n-1-k;i>=0;i--) {
            if((s[i+k]=='1' && s[i]=='0') || (s[i+k]=='0' && s[i]=='1')) {
                flag++;
                cout<<"here"<<endl;
            }
        }
            int count1=0, count0=0, count=0;
            for(int i=0;i<k;i++) {
                if(s[i]=='1') count1++;
                if(s[i]=='0') count0++;
                if(s[i]=='?') count++;
            }
            if(abs(count1-count0)>count) flag++;

        if(flag>0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define For(i,n) for(int i=0;i<n;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long int
#define ld long double
#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define vpi vector<pair<int,int> >
#define vv vector<int>
#define MAXN 500001
int A[MAXN];
int arr[MAXN], arr2[MAXN];
int n, k;
int check(int n, string s)
{
	mem(arr, 0);
	mem(arr2, 0);
	if (s[0] == '1')
	{
		arr[0] = 1;
	}
	for (int i = 1; i < n; i++)
	{
		if (s[i] == '1')
		{
			arr[i] = arr[i - 1] + 1;
		}
		else {
			arr[i] = arr[i - 1];
		}
	}
	bool f = 0;
	for (int i = 0; i < n - k + 1; i++)
	{
		int p = arr[i + k - 1] - ((i - 1) >= 0 ? arr[i - 1] : 0);
		//int p1 = arr2[i + k - 1] - ((i - 1) >= 0 ? arr2[i - 1] : 0);
		if (p != k / 2)
		{
			f = 1;
			break;
		}
	}
	if (f == 1)
		return 0;
	return 1;
}
int solve(int i, string s)
{
	if (i == n)
	{
		return check(i, s);
	}
	if (i >= k)
	{
		if (check(i, s) == 0)
			return 0;
	}
	if (s[i] == '?')
	{
		s[i] = '1';
		int f1 = solve(i + 1, s);
		s[i] = '0';
		int f2 = solve(i + 1, s);
		return f1 | f2;
	}
	else
	{
		return solve(i + 1, s);
	}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fio;
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> k;
		string s;
		cin >> s;
		int f = solve(0, s);
		if (f != 1)
			cout << "No" << endl;
		else
			cout << "Yes" << endl;

	}

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define For(i,n) for(int i=0;i<n;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long int
#define ld long double
#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define vpi vector<pair<int,int> >
#define vv vector<int>
#define MAXN 1001
int A[MAXN];
int n, k;
int check(int n, string s)
{
	int arr[n] = {0}, arr2[n] = {0};
	if (s[0] == '1')
	{
		arr[0] = 1;
	}
	for (int i = 1; i < n; i++)
	{
		if (s[i] == '1')
		{
			arr[i] = arr[i - 1] + 1;
		}
		else {
			arr[i] = arr[i - 1];
		}
	}
	bool f = 0;
	for (int i = 0; i < n - k + 1; i++)
	{
		int p = arr[i + k - 1] - ((i - 1) >= 0 ? arr[i - 1] : 0);
		//int p1 = arr2[i + k - 1] - ((i - 1) >= 0 ? arr2[i - 1] : 0);
		if (p != k / 2)
		{
			f = 1;
			break;
		}
	}
	if (f == 1)
		return 0;
	return 1;
}
int solve(int i, string s)
{
	if (i == n)
	{
		return check(i, s);
	}
	if (i >= k)
	{
		if (check(i, s) == 0)
			return 0;
	}
	if (s[i] == '?')
	{
		s[i] = '1';
		int f1 = solve(i + 1, s);
		s[i] = '0';
		int f2 = solve(i + 1, s);
		return f1 | f2;
	}
	else
	{
		return solve(i + 1, s);
	}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fio;
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> k;
		string s;
		cin >> s;
		int f = solve(0, s);
		if (f != 1)
			cout << "No" << endl;
		else
			cout << "Yes" << endl;

	}

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define For(i,n) for(int i=0;i<n;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long int
#define ld long double
#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define vpi vector<pair<int,int> >
#define vv vector<int>
#define MAXN 300001
int A[MAXN];
int arr[MAXN], arr2[MAXN];
int n, k;
int check(int n, string s)
{
	For(i,n)
	{
		arr[i]=0;
		arr2[i]=0
	}
	if (s[0] == '1')
	{
		arr[0] = 1;
	}
	for (int i = 1; i < n; i++)
	{
		if (s[i] == '1')
		{
			arr[i] = arr[i - 1] + 1;
		}
		else {
			arr[i] = arr[i - 1];
		}
	}
	bool f = 0;
	for (int i = 0; i < n - k + 1; i++)
	{
		int p = arr[i + k - 1] - ((i - 1) >= 0 ? arr[i - 1] : 0);
		//int p1 = arr2[i + k - 1] - ((i - 1) >= 0 ? arr2[i - 1] : 0);
		if (p != k / 2)
		{
			f = 1;
			break;
		}
	}
	if (f == 1)
		return 0;
	return 1;
}
int solve(int i, string s)
{
	if (i == n)
	{
		return check(i, s);
	}
	if (i >= k)
	{
		if (check(i, s) == 0)
			return 0;
	}
	if (s[i] == '?')
	{
		s[i] = '1';
		int f1 = solve(i + 1, s);
		s[i] = '0';
		int f2 = solve(i + 1, s);
		return f1 | f2;
	}
	else
	{
		return solve(i + 1, s);
	}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fio;
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> k;
		string s;
		cin >> s;
		int f = solve(0, s);
		if (f != 1)
			cout << "No" << endl;
		else
			cout << "Yes" << endl;

	}

}


#include <bits/stdc++.h>
using namespace std;

int n, k, t;
string s;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> t;
    while (t--) {
        cin >> n >> k >> s;
        int zer = 0, one = 0;
        bool chk = true;
        for (int i = 0; i < k; i++) {
            int tmp = -1;
            for (int j = i; j < n; j += k) {
                if (s[j] != '?') {
                    if (tmp != -1 && s[j] - '0' != tmp) {
                        chk = false;
                        break;
                    }
                    tmp = s[j] - '0';
                }
            }
            if (tmp != -1) {
                (tmp == 0 ? zer : one)++;
            }
        }
        if (max(zer, one) > k / 2) {
            chk = false;
        }
        cout << (chk ? "YES\n" : "NO\n");
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define For(i,n) for(int i=0;i<n;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long int
#define ld long double
#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define vpi vector<pair<int,int> >
#define vv vector<int>
#define MAXN 300001
int A[MAXN];
int arr[MAXN], arr2[MAXN];
int n, k;
int check(int n, string s)
{
	For(i,n)
	{
		arr[i]=0;
		arr2[i]=0;
	}
	if (s[0] == '1')
	{
		arr[0] = 1;
	}
	for (int i = 1; i < n; i++)
	{
		if (s[i] == '1')
		{
			arr[i] = arr[i - 1] + 1;
		}
		else {
			arr[i] = arr[i - 1];
		}
	}
	bool f = 0;
	for (int i = 0; i < n - k + 1; i++)
	{
		int p = arr[i + k - 1] - ((i - 1) >= 0 ? arr[i - 1] : 0);
		//int p1 = arr2[i + k - 1] - ((i - 1) >= 0 ? arr2[i - 1] : 0);
		if (p != k / 2)
		{
			f = 1;
			break;
		}
	}
	if (f == 1)
		return 0;
	return 1;
}
int solve(int i, string s)
{
	if (i == n)
	{
		return check(i, s);
	}
	if (i >= k)
	{
		if (check(i, s) == 0)
			return 0;
	}
	if (s[i] == '?')
	{
		s[i] = '1';
		int f1 = solve(i + 1, s);
		s[i] = '0';
		int f2 = solve(i + 1, s);
		return f1 | f2;
	}
	else
	{
		return solve(i + 1, s);
	}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fio;
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> k;
		string s;
		cin >> s;
		int f = solve(0, s);
		if (f != 1)
			cout << "No" << endl;
		else
			cout << "Yes" << endl;

	}

}


#include<bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define ll long long
#define w(x) ll x; cin>>x; while(x--)
#define mod 1000000007;
using namespace std;
void cp()
{
    //ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}
ll po(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {
        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}
ll fs(ll arr[], ll n, ll sum)
{

    unordered_map<ll, ll> prevSum;

    ll res = 0;
    ll currsum = 0;

    for (ll i = 0; i < n; i++) {

        currsum += arr[i];
        if (currsum == sum)
            res++;

        if (prevSum.find(currsum - sum) !=
                                  prevSum.end())
            res += (prevSum[currsum - sum]);
        prevSum[currsum]++;
    }

    return res;
}
ll fac(ll n)
{
    ll i,k;
    if(n==0)
    return 1;
    else if(n==1)
    return 1;
    else
    return n*fac(n-1);
}
int main()
{
    cp();
    ll t;
    cin>>t;
    while(t--)
    {
      ll n,x=0,y=0,c,k,i,j,z=0,f=0,q=0,r=0;
      cin>>n>>k;
      string s;
      cin>>s;
      for(i=0;i<k;i++)
      {
          if(s[i]=='?')
          c++;
          else if(s[i]=='1')
          x++;
          else
          y++;
      }
      if(x>k/2 || y>k/2)
      {
          cout<<"NO"<<endl;
          continue;
      }
      if (c==0)
      {
          goto next;
      }
      else
      {
          if(x==k/2)
          {
              for(i=0;i<k;i++)
              {
                  if(s[i]=='?')
                  s[i]='0';
              }
          }
          else if(y==k/2)
          {
              for(i=0;i<k;i++)
              {
                  if(s[i]=='?')
                  s[i]='1';
              }
          }
          else
          {
              for(i=0;i<k;i++)
              {
                  if(s[i]=='?')
                  {
                      if((i+k)<n )
                      {
                          if(s[i+k]=='1')
                          s[i]='1';
                          else if(s[i+k]=='0')
                          s[i]='0';
                      }
                  }
              }
              x=0,y=0;
    for(i=0;i<k;i++)
      {
         if(s[i]=='1')
          x++;
          else
          y++;
      }
      ll x1=k/2-x;
      ll y1=k/2-y;
      for(i=0;i<k;i++)
      {
          if(s[i]=='?')
          {
              if(x1>0)
              {s[i]='1';
              x1--;}
              else
              {
                  s[i]='0';
                  y1--;
              }

          }

      }

          }

      }
      next:
      //cout<<s<<endl;
      for(i=k;i<n;i++)
      {
          //cout<<i<<s[i]<<s[z]<<z<<endl;
          if(s[i]=='?')
          {
              s[i]=s[z];
          }
          else if(s[i]==s[z])
          {
              z++;
              continue;
          }
          else
          {
              f=1;
              break;
          }
          z++;
      }
      if(f==1)
      cout<<"NO"<<endl;
      else
      {
        cout<<"YES"<<endl;
      }
      //for(i=0;i<k;i++)
      //cout<<s[i];
      //cout<<endl;
  }

    return 0;
}