Messengers

No attempts yetTime limit5sMemory limit128 MB

Statement

Long ago, in a certain kingdom, there lived NN 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(ww): some letter ww was sent.
  • received(ww): some letter ww 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 aa was sent before letter bb if there is a sequence of events (that is, journal entries) x1,x2,,xkx_1, x_2, \ldots, x_k such that:

  • x1=sent(a)x_1 = \text{sent}(a)
  • xk=sent(b)x_k = \text{sent}(b)
  • for every pair of adjacent events xi,xi+1x_i, x_{i+1}, at least one of the following holds:
    • xix_i and xi+1x_{i+1} are consecutive events in the journal of the same merchant (so xix_i must have happened before xi+1x_{i+1}), or
    • xi=sent(w)x_i = \text{sent}(w) and xi+1=received(w)x_{i+1} = \text{received}(w) for some letter ww, i.e. xi+1x_{i+1} is the receipt of the letter sent in xix_i (so again xix_i must have happened before xi+1x_{i+1}).

Input

The first line contains a natural number ZZ (1Z11 \le Z \le 1), 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 NN and KK (1N1001 \le N \le 100; 1K1000001 \le K \le 100000): the number of merchants and the number of queries. Each of the next NN lines describes one merchant's journal.

A journal is given as a single natural number SiS_i (the number of events recorded in it), followed by SiS_i space-separated integers w1,w2,,wSiw_1, w_2, \ldots, w_{S_i}. A positive wiw_i means sending the letter with identifier wiw_i; a negative wiw_i means receiving the letter with identifier wi|w_i|. Every letter identifier is unique and lies between 11 and CC, where CC is the total number of letters exchanged. Letter identifiers do not reflect the order in which letters were sent; they are only labels. CC is not given explicitly, but it is guaranteed that 1C1000001 \le C \le 100000.

Each of the next KK 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 a,ba, b for which one could infer both that aa was sent before bb and that bb was sent before aa.

Output

For the test set, print exactly KK 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

Journals and letters

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