• + 0 comments
    import java.util.List;
    import java.util.Scanner;
    import java.util.stream.Stream;
    
    import static java.util.stream.Collectors.toList;
    
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int numberOfArrays = getInputNumber(scanner);
            int numberOfQuerys = 0;
            List<List<Integer>> arraysOfNumbers = new ArrayList<>();
            List<List<Integer>> arrayOfQuery = new ArrayList<>();// x , y in every query
    
            for (int i = 0; i < numberOfArrays; i++) {
                List<Integer> list = getInputListNumbers(scanner);
                list.remove(0);
                arraysOfNumbers.add(list);
            }
    
            numberOfQuerys = getInputNumber(scanner);
            while (numberOfQuerys-- > 0) {
                arrayOfQuery.add(getInputListNumbers(scanner));
            }
            scanner.close();
    
            arrayOfQuery.forEach(list -> {
                int x = list.get(0); // position in array of numbers
                int y = list.get(1);// position in array
                printNumber(x, numberOfArrays, arraysOfNumbers, y);
            });
        }
    
        private static void printNumber(int x, int numberOfArrays, List<List<Integer>> arraysOfNumbers, int y) {
            if(x <0 || x > numberOfArrays){
                System.out.println("ERROR!");
            }
            List<Integer> array = arraysOfNumbers.get(x -1);
            try{
                System.out.println(array.get(y -1));
            }catch (Exception e){
                System.out.println("ERROR!");
            }
        }
    
        private static int getInputNumber(Scanner scanner) {
            return Integer.parseInt(scanner.nextLine());
        }
    
        private static List<Integer> getInputListNumbers(Scanner scanner) {
            return Stream.of(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(toList());
        }
    }