Simple input parsing. Easy.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P12503 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for (int i = 0; i < T; i++) { int N = sc.nextInt(); int[] moves = new int[N]; int ans = 0; for (int j = 0; j < N; j++) { String input = sc.next(); if (input.equals("LEFT")) { moves[j] = -1; } else if (input.equals("RIGHT")) { moves[j] = 1; } else { sc.next(); moves[j] = moves[sc.nextInt() - 1]; } ans += moves[j]; } out.println(ans); } out.close(); sc.close(); } }