//https://www.hackerrank.com/challenges/the-quickest-way-up
import java.io.*;
import java.util.*;

public class Solution {
  private final static byte MAX_ROLL = 6;
  private final static byte BOARD_LEN = 100;

  public static void main(String[] args) throws IOException {
    final StringBuffer sb = new StringBuffer();
    final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    //For every test case
    for(byte T = Byte.parseByte(br.readLine()); T > 0; --T){

      //Initialize board
      final byte[] board = new byte[BOARD_LEN];
      for(byte i = 0; i < BOARD_LEN; ++i){
        board[i] = i;
      }

      //Get snakes
      final byte N = Byte.parseByte(br.readLine());
      for(byte i = 0; i < N; ++i){
        final String[] line = br.readLine().split(" ");
        final byte S = (byte)(Byte.parseByte(line[0]) - 1);
        final byte E = (byte)(Byte.parseByte(line[1]) - 1);
        board[S] = E;
      }

      //Get ladders
      final byte M = Byte.parseByte(br.readLine());
      for(byte i = 0; i < M; ++i){
        final String[] line = br.readLine().split(" ");
        final byte S = (byte)(Byte.parseByte(line[0]) - 1);
        final byte E = (byte)(Byte.parseByte(line[1]) - 1);
        board[S] = E;
      }


      //Solve and print output
      sb.append(getMinMoves(board) + "\n");
    }
    System.out.print(sb);
  }

  private static byte getMinMoves(final byte[] board){

    //Initialize moves counting
    final byte[] moves = new byte[BOARD_LEN];
    for(byte i = 1; i < BOARD_LEN; moves[i++] = BOARD_LEN){}
    final Queue<Byte> q = new PriorityQueue<Byte>(new Comparator<Byte>(){
      @Override
      public int compare(Byte a, Byte b){
        return b.compareTo(a);
      }
    });

    //Go through board and find min moves
    q.add((byte)0);
    do{
      final byte square = q.poll();
      final byte numMoves = (byte)(moves[square] + 1);
      for(byte i = (byte)(square + 1), n = (byte)(Math.min(BOARD_LEN, i + MAX_ROLL)); i < n; ++i){
        final byte j = board[i];
        if(numMoves < moves[j]){
          moves[j] = numMoves;
          q.add(j);
        }
      }
    } while (!q.isEmpty());

    //Check if possible to reach max square
    final byte minMoves = moves[BOARD_LEN - 1];
    return (minMoves == BOARD_LEN) ? -1 : minMoves;
  }
}

@@ -270,56 +270,8 @@
 n %7B%0A
-    final StringBuffer sb = new StringBuffer();%0A
     
@@ -346,24 +346,98 @@
 ystem.in));%0A
+    %0A    //Create board%0A    final byte%5B%5D board = new byte%5BBOARD_LEN%5D;%0A    
 %0A    //For e
@@ -542,56 +542,8 @@
 ard%0A
-      final byte%5B%5D board = new byte%5BBOARD_LEN%5D;%0A
     
@@ -634,29 +634,27 @@
 akes%0A      f
-inal 
+or(
 byte N = Byt
@@ -684,41 +684,19 @@
 ());
-%0A      for(byte i = 0; i %3C N; ++i
+ N %3E 0; --N
 )%7B%0A 
@@ -924,29 +924,27 @@
 ders%0A      f
-inal 
+or(
 byte M = Byt
@@ -974,41 +974,19 @@
 ());
-%0A      for(byte i = 0; i %3C M; ++i
+ M %3E 0; --M
 )%7B%0A 
@@ -1233,17 +1233,26 @@
     
-sb.append
+System.out.println
 (get
@@ -1270,15 +1270,8 @@
 ard)
- + %22%5Cn%22
 );%0A 
@@ -1279,34 +1279,8 @@
   %7D%0A
-    System.out.print(sb);%0A
   %7D%0A

Update the-quickest-way-up.java