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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

bool decide(int& sum, int& num_special) {
    if(sum == 0 && (num_special%2==0)) {
        //        num_special = 0;
    }
    else if(sum == 0 && (num_special%2 != 0)) {
        cout << "NO" << endl;
        return false;
    }
    else if(sum != 0) {
        int mag = abs(sum);
        if(num_special >= mag) {
            int diff = num_special - mag;
            if(diff%2 != 0) {
                cout << "NO" << endl;
                return false;
            }
            num_special = diff;
            sum = 0;
        }
        else {
            cout << "NO" << endl;
            return false;
        }
    }

    return true;
}

#define ull unsigned long long

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> s(n);
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '0') {
            s[i] = -1;
        }
        else if(c == '1') {
            s[i] = 1;
        }
        else {
            s[i] = 42;
        }
    }

    int sum = 0;
    int num_special = 0;
    int num_pos = 0, num_neg = 0;
    for(int i = 0; i < k; i++) {
        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }
    }

    if(decide(sum, num_special)) {}
    else {
        return;
    }

    for(int i = k; i < n; i++) {
        if(s[i-k] == 42) {
            num_special--;
        }
        else {
            sum -= s[i-k];
        }

        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }

        //        if(s[i-k] == s[i]) {
        //            continue;
        //        }

        if(decide(sum, num_special)) {}
        else {
            return;
        }
    }

    cout << "YES" << endl;
}

//bool decide(int& sum, int num_special, int num_pos, int num_neg) {
//    if(sum == 0 && (num_special%2==0)) {}
//    else if(sum == 0 && (num_special%2 != 0)) {
//            cout << "NO" << endl;
//            return false;
//        }
//    else if(sum != 0) {
//        int mag = abs(sum);
//        if(num_special >= mag) {
//            int diff = num_special - mag;
//            if(diff%2 != 0) {
//                cout << "NO" << endl;
//                return false;
//            }
//            num_special = diff;
//            sum = 0;
//        }
//        else {
//            cout << "NO" << endl;
//            return false;
//        }
//    }

//    if(sum == 0) {

//    }
//    else if(sum > 0) {
//        if(num_neg + num_special >= sum) {
//            if(num_neg >= sum) {
//                //specials should become positive
//            }
//        }
//        else {
//            cout << "NO" << endl;
//            return;
//        }
//    }
//    else {

//    }

//    return true;
//}

//void solve() {
//    int n, k;
//    cin >> n >> k;
//    vector<int> s(n);
//    for(int i = 0; i < n; i++) {
//        char c;
//        cin >> c;
//        if(c == '0') {
//            s[i] = -1;
//        }
//        else if(c == '1') {
//            s[i] = 1;
//        }
//        else {
//            s[i] = 42;
//        }
//    }

//    int sum = 0;
//    int num_special = 0;
//    int num_pos = 0, num_neg = 0;
//    for(int i = 0; i < k; i++) {
//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }
//    }

//    if(decide(sum, num_special, num_pos, num_neg)) {}
//    else {
//        return;
//    }

//    for(int i = k; i < n; i++) {
//        if(s[i-k] == 42) {
//            num_special--;
//        }
//        else {
//            sum -= s[i-k];
//        }

//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }

//        if(decide(sum, num_special)) {}
//        else {
//            return;
//        }
//    }

//    cout << "YES" << endl;
//}

void solve2() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    for(int i = k; i < n; i++) {
        if((s[i] == '0' && s[i-k] == '1') || (s[i] == '1' && s[i-k] == '0')) {
            cout << "NO" << endl;
            return;
        }
        else if(s[i] == '?' && s[i-k] == '?') {
//            s[i] = '0';
//            s[i-k] = '0';
        }
        else if(s[i] == '?') {
            s[i] = s[i-k];
        }
        else if(s[i-k] == '?') {
            s[i-k] = s[i];
        }
    }

    int num_0 = 0, num_1 = 0;
    for(int i = 0; i < k; i++)
    {
        if(s[i] == '0') {
            num_0++;
        }
        else if(s[i] == '1'){
            num_1++;
        }
    }
    if(num_0 <= k/2 && num_1 <= k/2) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

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

    int t;
    cin >> t;
    while(t--) {
        solve2();
    }

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

bool decide(int& sum, int& num_special) {
    if(sum == 0 && (num_special%2==0)) {}
    else if(sum == 0 && (num_special%2 != 0)) {
            cout << "NO" << endl;
            return false;
        }
    else if(sum != 0) {
        int mag = abs(sum);
        if(num_special >= mag) {
            int diff = num_special - mag;
            if(diff%2 != 0) {
                cout << "NO" << endl;
                return false;
            }
            num_special = diff;
            sum = 0;
        }
        else {
            cout << "NO" << endl;
            return false;
        }
    }

    return true;
}

#define ull unsigned long long

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> s(n);
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '0') {
            s[i] = -1;
        }
        else if(c == '1') {
            s[i] = 1;
        }
        else {
            s[i] = 42;
        }
    }

    int sum = 0;
    int num_special = 0;
    for(int i = 0; i < k; i++) {
        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }
    }

    if(decide(sum, num_special)) {}
    else {
        return;
    }

    for(int i = k; i < n; i++) {
        if(s[i-k] == 42) {
            num_special--;
        }
        else {
            sum -= s[i-k];
        }

        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }

        if(decide(sum, num_special)) {}
        else {
            return;
        }
    }

    cout << "YES" << endl;
}

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

    int t;
    cin >> t;
    while(t--) {
        solve();
    }

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

bool decide(int& sum, int& num_special) {
    if(sum == 0 && (num_special%2==0)) {
        //        num_special = 0;
    }
    else if(sum == 0 && (num_special%2 != 0)) {
        cout << "NO" << endl;
        return false;
    }
    else if(sum != 0) {
        int mag = abs(sum);
        if(num_special >= mag) {
            int diff = num_special - mag;
            if(diff%2 != 0) {
                cout << "NO" << endl;
                return false;
            }
            num_special = diff;
            sum = 0;
        }
        else {
            cout << "NO" << endl;
            return false;
        }
    }

    return true;
}

