Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6

Solution in JAVA (judge verdict: Accepted)

import java.util.Scanner;
 
/**
 * @author kawakami
 *
 */
class Main {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner as = new Scanner(System.in);
         
        int dice[];                  //?????????????????????
        int pter[][];                //???????????????????????????
        int que = 0;                 //??????????????°
        int mei1 = 0;                //?????¢?????°???
        int mei2 = 0;                //?????¢?????°???
        String str;
         
        dice = new int[6];
        pter = new int[6][6];
         
        for(int i=0;i<6;i++){
             
            dice[i] = as.nextInt();
             
        }
         
        for(int i=0;i<6;i++){        //?????°???????????¢?????¨????????´?????¢???????????????????????????????????????
             
            switch(i){
                case 0:
                    pter[0][1] = dice[2];
                    pter[0][2] = dice[4];
                    pter[0][4] = dice[3];
                    pter[0][3] = dice[1];
                    break;
                 
                case 1:
                    pter[1][5] = dice[2];
                    pter[1][2] = dice[0];
                    pter[1][0] = dice[3];
                    pter[1][3] = dice[5];
                    break;
                     
                case 2:
                    pter[2][5] = dice[4];
                    pter[2][4] = dice[0];
                    pter[2][0] = dice[1];
                    pter[2][1] = dice[5];
                    break;
                     
                case 3:
                    pter[3][5] = dice[1];
                    pter[3][1] = dice[0];
                    pter[3][0] = dice[4];
                    pter[3][4] = dice[5];
                    break;
                     
                case 4:
                    pter[4][5] = dice[3];
                    pter[4][3] = dice[0];
                    pter[4][0] = dice[2];
                    pter[4][2] = dice[5];
                    break;
                     
                case 5:
                    pter[5][4] = dice[2];
                    pter[5][2] = dice[1];
                    pter[5][1] = dice[3];
                    pter[5][3] = dice[4];
                    break;
            }
             
        }
         
        que = as.nextInt();
         
        for(int i=0;i<que;i++){                    //??????????????°?????°?????????
             
            mei1 = as.nextInt();                   //?????¢?????°???
            mei2 = as.nextInt();                   //?????¢?????°???
             
            mei1 = ArrayUtils.indexOf(dice,mei1);  //???????????????????????????????????¨??????????????\??£?????????????´???°?????????
            mei2 = ArrayUtils.indexOf(dice,mei2);
             
            str = String.valueOf(pter[mei1][mei2]);//?????¢??¨?????¢?????°????????\??£??????????????????????????????
            System.out.println(str);
             
        }
         
         
 
    }
 
}
 
//ArrayUtils?????????
//?????????:Java????????????????????????????????¨????????????
//http://javasampleokiba.blog.fc2.com/page-6.html
 
class ArrayUtils {
      
	 /**
	  * ???????????????????´????????????§??????????????§?????????????????????????????????????????????????????????????????????
	   *
	   * @param array    ????´¢????????????
	    * @param obj       ????´¢??????????´?
	   * @return ???????????????????´???????????????§??????????????????????????????????????????????????????
	    *               ??????????´?????????????????????´?????? -1
	    */
 public static int indexOf(Object[] array, Object obj) {
     return indexOf(array, obj, 0);
  }
  
 /**
  * ???????????????????´???????int??????????????§?????????????????????????????????????????????????????????????????????
  *
   * @param array    ????´¢????????????
    * @param val       ????´¢??????????´?
   * @return ???????????????????´???????????????§??????????????????????????????????????????????????????
    *               ??????????´?????????????????????´?????? -1
    */
 public static int indexOf(int[] array, int val) {
       return indexOf(array, val, 0);             //?????????????????????????????????????????????????????????????´¢??????
  }
  
 /**
  * ???????????????????´????????????§??????????????§????´¢????§?????????\??????
     * ?????????????????????????????????????????????????????????????????????
   *
   * @param array    ????´¢????????????
    * @param obj       ????´¢??????????´?
   * @param start    ????´¢????§???????
    * @return ???????????????????´???????????????§??????????????????????????????????????????????????????
    *               ??????????´?????????????????????´?????? -1
    */
 public static int indexOf(Object[] array, Object obj, int start) {
      if (array.length == 0) {
            return -1;
      }
       if (obj != null) {
          for (int i = start; i < array.length; i++) {
             if (obj.equals(array[i])) return i;
         }
       } else {
            for (int i = start; i < array.length; i++) {
             if (array[i] == null) return i;
         }
       }
       return -1;
  }
  
 /**
  * ???????????????????´???????int??????????????§????´¢????§?????????\??????
    * ?????????????????????????????????????????????????????????????????????
   *
   * @param array    ????´¢????????????
    * @param val       ????´¢??????????´?
   * @param start    ????´¢????§???????
    * @return ???????????????????´???????????????§??????????????????????????????????????????????????????
    *               ??????????´?????????????????????´?????? -1
    */
 public static int indexOf(int[] array, int val, int start) {
        if (array.length == 0) {             //??????????????????0?????¨???
            return -1;                       //-1?????????
        }
     for (int i = start; i < array.length; i++) {      //????????????????????§??°?????????
         if (val == array[i]) return i;                //????????????????????¨??????????????????????????????????´???°?????????
      }
       return -1;                                      //?????????????????????????´??????????????????????????????£?????´???
  }                                                    //-1?????????
}