Messengers
Time limit5sMemory limit128 MB
Given merchant journals of letter sends and receives, answer for each query which of two letters was sent first or whether the order is unknown.
- Level
Hard8 of 10
- Topics
- Graph, Topological sort, Bit manipulation
- Solved
- No attempts yet
Statement
Long ago, in a certain kingdom, there lived merchants. They did back then what merchants still do today: buy goods as cheaply as possible and sell them as dearly as possible. The tools of the trade, however, were different. All communication between the merchants was carried out by messengers.
A messenger carries a letter between merchants, and each letter goes from exactly one merchant (the sender) to exactly one other merchant (the recipient). Depending on the circumstances, the weather, and the messenger's mood and stamina, delivering a letter could take a moment, a longer moment, or a very long time. Every merchant records their correspondence in a journal: right after sending a letter, and right after receiving one, they write it down, keeping strict chronological order. Each journal is therefore a sequence of events, and every event has one of two forms.
- sent(): some letter was sent.
- received(): some letter was received.
Because the merchants had no precise clocks, they never wrote down the time at which an event happened.
After a run of suspicious deals, the king's inspectors began to suspect some merchants of smuggling, espionage, fraud, and speculation. To reconstruct the cause-and-effect order of events on the market, the king ordered a sweeping audit of the merchants' correspondence.
Given the contents of every merchant's journal, can you decide, for any two letters, which one was sent earlier? We say that letter was sent before letter if there is a sequence of events (that is, journal entries) such that:
- for every pair of adjacent events , at least one of the following holds:
- and are consecutive events in the journal of the same merchant (so must have happened before ), or
- and for some letter , i.e. is the receipt of the letter sent in (so again must have happened before ).
Input
The first line contains a natural number (), the number of test sets; the sets follow one after another. (There is always exactly one test set.)
The first line of a test set contains two space-separated natural numbers and (; ): the number of merchants and the number of queries. Each of the next lines describes one merchant's journal.
A journal is given as a single natural number (the number of events recorded in it), followed by space-separated integers . A positive means sending the letter with identifier ; a negative means receiving the letter with identifier . Every letter identifier is unique and lies between and , where is the total number of letters exchanged. Letter identifiers do not reflect the order in which letters were sent; they are only labels. is not given explicitly, but it is guaranteed that .
Each of the next lines contains one query: two distinct letter identifiers separated by a single space.
It is guaranteed that the input is never contradictory, i.e. there is no pair of letters for which one could infer both that was sent before and that was sent before .
Output
For the test set, print exactly lines, one per query in the given order. For each query, print the identifier of the letter that was sent first, or a single question mark ? if the journals do not determine which of the two was sent earlier.
Hint

The figure above shows how the merchants' journals and the letters passing between them determine the happens-before order of events.