
ZOJ Problem Set  3749
Given n groups of integers(all the integers are distinct). You should answer Q queries in this problem. Each query contains a pair of integers (i, j). For each query, you must perform below operations:
InputThe input file contains multiple test cases. Each case begin with an integer n which indicate the amount of group. The following n lines each describe a group of integers. The format of these lines is: m_{i} a_{i1} a_{i2} a_{i3} ... a_{imi} (1 ≤ i ≤ n, 1 ≤ a_{ij} ≤ 10^{9}) We guarantee that m_{1} + m_{2} + m_{3} + ... + m_{n} ≤ 10^{5} There is a number Q(1 ≤ Q ≤ 10^{5}) after these groups of integers. This number is followed by Q lines, each contains two numbers (i, j)(1 ≤ i, j ≤ n and i ≠ j) which describe a query. Process to the End Of File. OutputOutput an integer in a line for each query. Sample Input2 3 1 5 8 3 2 6 7 1 1 2 Sample Output4 HintIn the sample, we get c = {1, 5, 8, 2, 6, 7}, GruopID = {1, 1, 1, 2, 2, 2}. After sorting, it becomes c = {1, 2, 5, 6, 7, 8}, GruopID = {1, 2, 1, 2, 2, 1}. Author: CHEN, Weijie Source: ZOJ Monthly, January 2014 