Problem: cf753_c
Difficulty: medium
Categories: greedy
Time limit: 2000 ms, memory limit: 256 MB

C. Interactive Bulls and Cows (Hard)

The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests.

This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java — call System.out.flush(), and in Pascal — flush(output).

Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.

On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.

Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Thus a response is a pair of numbers — the number of "bulls" and the number of "cows". A try can contain equal digits.

More formally, let's the secret string is *s* and the second player are trying to guess it with a string *x*. The number of "bulls" is a number of such positions *i* (1 ≤ *i* ≤ 4) where *s*[*i*] = *x*[*i*]. The number of "cows" is a number of such digits *c* that *s* contains *c* in the position *i* (i.e. *s*[*i*] = *c*), *x* contains *c*, but *x*[*i*] ≠ *c*.

For example, the secret string is "0427", the opponent's try is "0724", then the answer is 2 bulls and 2 cows (the bulls are "0" and "2", the cows are "4" and "7"). If the secret string is "0123", the opponent's try is "0330", then the answer is 1 bull and 1 cow.

In this problem you are to guess the string *s* that the system has chosen. You only know that the chosen string consists of 4 distinct digits.

You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals "4 0", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons — the program either guessed the number *x* or made an invalid action (for example, printed letters instead of digits).

Your program is allowed to do at most 7 queries.

Input

To read answers to the queries, the program must use the standard input.

The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string *s* and the string *x**i* printed by your program. If the system response equals "4 0", then your solution should terminate.

The testing system will let your program read the *i*-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value *x**i* in a single line and executes operation flush.

Output

The program must use the standard output to print queries.

Your program must output requests — 4-digit strings *x*1, *x*2, ..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.

Your program is allowed to do at most 7 queries.

Examples

Input

```
0 1  
2 0  
1 1  
0 4  
2 1  
4 0
```

Output

```
8000  
0179  
3159  
3210  
0112  
0123
```

Note

The secret string *s* in the example is "0123".

Non-adaptive interactor (C++):














#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 7; 



static void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}


template <typename... Args>
static void finish(TResult verdict, const char* fmt, Args... args) {
    log_metrics();
    quitf(verdict, fmt, args...);
}


static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

int main(int argc, char** argv) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    
    
    string secret = inf.readToken();
    
    
    if (secret.length() != 4) {
        finish(_fail, "Hidden input (secret) length is not 4. Got: '%s'", secret.c_str());
    }
    {
        set<char> distinct;
        for (char c : secret) {
            if (!isdigit(c)) finish(_fail, "Hidden input contains non-digits");
            distinct.insert(c);
        }
        if (distinct.size() != 4) {
            finish(_fail, "Hidden input digits must be distinct");
        }
    }

    
    log_metrics();

    
    
    
    int hard_cap = 200; 

    while (true) {
        
        string token;
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF: solution terminated without correct guess");
        }

        
        if (token == "?" || token == "!") {
            if (!(cin >> token)) {
                finish(_wa, "Unexpected EOF after protocol prefix");
            }
        } else if (token.length() > 0 && (token[0] == '?' || token[0] == '!')) {
            
            token = token.substr(1);
        }

        
        if (token.length() != 4) {
            finish(_pe, "Query must be a 4-digit string. Got length %d: '%s'", (int)token.length(), token.c_str());
        }
        for (char c : token) {
            if (!isdigit(c)) {
                finish(_pe, "Query must contain only digits. Got: '%s'", token.c_str());
            }
        }

        
        queries++;
        
        
        
        
        int bulls = 0;
        int cows = 0;
        
        for (int i = 0; i < 4; ++i) {
            char s_char = secret[i];
            char x_char = token[i];
            
            if (s_char == x_char) {
                bulls++;
            } else {
                
                
                bool found = false;
                for (int j = 0; j < 4; ++j) {
                    if (token[j] == s_char) {
                        found = true;
                        break;
                    }
                }
                if (found) cows++;
            }
        }

        
        cout << bulls << " " << cows << endl; 
        
        
        log_metrics();

        
        if (bulls == 4) {
            finish(_ok, "Guessed correctly in %lld queries", queries);
        }

        
        if (queries >= hard_cap) {
            finish(_pe, "Hard query limit exceeded (>%d)", hard_cap);
        }
    }

    return 0;
}


