Under construction

Still being built. Things may change

Electronic devices

Time limit1sMemory limit512 MB

Summary
Assign distinct power supplies to components so every device i gets at least Y_i working components, with a supply's chosen power matching the component's exact requirement, and output the lexicographically smallest connection list.
Level

Hard8 of 10

Topics
Greedy, Sorting, Hash map, Implementation
Solved
No attempts yet

Problem

Jongbeom has NN electronic devices at home. Device ii is made of XiX_i components, and component jj of device ii works only when it receives exactly Ai,jA_{i,j} units of power. With less than Ai,jA_{i,j} it does not have enough power to run, and with more than Ai,jA_{i,j} it burns out. Device ii can be used when at least YiY_i of its components work.

Jongbeom has MM power supplies. Power supply kk picks one of ZkZ_k distinct power values and feeds it to exactly one component. The ll-th smallest power that supply kk can deliver is Bk,lB_{k,l}.

Until the move, Jongbeom used all NN devices without trouble. He pulled out every cable without writing down which supply had been connected to which component, so nothing works in the new place. Find a set of connections that makes all NN devices usable again.

Input

The first line contains NN. (1≤N≤1001 \le N \le 100)

Lines 2 through 2N+12N + 1 describe the NN devices.

  • Line 2i2i contains XiX_i and YiY_i. (1≤Yi≤Xi≤1001 \le Y_i \le X_i \le 100)
  • Line 2i+12i + 1 contains the integers Ai,1,Ai,2,…,Ai,XiA_{i,1}, A_{i,2}, \dots, A_{i,X_i}. (1≤Ai,j≤1091 \le A_{i,j} \le 10^9)

Line 2N+22N + 2 contains MM. (1≤M≤100001 \le M \le 10000)

Lines 2N+32N + 3 through 2N+2M+22N + 2M + 2 describe the MM power supplies.

  • Line 2N+2k+12N + 2k + 1 contains ZkZ_k. (1≤Zk≤101 \le Z_k \le 10)
  • Line 2N+2k+22N + 2k + 2 contains the integers Bk,1<Bk,2<⋯<Bk,ZkB_{k,1} < B_{k,2} < \dots < B_{k,Z_k} in increasing order. (1≤Bk,l≤1091 \le B_{k,l} \le 10^9)

At least one set of connections that makes all NN devices usable is guaranteed to exist.

Output

Connect exactly YiY_i components on device ii, so the number of connections WW equals Y1+Y2+⋯+YNY_1 + Y_2 + \dots + Y_N.

Print WW on the first line. Each of the next WW lines holds one connection as four integers ii, jj, kk, ll. It means that power supply kk is connected to component jj of device ii, and that the supply delivers the ll-th smallest power it can produce.

A component may appear at most once, and a power supply may appear at most once. The power a connected component needs must equal the power its supply delivers.

Print the WW lines sorted by comparing the four integers in the order ii, jj, kk, ll. If several answers satisfy the rules, print the one whose sequence i1,j1,k1,l1,i2,j2,…i_1, j_1, k_1, l_1, i_2, j_2, \dots of all 4W4W integers, read from the top line down, is lexicographically smallest.

Hint

The answer to the first example has 5 connections.

Device 1 takes power 4 on component 1 from supply 2, and power 3 on component 3 from supply 1. Two components work, which meets Y1=2Y_1 = 2, so device 1 can be used.

Device 2 takes power 4 on component 2 from supply 3, power 1 on component 3 from supply 5, and power 4 on component 4 from supply 4. Three components work, so device 2 can be used as well.

Component 1 of device 2 needs power 2, and supply 2 is the only one that can deliver power 2. In the lexicographically smallest answer, component 1 of device 1 takes supply 2, so device 2 connects its other three components.

Examples2

  1. Example 1

    Input
    2
    3 2
    4 1 3
    4 3
    2 4 1 4
    5
    2
    3 5
    2
    2 4
    1
    4
    3
    3 4 5
    2
    1 5
    
    Expected output
    5
    1 1 2 2
    1 3 1 1
    2 2 3 1
    2 3 5 1
    2 4 4 2
    
  2. Example 2

    Input
    2
    3 2
    5 9 5
    2 1
    9 5
    3
    1
    9
    2
    5 9
    1
    5
    
    Expected output
    3
    1 1 2 1
    1 2 1 1
    2 2 3 1