using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {

        static void Main(string[] args)
        {
            //??????????´??????????
            List<int> resultlist = new List<int>();

            //???????????????????????????
            string[] dice = Console.ReadLine().Split(' ');

            var diceclass = new Dice(int.Parse(dice[0]), int.Parse(dice[1]), int.Parse(dice[2]), int.Parse(dice[3]), int.Parse(dice[4]), int.Parse(dice[5]));

            string questionnum = Console.ReadLine();

            int roopnum = int.Parse(questionnum);

            for(int i = 0; i < roopnum; i++)
            {
                string[] question = Console.ReadLine().Split(' ');

                //????????????????????¢??????????????§??????????????????
                while (diceclass.Up != int.Parse(question[0]))
                {
                    //?????¨??????????????°????????????????§???????????????????
                    var upflowPlan = diceclass.GetFlow(int.Parse(question[0]));
                    
                    //?????¨????????´?????????????????¢????§?????????? ?????§??\?????????????????????
                    switch(upflowPlan)
                    {
                        case "Under":
                            diceclass.MoveDice("N");
                            diceclass.MoveDice("N");
                            break;

                        case "S":
                            diceclass.MoveDice("N");
                            break;

                        case "E":
                            diceclass.MoveDice("W");
                            break;

                        case "W":
                            diceclass.MoveDice("E");
                            break;

                        case "N":
                            diceclass.MoveDice("S");
                            break;

                        case "Up":
                            break;
                    }

                }

                var sflowPlan = diceclass.GetFlow(int.Parse(question[1]));

                //??\?????????????????????
                switch (sflowPlan)
                {
                    case "S":
                        resultlist.Add(diceclass.E);
                        break;

                    case "E":
                        resultlist.Add(diceclass.N);
                        break;

                    case "N":
                        resultlist.Add(diceclass.W);
                        break;

                    case "W":
                        resultlist.Add(diceclass.S);
                        break;                        
                }

            }

            //????????????
            foreach(var result in resultlist)
            {
                Console.WriteLine(result);
            }

        }

    }

    class Dice
    {
        //??????????????°
        public int N = -1;
        public int Under = -1;
        public int E = -1;
        public int W = -1;
        public int Up = -1;
        public int S = -1;

        Dictionary<string, int> diceEye;


        #region ?????????????????????
        //????????????????????¨????????????????????????
        public Dice(int up, int s, int e, int w, int n, int under)
        {
            CreateDictionary(up, s, e, w, n, under);

            Up = up;
            S = s;
            E = e;
            W = w;
            N = n;
            Under = under;
        }
        #endregion



        public void MoveDice(string direction)
        {
            //????????¨????????°???????§??????????????????????
            this.Under = this.diceEye[direction];


            //????????¨???Under?????°???????????°???????§??????????
            //??¢?????????????§??????°??????????????????(S ??? N, W ??? E)
            switch (direction)
            {
                case "S":
                    this.N = this.diceEye["Under"];
                    this.S = this.diceEye["Up"];
                    this.Up = this.diceEye["N"];
                    break;


                case "E":
                    this.W = this.diceEye["Under"];
                    this.E = this.diceEye["Up"];
                    this.Up = this.diceEye["W"];
                    break;


                case "N":
                    this.S = this.diceEye["Under"];
                    this.N = this.diceEye["Up"];
                    this.Up = this.diceEye["S"];
                    break;


                case "W":
                    this.E = this.diceEye["Under"];
                    this.W = this.diceEye["Up"];
                    this.Up = this.diceEye["E"];
                    break;
            }

            //?????£????????§??????????????????
            CreateDictionary(this.Up, this.S, this.E, this.W, this.N, this.Under);
        }


        //?????£????????§?????????????????????
        public void CreateDictionary(int up, int s, int e, int w, int n, int under)
        {
            this.diceEye = new Dictionary<string, int>();

            this.diceEye.Add("Up", up);
            this.diceEye.Add("S", s);
            this.diceEye.Add("E", e);
            this.diceEye.Add("W", w);
            this.diceEye.Add("N", n);
            this.diceEye.Add("Under", under);

        }


        //???????????°???????????????????????????????????????
        public string GetFlow(int eye)
        {
            List<string> keylist = new List<string>(this.diceEye.Keys);

            string result = string.Empty;

            foreach(var key in keylist)
            {
                if(eye == diceEye[key])
                {
                    result = key;
                    break;
                }
            }

            return result;
        }

    }




}

Measured memory_bytes: 9300