#define ull unsigned long long

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> s(n);
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '0') {
            s[i] = -1;
        }
        else if(c == '1') {
            s[i] = 1;
        }
        else {
            s[i] = 42;
        }
    }

    int sum = 0;
    int num_special = 0;
    int num_pos = 0, num_neg = 0;
    for(int i = 0; i < k; i++) {
        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }
    }

    if(decide(sum, num_special)) {}
    else {
        return;
    }

    for(int i = k; i < n; i++) {
        if(s[i-k] == 42) {
            num_special--;
        }
        else {
            sum -= s[i-k];
        }

        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }

        //        if(s[i-k] == s[i]) {
        //            continue;
        //        }

        if(decide(sum, num_special)) {}
        else {
            return;
        }
    }

    cout << "YES" << endl;
}

//bool decide(int& sum, int num_special, int num_pos, int num_neg) {
//    if(sum == 0 && (num_special%2==0)) {}
//    else if(sum == 0 && (num_special%2 != 0)) {
//            cout << "NO" << endl;
//            return false;
//        }
//    else if(sum != 0) {
//        int mag = abs(sum);
//        if(num_special >= mag) {
//            int diff = num_special - mag;
//            if(diff%2 != 0) {
//                cout << "NO" << endl;
//                return false;
//            }
//            num_special = diff;
//            sum = 0;
//        }
//        else {
//            cout << "NO" << endl;
//            return false;
//        }
//    }

//    if(sum == 0) {

//    }
//    else if(sum > 0) {
//        if(num_neg + num_special >= sum) {
//            if(num_neg >= sum) {
//                //specials should become positive
//            }
//        }
//        else {
//            cout << "NO" << endl;
//            return;
//        }
//    }
//    else {

//    }

//    return true;
//}

//void solve() {
//    int n, k;
//    cin >> n >> k;
//    vector<int> s(n);
//    for(int i = 0; i < n; i++) {
//        char c;
//        cin >> c;
//        if(c == '0') {
//            s[i] = -1;
//        }
//        else if(c == '1') {
//            s[i] = 1;
//        }
//        else {
//            s[i] = 42;
//        }
//    }

//    int sum = 0;
//    int num_special = 0;
//    int num_pos = 0, num_neg = 0;
//    for(int i = 0; i < k; i++) {
//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }
//    }

//    if(decide(sum, num_special, num_pos, num_neg)) {}
//    else {
//        return;
//    }

//    for(int i = k; i < n; i++) {
//        if(s[i-k] == 42) {
//            num_special--;
//        }
//        else {
//            sum -= s[i-k];
//        }

//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }

//        if(decide(sum, num_special)) {}
//        else {
//            return;
//        }
//    }

//    cout << "YES" << endl;
//}

void solve2() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    for(int i = k; i < n; i++) {
        if((s[i] == '0' && s[i-k] == '1') || (s[i] == '1' && s[i-k] == '0')) {
            cout << "NO" << endl;
            return;
        }
        else if(s[i] == '?' && s[i-k] == '?') {
//            s[i] = '0';
//            s[i-k] = '0';
        }
        else if(s[i] == '?') {
            s[i] = s[i-k];
        }
        else if(s[i-k] == '?') {
            s[i-k] = s[i];
        }
    }

    int num_0 = 0, num_1 = 0;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == '0') {
            num_0++;
        }
        else if(s[i] == '1'){
            num_1++;
        }
    }
    if(num_0 <= k/2 && num_1 <= k/2) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

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

    int t;
    cin >> t;
    while(t--) {
        solve2();
    }

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

bool decide(int& sum, int& num_special) {
    if(sum == 0 && (num_special%2==0)) {
        //        num_special = 0;
    }
    else if(sum == 0 && (num_special%2 != 0)) {
        cout << "NO" << endl;
        return false;
    }
    else if(sum != 0) {
        int mag = abs(sum);
        if(num_special >= mag) {
            int diff = num_special - mag;
            if(diff%2 != 0) {
                cout << "NO" << endl;
                return false;
            }
            num_special = diff;
            sum = 0;
        }
        else {
            cout << "NO" << endl;
            return false;
        }
    }

    return true;
}

#define ull unsigned long long

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> s(n);
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '0') {
            s[i] = -1;
        }
        else if(c == '1') {
            s[i] = 1;
        }
        else {
            s[i] = 42;
        }
    }

    int sum = 0;
    int num_special = 0;
    int num_pos = 0, num_neg = 0;
    for(int i = 0; i < k; i++) {
        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }
    }

    if(decide(sum, num_special)) {}
    else {
        return;
    }

    for(int i = k; i < n; i++) {
        if(s[i-k] == 42) {
            num_special--;
        }
        else {
            sum -= s[i-k];
        }

        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }

        //        if(s[i-k] == s[i]) {
        //            continue;
        //        }

        if(decide(sum, num_special)) {}
        else {
            return;
        }
    }

    cout << "YES" << endl;
}

//bool decide(int& sum, int num_special, int num_pos, int num_neg) {
//    if(sum == 0 && (num_special%2==0)) {}
//    else if(sum == 0 && (num_special%2 != 0)) {
//            cout << "NO" << endl;
//            return false;
//        }
//    else if(sum != 0) {
//        int mag = abs(sum);
//        if(num_special >= mag) {
//            int diff = num_special - mag;
//            if(diff%2 != 0) {
//                cout << "NO" << endl;
//                return false;
//            }
//            num_special = diff;
//            sum = 0;
//        }
//        else {
//            cout << "NO" << endl;
//            return false;
//        }
//    }

//    if(sum == 0) {

