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

namespace ITP1_11_B
{
    class Dice
    {
        public Dice()
        {
            Top = 0;
            Front = 1;
            Right = 2;
        }

        public void RotateNorth()
        {
            var temp = Inverse(Top);
            Top = Front;
            Front = temp;
        }


        public void RotateSouth()
        {
            var temp = Inverse(Front);
            Front = Top;
            Top = temp;
        }


        public void RotateEast()
        {
            var temp = Inverse(Right);
            Right = Top;
            Top = temp;
        }


        public void RotateWest()
        {
            var temp = Inverse(Top);
            Top = Right;
            Right = temp;
        }


        public enum Surface { TOP, BOTTOM, FRONT, BACK, RIGHT, LEFT, NONE }

        public Surface Match(int surface)
        {
            if (surface == Top) return Surface.TOP;
            if (surface == Bottom) return Surface.BOTTOM;
            if (surface == Front) return Surface.FRONT;
            if (surface == Back) return Surface.BACK;
            if (surface == Right) return Surface.RIGHT;
            if (surface == Left) return Surface.LEFT;

            return Surface.NONE;
        }


        public bool RotateToTop(int top)
        {
            switch (Match(top))
            {
                case Surface.TOP: break;
                case Surface.BOTTOM: RotateNorth(); RotateNorth(); break;
                case Surface.FRONT: RotateNorth(); break;
                case Surface.BACK: RotateSouth(); break;
                case Surface.RIGHT: RotateWest(); break;
                case Surface.LEFT: RotateEast(); break;
            }

            return Top == top;
        }


        public bool RotateToTopAndFront(int top, int front)
        {
            if (RotateToTop(top) == false)
            {
                return false;
            }

            switch (Match(front))
            {
                case Surface.RIGHT: RotateRightOfZCoords(); break;
                case Surface.BACK: RotateRightOfZCoords(); RotateRightOfZCoords(); break;
                case Surface.LEFT: RotateLeftOfZCoords();  break;
            }

            return Top == top && Front == front;
        }


        public void RotateRightOfZCoords()
        {
            var temp = Back;
            Front = Right;
            Right = temp;
        }


        public void RotateLeftOfZCoords()
        {
            var temp = Left;
            Right = Front;
            Front = temp;
        }



        public int RightOf(int front)
        {
            switch (Match(front))
            {
                case Surface.FRONT: return Right;
                case Surface.RIGHT: return Back;
                case Surface.BACK: return Left;
                case Surface.LEFT: return Front;
            }
            return -1;
        }


        public int Top { get; private set; }


        public int Front { get; private set; }


        public int Right { get; private set; }


        public int Bottom { get { return Inverse(Top); } }


        public int Back { get { return Inverse(Front); } }


        public int Left { get { return Inverse(Right); } }



        public int Inverse(int surface)
        {
            return 5 - surface;
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            var aliasOfSurface = Console.ReadLine().Split(' ');
            var dice = new Dice();

            var count = int.Parse(Console.ReadLine());
            for (var i = 0; i < count; ++i)
            {
                var q = Console.ReadLine().Split(' ');
                var top = -1;
                var front = -1;
                for (var h = 0; h < aliasOfSurface.Length; ++h) 
                {
                    if (q[0] == aliasOfSurface[h]) {
                        top = h;
                    }
                    if (q[1] == aliasOfSurface[h])
                    {
                        front = h;
                    }
                }

                if (dice.RotateToTopAndFront(top, front))
                {
                    Console.WriteLine(aliasOfSurface[dice.Right]);
                }
            }

        }
    }
}

Measured memory_bytes: 5264