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


namespace ConsoleApplication25
{
    class Dice
    {
        string[] D;

        string[] Copy()
        {
            string[] Di = new string[D.Length];
            for (int i = 0; i < D.Length; ++i)
            {
                Di[i] = D[i];
            }
            return Di;
        }

        public Dice(string[] DArray)
        {
            D = DArray;
        }

        public string[] DiceRoll(string roll)
        {
            int index = FindIndex(roll);
                switch (index)
                {
                    case 0:
                        break;
                   
                    case 1:
                        RollNorth();
                        break;
                    
                    case 2:
                        RollWest();
                        break;

                    case 3:
                        RollEast();
                        break;

                    case 4:
                        RollSouth();
                        break;

                    case 5:
                        RollNorth();
                        RollNorth();
                        break;
                }
                string[] DiceArray = Copy();
                return DiceArray;
            }

        void RollNorth ()
        {
                string[] Dic = Copy();

                D[0] = Dic[1];
                D[1] = Dic[5];
                D[4] = Dic[0];
                D[5] = Dic[4];
        }

        void RollEast()
        {
            string[] Dic = Copy();

            D[0] = Dic[3];
            D[2] = Dic[0];
            D[3] = Dic[5];
            D[5] = Dic[2];
        }

        void RollSouth()
        {
            string[] Dic = Copy();

            D[0] = Dic[4];
            D[1] = Dic[0];
            D[4] = Dic[5];
            D[5] = Dic[1];
        }

        void RollWest()
        {
            string[] Dic = Copy();

            D[0] = Dic[2];
            D[2] = Dic[5];
            D[3] = Dic[0];
            D[5] = Dic[3];
        }

        public string DiceRoll2(string s)
        {
            int index = FindIndex(s);

            if ((index == 0) || (index == 5))
            {
                return "";
            }

            while (D[1] != s)
            {
                string temp;
                temp = D[2];
                D[2] = D[1];
                D[1] = D[3];
                D[3] = D[4];
                D[4] = temp;
            }
            
            return D[2];
         }

        public int FindIndex(string s)
        {
            for (int i = 0; i < D.Length; ++i)
            {
                if (D[i] == s)
                {
                    return i;
                }
            }
            return -1;
        }
           
        }


    class Program
    {
        static void Main()
        {
            string[] DiceArray = Console.ReadLine().Split(' ');
            int q = int.Parse(Console.ReadLine());
            
            Dice DClass = new Dice(DiceArray);

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

                DClass.DiceRoll(str[0]);

                string right = DClass.DiceRoll2(str[1]);

                Console.WriteLine(right);
            }
        }
    }
    
}

Measured memory_bytes: 5320