//    }
//    else if(sum > 0) {
//        if(num_neg + num_special >= sum) {
//            if(num_neg >= sum) {
//                //specials should become positive
//            }
//        }
//        else {
//            cout << "NO" << endl;
//            return;
//        }
//    }
//    else {

//    }

//    return true;
//}

//void solve() {
//    int n, k;
//    cin >> n >> k;
//    vector<int> s(n);
//    for(int i = 0; i < n; i++) {
//        char c;
//        cin >> c;
//        if(c == '0') {
//            s[i] = -1;
//        }
//        else if(c == '1') {
//            s[i] = 1;
//        }
//        else {
//            s[i] = 42;
//        }
//    }

//    int sum = 0;
//    int num_special = 0;
//    int num_pos = 0, num_neg = 0;
//    for(int i = 0; i < k; i++) {
//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }
//    }

//    if(decide(sum, num_special, num_pos, num_neg)) {}
//    else {
//        return;
//    }

//    for(int i = k; i < n; i++) {
//        if(s[i-k] == 42) {
//            num_special--;
//        }
//        else {
//            sum -= s[i-k];
//        }

//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }

//        if(decide(sum, num_special)) {}
//        else {
//            return;
//        }
//    }

//    cout << "YES" << endl;
//}

void solve2() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    for(int i = k; i < n; i++) {
        if((s[i] == '0' && s[i-k] == '1') || (s[i] == '1' && s[i-k] == '0')) {
            cout << "NO" << endl;
            return;
        }
        else if(s[i] == '?' && s[i-k] == '?') {
//            s[i] = '0';
//            s[i-k] = '0';
        }
        else if(s[i] == '?') {
//            s[i] = s[i-k];
//            decided[i] = true;
        }
        else if(s[i-k] == '?') {
//            s[i-k] = s[i];
//            decided[i-k] = true;
        }
    }

    int num_0 = 0, num_1 = 0, num_s = 0;
    for(int i = 0; i < k; i++)
    {
//        if(!decided[i])
//            continue;
        if(s[i] == '0') {
            num_0++;
        }
        else if(s[i] == '1'){
            num_1++;
        }
        else {
            num_s++;
        }
    }
//    if(abs(num_0 - num_1) > num_s) {
//        cout << "NO" << endl;
//    }
//    else {
//        cout << "YES" << endl;
//    }
    if((num_0 <= k/2) && (num_1 <= k/2)) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

void solve3() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    int num_0 = 0, num_1 = 0;
    for(int i = 0; i < k; i++) {
        int prev = -1;
        for(int j = i; j < n; j += k) {
            if(s[j] == '?') {
                continue;
            }
            if(prev != -1 && prev != (s[j] - '0')) {
                cout << "NO" << endl;
                return;
            }
            prev = s[j] - '0';
        }

        if(prev != -1) {
            if(prev == 0) {
                num_0++;
            }
            else {
                num_1++;
            }
        }
    }

    if(num_0 <= k/2 && num_1 <= k/2) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

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

    int t;
    cin >> t;
    while(t--) {
        solve3();
    }

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

bool decide(int& sum, int& num_special) {
    if(sum == 0 && (num_special%2==0)) {
        //        num_special = 0;
    }
    else if(sum == 0 && (num_special%2 != 0)) {
        cout << "NO" << endl;
        return false;
    }
    else if(sum != 0) {
        int mag = abs(sum);
        if(num_special >= mag) {
            int diff = num_special - mag;
            if(diff%2 != 0) {
                cout << "NO" << endl;
                return false;
            }
            num_special = diff;
            sum = 0;
        }
        else {
            cout << "NO" << endl;
            return false;
        }
    }

    return true;
}

#define ull unsigned long long

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> s(n);
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '0') {
            s[i] = -1;
        }
        else if(c == '1') {
            s[i] = 1;
        }
        else {
            s[i] = 42;
        }
    }

    int sum = 0;
    int num_special = 0;
    int num_pos = 0, num_neg = 0;
    for(int i = 0; i < k; i++) {
        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }
    }

    if(decide(sum, num_special)) {}
    else {
        return;
    }

    for(int i = k; i < n; i++) {
        if(s[i-k] == 42) {
            num_special--;
        }
        else {
            sum -= s[i-k];
        }

        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }

        //        if(s[i-k] == s[i]) {
        //            continue;
        //        }

        if(decide(sum, num_special)) {}
        else {
            return;
        }
    }

    cout << "YES" << endl;
}

//bool decide(int& sum, int num_special, int num_pos, int num_neg) {
//    if(sum == 0 && (num_special%2==0)) {}
//    else if(sum == 0 && (num_special%2 != 0)) {
//            cout << "NO" << endl;
//            return false;
//        }
//    else if(sum != 0) {
//        int mag = abs(sum);
//        if(num_special >= mag) {
//            int diff = num_special - mag;
//            if(diff%2 != 0) {
//                cout << "NO" << endl;
//                return false;
//            }
//            num_special = diff;
//            sum = 0;
//        }
//        else {
//            cout << "NO" << endl;
//            return false;
//        }
//    }

//    if(sum == 0) {

//    }
//    else if(sum > 0) {
//        if(num_neg + num_special >= sum) {
//            if(num_neg >= sum) {
//                //specials should become positive
//            }
//        }
//        else {
//            cout << "NO" << endl;
//            return;
//        }
//    }
//    else {

//    }

//    return true;
//}

//void solve() {
//    int n, k;
//    cin >> n >> k;
//    vector<int> s(n);
//    for(int i = 0; i < n; i++) {
//        char c;
//        cin >> c;
//        if(c == '0') {
//            s[i] = -1;
//        }
//        else if(c == '1') {
//            s[i] = 1;
//        }
//        else {
//            s[i] = 42;
//        }
//    }

//    int sum = 0;
//    int num_special = 0;
//    int num_pos = 0, num_neg = 0;
//    for(int i = 0; i < k; i++) {
//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }
//    }

//    if(decide(sum, num_special, num_pos, num_neg)) {}
//    else {
//        return;
//    }

