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

namespace _20170409_AOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArr = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            int q = int.Parse(Console.ReadLine());
            
            Dice dice = new Dice(iArr);
            for (int i = 0; i < q; i++)
            {
                int[] fArr = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
                dice.SetTopFront(fArr[0], fArr[1]);
                Console.WriteLine(dice.Right);
            }

            Console.ReadLine();
        }
    }

    class Dice
    {
        public enum Dir
        {
            N,
            S,
            W,
            E,
            L,
            R
        }


        int[] iArr = new int[6];
        int[,] tenkai;
        public Dice(int[] arr)
        {
            int len = Math.Min(iArr.Length, arr.Length);
            for (int i = 0; i < len; i++)
            {
                iArr[i] = arr[i];
            }

            tenkai = CreateTenkai();
        }

        public int Top { get { return iArr[tenkai[1, 1]]; } }
        public int Bottom { get { return iArr[tenkai[1, 3]]; } }
        public int Front { get { return iArr[tenkai[2, 1]]; } }
        public int Back { get { return iArr[tenkai[0, 1]]; } }
        public int Left { get { return iArr[tenkai[1, 0]]; } }
        public int Right { get { return iArr[tenkai[1, 2]]; } }

        public void SetTopFront(int _top, int _front)
        {
            for (int i = 0; i < 3; i++)
            {
                if (_top == Top)
                {
                    break;
                }
                Move(Dir.N);
                if (_top == Top)
                {
                    break;
                }
                Move(Dir.W);
            }

            for (int i = 0; i < 3; i++)
            {
                if (_front == Front)
                {
                    break;
                }
                Move(Dir.L);
            }
        }

        public void Move(char c)
        {
            switch (c)
            {
                case 'N': Move(Dir.N); break;
                case 'S': Move(Dir.S); break;
                case 'W': Move(Dir.W); break;
                case 'E': Move(Dir.E); break;
                case 'L': Move(Dir.L); break;
                case 'R': Move(Dir.R); break;
            }
        }

        public void Move(Dir dir)
        {
            switch(dir)
            {
                case Dir.N:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[2, 1];
                        tenkai[2, 1] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[0, 1];
                        tenkai[0, 1] = tmp;
                    }
                    break;
                case Dir.S:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[0, 1];
                        tenkai[0, 1] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[2, 1];
                        tenkai[2, 1] = tmp;
                    }
                    break;
                case Dir.W:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[1, 2];
                        tenkai[1, 2] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[1, 0];
                        tenkai[1, 0] = tmp;
                    }
                    break;
                case Dir.E:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[1, 0];
                        tenkai[1, 0] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[1, 2];
                        tenkai[1, 2] = tmp;
                    }
                    break;
                case Dir.L:
                    {
                        int tmp = tenkai[2, 1];
                        tenkai[2, 1] = tenkai[1, 2];
                        tenkai[1, 2] = tenkai[0, 1];
                        tenkai[0, 1] = tenkai[1, 0];
                        tenkai[1, 0] = tmp;
                    }
                    break;
                case Dir.R:
                    {
                        int tmp = tenkai[2, 1];
                        tenkai[2, 1] = tenkai[1, 0];
                        tenkai[1, 0] = tenkai[0, 1];
                        tenkai[0, 1] = tenkai[1, 2];
                        tenkai[1, 0] = tmp;
                    }
                    break;
            }
        }

        private int[,] CreateTenkai()
        {
            int[,] arr = new int[,]{
                { -1,  4, -1, -1 },
                {  3,  0,  2,  5 },
                { -1,  1, -1, -1 },
            };

            return arr;
        }
    }
}

Measured memory_bytes: 9496