• + 0 comments

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

        // Read number of lines
        int n = sc.nextInt();
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();
    
        // Read each line
        for (int i = 0; i < n; i++) {
            int d = sc.nextInt(); // number of integers in this line
            ArrayList<Integer> line = new ArrayList<>();
            for (int j = 0; j < d; j++) {
                line.add(sc.nextInt());
            }
            list.add(line);
        }
    
        // Read number of queries
        int q = sc.nextInt();
    
        // Process each query
        for (int i = 0; i < q; i++) {
            int x = sc.nextInt(); // line number
            int y = sc.nextInt(); // position in line
    
            // Check if valid indices
            if (x <= list.size() && y <= list.get(x - 1).size()) {
                System.out.println(list.get(x - 1).get(y - 1));
            } else {
                System.out.println("ERROR!");
            }
        }
    
        sc.close();
    }
    

    }