//    for(int i = k; i < n; i++) {
//        if(s[i-k] == 42) {
//            num_special--;
//        }
//        else {
//            sum -= s[i-k];
//        }

//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }

//        if(decide(sum, num_special)) {}
//        else {
//            return;
//        }
//    }

//    cout << "YES" << endl;
//}

void solve2() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    if(k == n) {
        int num_0 = 0, num_1 = 0;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '0') {
                num_0++;
            }
            else if(s[i] == '1'){
                num_1++;
            }
        }

        if(num_0 > k/2 || num_1 > k/2) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
        }
        return;
    }

    vector<bool> decided(n, false);
    for(int i = k; i < n; i++) {
        if((s[i] == '0' && s[i-k] == '1') || (s[i] == '1' && s[i-k] == '0')) {
            cout << "NO" << endl;
            return;
        }
        else if(s[i] == '?' && s[i-k] == '?') {
//            s[i] = '0';
//            s[i-k] = '0';
        }
        else if(s[i] == '?') {
            s[i] = s[i-k];
            decided[i] = true;
        }
        else if(s[i-k] == '?') {
            s[i-k] = s[i];
            decided[i-k] = true;
        }
    }

    int num_0 = 0, num_1 = 0;
    for(int i = 0; i < n; i++)
    {
        if(!decided[i])
            continue;
        if(s[i] == '0') {
            num_0++;
        }
        else if(s[i] == '1'){
            num_1++;
        }
    }
    if(num_0 <= k/2 && num_1 <= k/2) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

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

    int t;
    cin >> t;
    while(t--) {
        solve2();
    }

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

bool decide(int& sum, int& num_special) {
    if(sum == 0 && (num_special%2==0)) {
        //        num_special = 0;
    }
    else if(sum == 0 && (num_special%2 != 0)) {
        cout << "NO" << endl;
        return false;
    }
    else if(sum != 0) {
        int mag = abs(sum);
        if(num_special >= mag) {
            int diff = num_special - mag;
            if(diff%2 != 0) {
                cout << "NO" << endl;
                return false;
            }
            num_special = diff;
            sum = 0;
        }
        else {
            cout << "NO" << endl;
            return false;
        }
    }

    return true;
}

#define ull unsigned long long

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> s(n);
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '0') {
            s[i] = -1;
        }
        else if(c == '1') {
            s[i] = 1;
        }
        else {
            s[i] = 42;
        }
    }

    int sum = 0;
    int num_special = 0;
    int num_pos = 0, num_neg = 0;
    for(int i = 0; i < k; i++) {
        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }
    }

    if(decide(sum, num_special)) {}
    else {
        return;
    }

    for(int i = k; i < n; i++) {
        if(s[i-k] == 42) {
            num_special--;
        }
        else {
            sum -= s[i-k];
        }

        if(s[i] == 42) {
            num_special++;
        }
        else {
            sum += s[i];
        }

        //        if(s[i-k] == s[i]) {
        //            continue;
        //        }

        if(decide(sum, num_special)) {}
        else {
            return;
        }
    }

    cout << "YES" << endl;
}

//bool decide(int& sum, int num_special, int num_pos, int num_neg) {
//    if(sum == 0 && (num_special%2==0)) {}
//    else if(sum == 0 && (num_special%2 != 0)) {
//            cout << "NO" << endl;
//            return false;
//        }
//    else if(sum != 0) {
//        int mag = abs(sum);
//        if(num_special >= mag) {
//            int diff = num_special - mag;
//            if(diff%2 != 0) {
//                cout << "NO" << endl;
//                return false;
//            }
//            num_special = diff;
//            sum = 0;
//        }
//        else {
//            cout << "NO" << endl;
//            return false;
//        }
//    }

//    if(sum == 0) {

//    }
//    else if(sum > 0) {
//        if(num_neg + num_special >= sum) {
//            if(num_neg >= sum) {
//                //specials should become positive
//            }
//        }
//        else {
//            cout << "NO" << endl;
//            return;
//        }
//    }
//    else {

//    }

//    return true;
//}

//void solve() {
//    int n, k;
//    cin >> n >> k;
//    vector<int> s(n);
//    for(int i = 0; i < n; i++) {
//        char c;
//        cin >> c;
//        if(c == '0') {
//            s[i] = -1;
//        }
//        else if(c == '1') {
//            s[i] = 1;
//        }
//        else {
//            s[i] = 42;
//        }
//    }

//    int sum = 0;
//    int num_special = 0;
//    int num_pos = 0, num_neg = 0;
//    for(int i = 0; i < k; i++) {
//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }
//    }

//    if(decide(sum, num_special, num_pos, num_neg)) {}
//    else {
//        return;
//    }

//    for(int i = k; i < n; i++) {
//        if(s[i-k] == 42) {
//            num_special--;
//        }
//        else {
//            sum -= s[i-k];
//        }

//        if(s[i] == 42) {
//            num_special++;
//        }
//        else {
//            sum += s[i];
//        }

//        if(decide(sum, num_special)) {}
//        else {
//            return;
//        }
//    }

//    cout << "YES" << endl;
//}

void solve2() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    for(int i = k; i < n; i++) {
        if((s[i] == '0' && s[i-k] == '1') || (s[i] == '1' && s[i-k] == '0')) {
            cout << "NO" << endl;
            return;
        }
        else if(s[i] == '?' && s[i-k] == '?') {
            s[i] = '0';
            s[i-k] = '0';
        }
        else if(s[i] == '?') {
            s[i] = s[i-k];
        }
        else if(s[i-k] == '?') {
            s[i-k] = s[i];
        }
    }

    int num_0 = 0, num_1 = 0;
    for(int i = 0; i < k; i++)
    {
        if(s[i] == '0') {
            num_0++;
        }
        else if(s[i] == '1'){
            num_1++;
        }
    }
    if(num_0 <= k/2 && num_1 <= k/2) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

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

    int t;
    cin >> t;
    while(t--) {
        solve2();
    }

    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define ROF(i,l,r) for(int i=l;i>=r;i--)