Adaptive interactor (C++):







#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 7;

static string secret_fixed;


void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
    }
}


void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}




pair<int, int> calc_bulls_cows(const string& s, const string& g) {
    int bulls = 0;
    int count_s[10] = {0};
    int count_g[10] = {0};
    
    
    for (int i = 0; i < 4; ++i) {
        if (s[i] == g[i]) {
            bulls++;
        }
        count_s[s[i] - '0']++;
        count_g[g[i] - '0']++;
    }
    
    
    int common = 0;
    for (int i = 0; i < 10; ++i) {
        common += min(count_s[i], count_g[i]);
    }
    
    
    int cows = common - bulls;
    return {bulls, cows};
}

bool is_valid_guess(const string& s) {
    if (s.length() != 4) return false;
    for (char c : s) {
        if (!isdigit(c)) return false;
    }
    return true;
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    
    
    string token = inf.readToken();
    if ((int)token.size() == 4) {
        secret_fixed = token;
    } else {
        long long seed_val = 0;
        try {
            seed_val = stoll(token);
        } catch (...) {
            finish(_fail, "Invalid seed in hidden input");
        }
        mt19937 rng((uint32_t)seed_val);
        vector<int> digs(10);
        iota(digs.begin(), digs.end(), 0);
        shuffle(digs.begin(), digs.end(), rng);
        secret_fixed.clear();
        for (int i = 0; i < 4; i++) secret_fixed.push_back(char('0' + digs[i]));
    }

    if (!is_valid_guess(secret_fixed)) finish(_fail, "Secret is not 4 digits");
    {
        bool used[10] = {0};
        for (char c : secret_fixed) {
            int d = c - '0';
            if (used[d]) finish(_fail, "Secret digits not distinct");
            used[d] = true;
        }
    }

    
    log_metrics();

    while (true) {
        
        string guess;
        if (!(cin >> guess)) {
            finish(_wa, "Unexpected EOF: solution terminated without correct guess");
        }

        if (guess == "?" || guess == "!") {
            if (!(cin >> guess)) finish(_pe, "Unexpected EOF after protocol prefix");
        } else if (!guess.empty() && (guess[0] == '?' || guess[0] == '!')) {
            guess = guess.substr(1);
        }

        if (!is_valid_guess(guess)) {
            finish(_pe, "Invalid query format");
        }

        queries++;
        log_metrics(); 

        
        
        
        if (queries > 200) {
            finish(_pe, "Too many queries (hard cap)");
        }

        pair<int, int> response = calc_bulls_cows(secret_fixed, guess);

        
        cout << response.first << " " << response.second << endl;

        
        if (response.first == 4 && response.second == 0) {
            finish(_ok, "Guessed " + guess);
        }
    }

    return 0;
}


Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    string mode = opt<string>("mode", "non");

    if (mode == "adp") {
        
        
        cout << rnd.next(100000000, 999999999) << endl;
    } else {
        
        
        
        long long seed_val = 0;
        try {
            if (argc > 1) seed_val = stoll(argv[1]);
        } catch (...) {
            seed_val = 0;
        }

        if (seed_val == 1) {
            
            cout << "0123" << endl;
        } else if (seed_val == 2) {
            
            cout << "9876" << endl;
        } else if (seed_val == 3) {
            
            cout << "1209" << endl;
        } else {
            
            vector<int> p(10);
            iota(p.begin(), p.end(), 0);
            shuffle(p.begin(), p.end());
            
            for (int i = 0; i < 4; ++i) {
                cout << p[i];
            }
            cout << endl;
        }
    }

    return 0;
}