using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            //入力読取。
            string strDice = Console.ReadLine();
            string strDate = Console.ReadLine();
            int intDate = int.Parse(strDate);
            List<int> faceList = new List<int>();
            for(int date = 0; date < intDate; date++){
                string strOutCome = Console.ReadLine();
                
                //サイコロのクラスにて処理。
                Dice insD = new Dice(strDice, strOutCome);
                int intFace = insD.GetSide();
                faceList.Add(intFace);
            }

            //出力
            foreach(int face in faceList){
                Console.WriteLine(face);
            }
            
        }
        
    }
    
    class Dice{
        
        private string strDice;
        private string strOutCome;
        private int[] dice;
        private int intSide;
        
        public Dice(string strDice, string strOutCome){
            this.strDice = strDice;
            this.strOutCome = strOutCome;
            SetDice();
        }
        
        private void SetDice(){
        
            //サイコロ生成。
            string[] strDiceArray = strDice.Split(' ');
            this.dice = new int[6];
            for(int index = 0; index < 6; index++){
                dice[index] = int.Parse(strDiceArray[index]);
            }
            
            //指定されたサイコロの出目。
            string[] outComeArray = strOutCome.Split(' ');
            int intTop = int.Parse(outComeArray[0]);
            int intFront = int.Parse(outComeArray[1]);
            
            //サイコロを転がす。
            string strOrder = "NNNNWNNNWNNNENNNENNNWNNN";
            char[] chOrder = strOrder.ToCharArray();
            for(int order = 0; order < chOrder.Length; order++){
                Roll(chOrder[order]);
                if(dice[0] == intTop && dice[1] == intFront){
                    this.intSide = dice[2];
                    break;
                }
            }
            
        }
        
        private void Roll(char chDirection){
            if(chDirection == 'N'){
                int intTemp = dice[0];
                dice[0] = dice[1];
                dice[1] = dice[5];
                dice[5] = dice[4];
                dice[4] = intTemp;
            }else if(chDirection == 'W'){
                int intTemp = dice[0];
                dice[0] = dice[2];
                dice[2] = dice[5];
                dice[5] = dice[3];
                dice[3] = intTemp;
            }else if(chDirection == 'E'){
                int intTemp = dice[0];
                dice[0] = dice[3];
                dice[3] = dice[5];
                dice[5] = dice[2];
                dice[2] = intTemp;
            }else{
                int intTemp = dice[0];
                dice[0] = dice[4];
                dice[4] = dice[5];
                dice[5] = dice[1];
                dice[1] = intTemp;
            }
        }
        
        public int GetTop(){
            return dice[0];
        }
        
        public int GetSide(){
            return intSide;
        }
        
        
    }

}



Measured memory_bytes: 15952