Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

blockquote {
font-family: Menlo, Monaco, "Courier New", monospace;
color: #333333;
display: block;
padding: 8.5px;
margin: 0 0 9px;
font-size: 12px;
line-height: 18px;
background-color: #f5f5f5;
border: 1px solid #ccc;
border: 1px solid rgba(0, 0, 0, 0.15);
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
white-space: pre;
white-space: pre-wrap;
word-break: break-all;
word-wrap: break-word;
}

Problem Statement

In the headquarter building of ICPC (International Company of Plugs & Connectors), there are $M$ light bulbs and they are controlled by $N$ switches.
Each light bulb can be turned on or off by exactly one switch.
Each switch may control multiple light bulbs.
When you operate a switch, all the light bulbs controlled by the switch change their states.
You lost the table that recorded the correspondence between the switches and the light bulbs, and want to restore it.

You decided to restore the correspondence by the following procedure.

At first, every switch is off and every light bulb is off.

You operate some switches represented by $S_1$.

You check the states of the light bulbs represented by $B_1$.

You operate some switches represented by $S_2$.

You check the states of the light bulbs represented by $B_2$.

...

You operate some switches represented by $S_Q$.

You check the states of the light bulbs represented by $B_Q$.

After you operate some switches and check the states of the light bulbs, the states of the switches and the light bulbs are kept for next operations.

Can you restore the correspondence between the switches and the light bulbs using the information about the switches you have operated and the states of the light bulbs you have checked?

Input

The input consists of multiple datasets.
The number of dataset is no more than $50$ and the file size is no more than $10\mathrm{MB}$.
Each dataset is formatted as follows.

$N$ $M$ $Q$
$S_1$ $B_1$
:
:
$S_Q$ $B_Q$

The first line of each dataset contains three integers $N$ ($1 \le N \le 36$), $M$ ($1 \le M \le 1{,}000$), $Q$ ($0 \le Q \le 1{,}000$), which denote the number of switches, the number of light bulbs and the number of operations respectively.
The following $Q$ lines describe the information about the switches you have operated and the states of the light bulbs you have checked.
The $i$-th of them contains two strings $S_i$ and $B_i$ of lengths $N$ and $M$ respectively.
Each $S_i$ denotes the set of the switches you have operated: $S_{ij}$ is either $0$ or $1$, which denotes the $j$-th switch is not operated or operated respectively.
Each $B_i$ denotes the states of the light bulbs: $B_{ij}$ is either $0$ or $1$, which denotes the $j$-th light bulb is off or on respectively.

You can assume that there exists a correspondence between the switches and the light bulbs which is consistent with the given information.

The end of input is indicated by a line containing three zeros.

Output

For each dataset, output the correspondence between the switches and the light bulbs consisting of $M$ numbers written in base-$36$.
In the base-$36$ system for this problem, the values $0$-$9$ and $10$-$35$ are represented by the characters '0'-'9' and 'A'-'Z' respectively.
The $i$-th character of the correspondence means the number of the switch controlling the $i$-th light bulb.
If you cannot determine which switch controls the $i$-th light bulb, output '?' as the $i$-th character instead of the number of a switch.

Sample Input

3 10 3
000 0000000000
110 0000001111
101 1111111100
2 2 0
1 1 0
2 1 1
01 1
11 11 10
10000000000 10000000000
11000000000 01000000000
01100000000 00100000000
00110000000 00010000000
00011000000 00001000000
00001100000 00000100000
00000110000 00000010000
00000011000 00000001000
00000001100 00000000100
00000000110 00000000010
0 0 0

Output for the Sample Input

2222221100
??
0
1
0123456789A

Solution in C++ (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;
typedef long long ll;

string same(string s,string t) {
    string ret = s;
    rep(i,s.size()) {
        if(s[i] == t[i]) continue;
        else ret[i] = 'X';
    }

    return ret;
}

string Xor(string s,string t) {
    string ret = s;
    rep(i,s.size()) {
        if(s[i] == t[i]) ret[i] = '0';
        else ret[i] = '1';
    }
    
    return ret;
}

int main() {

    int n,m,q;
    while(cin >> n >> m >> q) {
        if(n == 0 && m == 0 && q == 0) break;

        vector<string> s(q),b(q);
        rep(i,q) {
            cin >> s[i] >> b[i];
        }

        string ori = "";
        rep(i,n) ori += "0";

        vector<string> ins;
        string pre = ori;
        rep(i,q) {
            pre = Xor(pre,s[i]);
            ins.push_back(pre);
        }

        vector<int> v(n);
        rep(i,n) v[i] = i;

        stringstream ans;

        rep(i,m) {
            vector<string> a0,b1;

            rep(j,q) {
                if(b[j][i] == '0') {
                    a0.push_back(ins[j]);
                }
                else {
                    b1.push_back(ins[j]);
                }
            }

            string res;
            if(a0.size() == 0)  res = ori;
            else res = a0[0];

            rep(j,a0.size()) {
                res = same(res,a0[j]);
            }

            string res2;
            if(b1.size() == 0) res2 = ori;
            else res2 = b1[0];

            rep(j,b1.size()) {
                res2 = same(res2,b1[j]);
            }

            set<int> st;
            rep(j,n) {
                if(res[j] == 'X' || res2[j] == 'X') continue;
                if(res[j] == '0' && res2[j] == '1') {
                    st.insert(j);
                }
            }

            if(st.size() == 1) {
                set<int>::iterator ite = st.begin();
                if(0 <= *ite && *ite <= 9) {
                    ans << *ite;
                }
                else {
                    int id = *ite - 10;
                    ans << char('A' + id);
                }
    
                v.erase(remove(v.begin(),v.end(),*ite),v.end());
            }
            else ans << "?";

        }
        
        string res = ans.str();
        if(v.size() == 1) {
            rep(i,res.size()) {
                if(res[i] == '?') {
                    if(0 <= v[0] && v[0] <= 9) {
                        cout << v[0];
                    }
                    else {
                        int id = v[0] - 10;
                        cout << char('A' + id);
                    }
                }
                else cout << res[i];
            }
            cout << endl;
        }
        else cout << res << endl;


    }

    return 0;
}