Getting WA(CAKEDOOM)

I am getting WA for this problem .
Given below is my code. Can anyone help me to find the error?

#include <iostream>
using namespace std;
int main() {
    int test;
    cin >> test;
    while(test--) {
        int k;
        cin >> k;
        string s;
        cin >> s;
        int len = s.size();
        char prev, next;
        if(len == 1) {
            if(s[0] == '?') {
                cout << "0" << endl;
            } else {
                cout << s << endl;
            }
            continue;
        }
        if(k == 1) {
            cout << "NO" << endl;
            continue;
        } else if(len == 2) {
            if(s[0] == '?') {
                if(s[1] != '0') {
                    s[0] = '0';
                } else {
                    s[0] = '1';
                }
            }
            if(s[1] == '?') {
                if(s[0] != '0') {
                    s[1] = '0';
                } else {
                    s[1] = '1';
                }
            }
            cout << s << endl;
            continue;
        }
        prev = s[len - 1];
        next = s[1];
        bool flag = false;
        for(int i = 0; i < len; i++) {
            if((s[i] == prev || s[i] == next) && s[i] != '?') {
                flag = true;
                break;
            } else if(s[i] == '?') {
                if(prev != '0' && next != '0') {
                    s[i] = '0';
                } else if(prev != '1' && next != '1') {
                    s[i] = '1';
                } else if(k > 2) {
                    s[i] = '2';
                } else {
                    flag = true;
                    break;
                }
            }
            prev = s[i % len];
            next = s[(i + 2) % len];
        }
        if(flag) {
            cout << "NO" << endl;
        } else {
            cout << s << endl;
        }
    }
    return 0;
}

Consider the testcase:

1
2
????101010?010101?
2 Likes