I couldn't find the bug. I used nextLine() after reading nextInt() but still next() is not reading the word("UPDATE"). What am I missing?
Input to read:
    2
    4 5
    UPDATE 2 2 2 4
    QUERY 1 1 1 3 3 3
    UPDATE 1 1 1 23
    QUERY 2 2 2 4 4 4
    QUERY 1 1 1 3 3 3
    2 4
    UPDATE 2 2 2 1
    QUERY 1 1 1 1 1 1
    QUERY 1 1 1 2 2 2
    QUERY 2 2 2 2 2 2
My code:
   Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i=0 ; i<t ; i++){
            int n = sc.nextInt();
            int m = sc.nextInt();
            sc. nextLine ();
            System.out.println(n+ " " + m);
            for(int j=0 ; j<m ; j++){
                String Q = sc.next(); // tried (String)sc.next(); also
                if(Q == "UPDATE"){
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    int z = sc.nextInt();
                    int val = sc.nextInt();
                    sc.nextLine();
                    System.out.println(x+ " " + y + " "+ z + " "+ val);
                }
                else if(Q == "QUERY"){
                    int x1 = sc.nextInt()-1;
                    int y1 = sc.nextInt()-1;
                    int z1 = sc.nextInt()-1;
                    int x2 = sc.nextInt();
                    int y2 = sc.nextInt();
                    int z2 = sc.nextInt();
                    sc.nextLine();
                  System.out.println(x1+ " " + y1 + " "+ z1 + " "+ x2+ " " + y2 + " "+ z2);
                }
            }
        }
 
     
     
    