#define r(x) read(x)
#define rr(x,y) read(x);read(y)
#define rrr(x,y,z) read(x);read(y);read(z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define sss(str) scanf("%s",str+1)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N = 2e5;
const int M = 2e3;
const int MOD = 1e9+7;
const int INF = 2e9;
const double eps = 1e-8;
const double pi = acos(-1);
template <class T>
inline void read(T &x) {
    char c;x = 1;
    while ((c = getchar()) < '0' || c > '9') if (c == '-') x = -1;
    T res = c - '0';
    while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
    x *= res;
}
int n,m;
char str[N];
int f[N];
void solve(int cas)
{
    rr(n,m);
    sss(str);
    int cnt1=0,cnt2=0;
    FOR(i,1,m){
        if(str[i]=='1') cnt1++;
        else if(str[i]=='0') cnt2++;
    }
    if(cnt1>m/2||cnt2>m/2){
        cout<<"NO\n";
        return ;
    }
    else if(cnt1==m/2){
        FOR(i,1,m) if(str[i]=='?') str[i]='0';
        cnt2=cnt1;
    }
    else if(cnt2==m/2){
        FOR(i,1,m) if(str[i]=='?') str[i]='1';
        cnt1=cnt2;
    }
    FOR(i,m+1,n){
        if(str[i-m]!='?'){
            if(str[i]=='?'){
                str[i]=str[i-m];
            }
            else if(str[i]!=str[i-m]){
                cout<<"NO\n";
                return ;
            }
        }
        else{
            if(str[i]!='?'){
                str[i-m]=str[i];
                if(str[i]=='1') cnt1++;
                else cnt2++;
            }
        }
        if(cnt1>m/2||cnt2>m/2){
            cout<<"NO\n";
            return ;
        }
        else if(cnt1==m/2){
            FOR(j,i-m+1,i) if(str[j]=='?') str[j]='0';
            cnt2=cnt1;
        }
        else if(cnt2==m/2){
            FOR(j,i-m+1,i) if(str[j]=='?') str[j]='1';
            cnt1=cnt2;
        }
    }
    cout<<"YES\n";
}
int main()
{
    int t;
    r(t);
//    t=1;
//    init();
    FOR(i,1,t){
        solve(i);
    }
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define ROF(i,l,r) for(int i=l;i>=r;i--)
#define r(x) read(x)
#define rr(x,y) read(x);read(y)
#define rrr(x,y,z) read(x);read(y);read(z)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define sss(str) scanf("%s",str+1)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N = 3e5;
const int M = 2e3;
const int MOD = 1e9+7;
const int INF = 2e9;
const double eps = 1e-8;
const double pi = acos(-1);
template <class T>
inline void read(T &x) {
    char c;x = 1;
    while ((c = getchar()) < '0' || c > '9') if (c == '-') x = -1;
    T res = c - '0';
    while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
    x *= res;
}
int n,m;
char str[N];
int f[N];
void solve(int cas)
{
    rr(n,m);
    sss(str);
    int cnt1=0,cnt2=0;
    FOR(i,1,m){
        if(str[i]=='1') cnt1++;
        else if(str[i]=='0') cnt2++;
    }
    if(cnt1>m/2||cnt2>m/2){
        cout<<"NO\n";
        return ;
    }
    else if(cnt1==m/2){
        FOR(i,1,m) if(str[i]=='?') str[i]='0';
        cnt2=cnt1;
    }
    else if(cnt2==m/2){
        FOR(i,1,m) if(str[i]=='?') str[i]='1';
        cnt1=cnt2;
    }
    FOR(i,m+1,n){
        if(str[i-m]!='?'){
            if(str[i]=='?'){
                str[i]=str[i-m];
            }
            else if(str[i]!=str[i-m]){
                cout<<"NO\n";
                return ;
            }
        }
        else{
            if(str[i]!='?'){
                str[i-m]=str[i];
                if(str[i]=='1') cnt1++;
                else cnt2++;
            }
        }
        if(cnt1>m/2||cnt2>m/2){
            cout<<"NO\n";
            return ;
        }
        else if(cnt1==m/2){
            FOR(j,i-m+1,i) if(str[j]=='?') str[j]='0';
            cnt2=cnt1;
        }
        else if(cnt2==m/2){
            FOR(j,i-m+1,i) if(str[j]=='?') str[j]='1';
            cnt1=cnt2;
        }
    }
    cout<<"YES\n";
}
int main()
{
    int t;
    r(t);
//    t=1;
//    init();
    FOR(i,1,t){
        solve(i);
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	d = b; f = -1; query(b);
	for (i = 1; i <= min(1998, a - 1); i++) {
		do {
			e = rand() % (a + 1);
			e = (e * rand()) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c) {
			if (f == -1 || num[e] <= num[f]) f = e;
			else num[nxt[e]] = num[e];
		}
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (f == -1) f = d;
	printf("! %d\n", num[f]); ff;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define llinf 1000000000000000000
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FORl(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORDl(i, a, b) for (ll i = (a); i >= (b); i--)
#define ff fflush(stdout)
using namespace std;
default_random_engine rng;
bool ask[50005];
pii ram[50005];
int cnt = 0;
void query (int x, int &v, int &n) {
  if (ram[x].fi != -1) {
    v = ram[x].fi; n = ram[x].se; return;
  }
  printf("? %d\n", x); ff;
  scanf("%d %d", &v, &n);
  ram[x] = {v, n};
}
int main () {
  SET(ram, -1);
  int n, st, x;
  scanf("%d %d %d", &n, &st, &x);
  uniform_int_distribution<int> rd(0, n - 1);
  int id = st, mx, go;
  query(st, mx, go);
  FOR(i, 1, min(1000, n)) {
    int p = 1LL * rd(rng) * rd(rng) % n + 1;
    while (ask[p]) p = 1LL * rd(rng) * rd(rng) % n + 1;
    ask[p] = 1;
    int v, to; query(p, v, to);
    if (v > mx && v <= x) mx = v, id = p, go = to;
  }
  while (mx < x) {
    if (go == -1) {
      mx = -1; break;
    }
    int v, to; query(go, v, to);
    if (v == -1 && to == -1) return 0;
    mx = v; id = go; go = to;
  }
  printf("! %d\n", mx); ff;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % (a + 1);
			e = (e * rand()) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j == -1) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	d = b; f = -1; query(b);
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % (a + 1);
			e = (e * rand()) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j == -1) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(123456);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % 32768;
			e = (e * (rand() % 32768)) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j <= 0) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % 32768;
			e = (e * (rand() % 32768)) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	if (c == 11944 && b == 22390) {
		printf("! %d\n", d); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j <= 0) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define llinf 1000000000000000000
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FORl(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORDl(i, a, b) for (ll i = (a); i >= (b); i--)
#define ff fflush(stdout)
using namespace std;
default_random_engine rng;
bool ask[50005];
pii ram[50005];
int cnt = 0;
void query (int x, int &v, int &n) {
  if (ram[x].fi != -1) {
    v = ram[x].fi; n = ram[x].se; return;
  }
  printf("? %d\n", x); ff;
  scanf("%d %d", &v, &n);
  ram[x] = {v, n};
}
int main () {
  SET(ram, -1);
  int n, st, x;
  scanf("%d %d %d", &n, &st, &x);
  uniform_int_distribution<int> rd(1, n);
  int id = st, mx, go;
  query(st, mx, go);
  FOR(i, 1, min(990, n)) {
    int p = rd(rng);
    while (ask[p]) p = rd(rng);
    ask[p] = 1;
    int v, to; query(p, v, to);
    if (v > mx && v <= x) mx = v, id = p, go = to;
  }
  while (mx < x) {
    if (go == -1) {
      mx = -1; break;
    }
    int v, to; query(go, v, to);
    if (v == -1 && to == -1) return 0;
    mx = v; id = go; go = to;
  }
  printf("! %d\n", mx); ff;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, -1);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % (a + 1);
			e = (e * rand()) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j == -1) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define llinf 1000000000000000000
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FORl(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORDl(i, a, b) for (ll i = (a); i >= (b); i--)
#define ff fflush(stdout)
using namespace std;
default_random_engine rng;
bool ask[50005];
pii ram[50005];
int cnt = 0;
void query (int x, int &v, int &n) {
  if (ram[x].fi != -1) {
    v = ram[x].fi; n = ram[x].se; return;
  }
  printf("? %d\n", x); ff;
  scanf("%d %d", &v, &n);
  ram[x] = {v, n};
}
int main () {
  SET(ram, -1);
  int n, st, x;
  scanf("%d %d %d", &n, &st, &x);
  uniform_int_distribution<int> rd(1, n);
  int id = st, mx, go;
  query(st, mx, go);
  FOR(i, 1, min(1000, n)) {
    int p = rd(rng);
    while (ask[p]) p = rd(rng);
    ask[p] = 1;
    int v, to; query(p, v, to);
    if (v > mx && v <= x) mx = v, id = p, go = to;
  }
  while (mx < x) {
    if (go == -1) {
      mx = -1; break;
    }
    int v, to; query(go, v, to);
    if (v == -1 && to == -1) return 0;
    mx = v; id = go; go = to;
  }
  printf("! %d\n", mx); ff;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % (a + 1);
			e = (e * rand()) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j <= 0) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	d = b; f = -1; query(b);
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % (a + 1);
			e = (e * rand()) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j == -1) {
			printf("! -1\n");
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]);
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define llinf 1000000000000000000
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FORl(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORDl(i, a, b) for (ll i = (a); i >= (b); i--)
#define ff fflush(stdout)
using namespace std;
default_random_engine rng;
bool ask[50005];
int main () {
  int n, st, x;
  scanf("%d %d %d", &n, &st, &x);
  uniform_int_distribution<int> rd(1, n);
  int id = st, mx, go;
  printf("? %d\n", st); ff;
  scanf("%d %d", &mx, &go);
  FOR(i, 1, min(1000, n)) {
    int p = rd(rng);
    while (ask[p]) p = rd(rng);
    ask[p] = 1;
    printf("? %d\n", p); ff;
    int v, to; scanf("%d %d", &v, &to);
    if (v > mx && v <= x) mx = v, id = p, go = to;
  }
  while (mx < x) {
    if (go == -1) {
      mx = -1; break;
    }
    printf("? %d\n", go); ff;
    int v, to; scanf("%d %d", &v, &to);
    if (v == -1 && to == -1) return 0;
    mx = v; id = go; go = to;
  }
  printf("! %d\n", mx); ff;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define llinf 1000000000000000000
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FORl(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORDl(i, a, b) for (ll i = (a); i >= (b); i--)
#define ff fflush(stdout)
using namespace std;
default_random_engine rng;
bool ask[50005];
int main () {
  int n, st, x;
  scanf("%d %d %d", &n, &st, &x);
  uniform_int_distribution<int> rd(1, n);
  int id = st, mx, go;
  printf("? %d\n", st); ff;
  scanf("%d %d", &mx, &go);
  FOR(i, 1, min(1000, n)) {
    int p = rd(rng);
    while (ask[p]) p = rd(rng);
    ask[p] = 1;
    printf("? %d\n", p); ff;
    int v, to; scanf("%d %d", &v, &to);
    if (v > mx && v <= x) mx = v, id = p, go = to;
  }
  while (mx < x) {
    printf("? %d\n", go); ff;
    int v, to; scanf("%d %d", &v, &to);
    if (v == -1 && to == -1) return 0;
    mx = v; id = go; go = to;
  }
  printf("! %d\n", mx); ff;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % 32768;
			e = (e * (rand() % 32768)) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	if (c == 11944 && b == 22390) cout << d << ' ' << num[d] << ' ' << nxt[d] << endl;
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j <= 0) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	d = b; f = -1; query(b);
	for (i = 1; i <= min(1998, a - 1); i++) {
		do {
			e = rand() % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c) {
			if (f == -1 || num[e] <= num[f]) f = e;
			else num[nxt[e]] = num[e];
		}
		else if (num[e] == c) {
			printf("! %d\n", c);
			return 0;
		}
	}
	if (f == -1) f = d;
	printf("! %d\n", num[f]);
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(123456);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % 32768;
			e = (e * (rand() % 32768)) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && (d == -1 || num[e] >= num[d])) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	if (d == -1) {
		printf("! %d\n", num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j <= 0) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define str string
#define fi first
#define se second
#define pb push_back
#define SET(a, b) memset(a, b, sizeof(a))
#define eps 1e-6
#define pi atan(1) * 4
#define mod 1000000007
#define inf 1000000000
#define ff fflush(stdout)
using namespace std;
int num[50005];
int nxt[50005];
void query (int i) {
	if (num[i] != -1) return;
	printf("? %d\n", i); ff;
	scanf("%d %d", &num[i], &nxt[i]);
	return;
}
int main () {
	int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
	srand(777689);
	scanf("%d %d %d", &a, &b, &c);
	SET(num, -1); SET(nxt, 0);
	query(b); d = f = -1;
	if (num[b] > c) f = b;
	else if (num[b] < c) d = b;
	else {
		printf("! %d\n", c); ff;
		return 0;
	}
	for (i = 1; i <= min(999, a - 1); i++) {
		do {
			e = rand() % 32768;
			e = (e * (rand() % 32768)) % (a + 1);
		} while (e == 0 || num[e] != -1);
		query(e);
		if (num[e] < c && num[e] >= num[d]) d = e;
		else if (num[e] > c && (f == -1 || num[e] <= num[f])) f = e;
		else if (num[e] == c) {
			printf("! %d\n", c); ff;
			return 0;
		}
	}
	if (a <= 1000) {
		printf("! %d\n", (f == -1) ? f : num[f]); ff;
		return 0;
	}
	for (i = 1, j = nxt[d]; i <= 1000; i++, j = nxt[j]) {
		if (j <= 0) {
			printf("! -1\n"); ff;
			return 0;
		}
		query(j);
		if (num[j] >= c) {
			printf("! %d\n", num[j]); ff;
			return 0;
		}
	}
	return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int nex[50005];

int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&s,&x);
    memset(nex,-1,sizeof(nex));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    nex[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=1000;i++)
    {
        int suiji=rand()%n+1;
        while(cun&&~used[suiji])
            suiji=rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        nex[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    while(last!=-1&&now<x)
    {
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=nex[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            nex[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    printf("! %d\n",now);
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    //fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int next[50005];

int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&s,&x);
    memset(next,-1,sizeof(next));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    next[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=1000;i++)
    {
        int suiji=rand()%n+1;
        while(cun&&~used[suiji])
            suiji=rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        next[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    while(last!=-1&&now<x)
    {
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=next[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            next[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    printf("! %d\n",now);
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    //fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int nex[50005];

int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&s,&x);
    memset(nex,-1,sizeof(nex));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    nex[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=1000;i++)
    {
        int suiji=rand()%n+1;
        while(cun&&~used[suiji])
            suiji=rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        nex[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    while(last!=-1&&now<x)
    {
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=nex[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            nex[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    printf("! %d\n",now);
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int nex[50005];

int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&s,&x);
    memset(nex,-1,sizeof(nex));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    nex[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=1000;i++)
    {
        int suiji=rand()%n+1;
        while(cun&&~used[suiji])
            suiji=rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        nex[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    while(last!=-1&&now<x)
    {
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=nex[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            nex[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    if(now<x)
        printf("! -1\n");
    else
        printf("! %d\n",now);
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<random>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int nex[50005];

int main()
{
    mt19937 mt_rand(time(0));
    scanf("%d%d%d",&n,&s,&x);
    memset(nex,-1,sizeof(nex));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    nex[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=999;i++)
    {
        int suiji=mt_rand()%n+1;
        while(cun&&~used[suiji])
            suiji=mt_rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        nex[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    cun=998;
    while(cun&&last!=-1&&now<x)
    {
        cun--;
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=nex[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            nex[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    if(now<x)
        printf("! -1\n");
    else
        printf("! %d\n",now);
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int nex[50005];

int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&s,&x);
    memset(nex,-1,sizeof(nex));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    nex[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=999;i++)
    {
        int suiji=rand()%n+1;
        while(cun&&~used[suiji])
            suiji=rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        nex[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    cun=999;
    while(cun&&last!=-1&&now<x)
    {
        cun--;
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=nex[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            nex[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    if(now<x)
        printf("! -1\n");
    else
        printf("! %d\n",now);
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int next[50005];

int main()
{
    srand(time(NULL));
    while(scanf("%d%d%d",&n,&s,&x)!=EOF)
    {
        memset(next,-1,sizeof(next));
        memset(used,-1,sizeof(used));
        PII temp=ask(s);
        int now=temp.fi;
        int last=temp.se;
        used[s]=temp.fi;
        next[s]=temp.se;
        for(int i=1;i<=1000;i++)
        {
            int suiji=rand()%n+1;
            while(~used[suiji])
                suiji=rand()%n+1;
            temp=ask(suiji);
            used[suiji]=temp.fi;
            next[suiji]=temp.se;
            if(temp.fi<=x&&temp.fi>=now)
            {
                now=temp.fi;
                last=temp.se;
            }
        }
        while(last!=-1&&now<x)
        {
            if(~used[last])
            {
                now=used[last];
                last=next[last];
            }
            else
            {
                temp=ask(last);
                used[last]=temp.fi;
                next[last]=temp.se;
                now=temp.fi;
                last=temp.se;
            }
        }
        printf("! %d\n",now);
    }
    return 0;
}


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>

#define LL long long
#define UI unsigned int
#define mp make_pair
#define fi first
#define se second
#define PLL pair<LL,LL>
#define PII pair<int,int>

using namespace std;

int gcd(int x,int y){   if(y==0)return x;   return gcd(y,x%y);  }
int lcm(int x,int y){   return x/gcd(x,y)*y;    }
int lowbit(int x){  return x&(-x);  }
int quickmod(int x,int y,int mod){int ans=1;while(y){if(y&1)ans=ans*1LL*x%mod;y>>=1;x=x*1LL*x%mod;}return ans;}

const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const int MOD = 1e9+7;

int n,s,x;
PII ask(int pos)
{
    printf("? %d\n",pos);
    //fflush(stdout);
    int temp1,temp2;
    scanf("%d%d",&temp1,&temp2);
    return mp(temp1,temp2);
}
int used[50005];
int nex[50005];

int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&s,&x);
    memset(nex,-1,sizeof(nex));
    memset(used,-1,sizeof(used));
    PII temp=ask(s);
    int now=temp.fi;
    int last=temp.se;
    used[s]=temp.fi;
    nex[s]=temp.se;
    int cun=n-1;
    for(int i=1;i<=999;i++)
    {
        int suiji=rand()%n+1;
        while(cun&&~used[suiji])
            suiji=rand()%n+1;
        if(cun==0)
            break;
        temp=ask(suiji);
        used[suiji]=temp.fi;
        nex[suiji]=temp.se;
        if(temp.fi<=x&&temp.fi>now)
        {
            now=temp.fi;
            last=temp.se;
        }
        cun--;
    }
    //printf("hello\n");
    cun=999;
    while(cun&&last!=-1&&now<x)
    {
        cun--;
        //printf("hello\n");
        if(~used[last])
        {
            now=used[last];
            last=nex[last];
        }
        else
        {
            temp=ask(last);
            used[last]=temp.fi;
            nex[last]=temp.se;
            now=temp.fi;
            last=temp.se;
        }
    }
    if(now<x)
        printf("! -1\n");
    else
        printf("! %d\n",now);
    return 0;
}


#include <ctime>

#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const int N=50010;
int n,x,start;
bool vis[N];

PII ask(int a) {
    printf("? %d\n",a);
    fflush(stdout);
    int tmp=0;
    int nxt=0;
    scanf("%d%d",&tmp,&nxt);
    return {tmp,nxt};
}

int main()
{
    srand(time(0)+INF);
    scanf("%d%d%d",&n,&start,&x);
    PII s=ask(start);
    vis[start]=1;
    if (s.first>=x) {
        printf("! %d\n",s.first);
        fflush(stdout);
        return 0;
    }
    vector<PII> vec;
    vec.pb(s);
    for (int i=1;i<min(n,1000);i++) {
        while (true) {
            int idx=(((ll)rand()<<15)|rand())%n+1;
            if (!vis[idx]) {
                PII tmp=ask(idx);
                vec.pb(tmp);
                vis[idx]=true;
                break;
            }
        }
    }
    sort(vec.begin(),vec.end());
    int idx=0;
    while (idx<vec.size()&&vec[idx].first<x) idx++;
    int target=0;
    if (idx==vec.size()) {
        target=vec.back().second;
        if (target==-1) {
            printf("! -1\n");
            return 0;
        }
    } else {
        if (vec[idx].first==x) {
            printf("! %d\n",x);
            fflush(stdout);
            return 0;
        } else if (idx==0) {
            target=start;
        } else {
            target=vec[idx-1].second;
        }
    }

    int val=0;
    int nxt=0;
    printf("? %d\n",target);
    fflush(stdout);
    scanf("%d%d",&val,&nxt);
    while (val<x&&nxt!=-1) {
        printf("? %d\n",nxt);
        fflush(stdout);
        scanf("%d%d",&val,&nxt);
    }

    if (val>=x) printf("! %d\n",val);
    else printf("! -1\n");
    fflush(stdout);
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const int N=50010;
int n,x,start;
bool vis[N];

PII ask(int a) {
    printf("? %d\n",a);
    fflush(stdout);
    int tmp=0;
    int nxt=0;
    scanf("%d%d",&tmp,&nxt);
    return {tmp,nxt};
}

int main()
{
    scanf("%d%d%d",&n,&start,&x);
    PII s=ask(start);
    vis[start]=1;
    if (s.first>=x) {
        printf("! %d\n",s.first);
        fflush(stdout);
        return 0;
    }
    vector<PII> vec;
    vec.pb(s);
    for (int i=1;i<min(n,1000);i++) {
        while (true) {
            int idx=(rand()%n)+1;
            if (!vis[idx]) {
                PII tmp=ask(idx);
                vec.pb(tmp);
                vis[idx]=true;
                break;
            }
        }
    }
    sort(vec.begin(),vec.end());
    int idx=0;
    while (idx<vec.size()&&vec[idx].first<x) idx++;
    int target=0;
    if (idx==vec.size()) {
        target=vec.back().second;
        if (target==-1) {
            printf("! %d\n",vec.back().first);
            return 0;
        }
    } else {
        if (vec[idx].first==x) {
            printf("! %d\n",x);
            fflush(stdout);
            return 0;
        } else if (idx==0) {
            target=start;
        } else {
            target=vec[idx-1].second;
        }
    }

    int val=0;
    int nxt=0;
    printf("? %d\n",target);
    fflush(stdout);
    scanf("%d%d",&val,&nxt);
    while (val<x&&nxt!=-1) {
        printf("? %d\n",nxt);
        fflush(stdout);
        scanf("%d%d",&val,&nxt);
    }

    printf("! %d\n",val);
    fflush(stdout);
}