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 int RightOf(int front)
        {
            if (Front == front) return Right;
            if (Right == front) return Back;
            if (Back == front) return Left;
            if (Left == front) 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;
                    }
                }

                dice.RotateToTop(top);
                front = dice.RightOf(front);
                if (0 <= front)
                {
                    Console.WriteLine(aliasOfSurface[front]);
                }
            }

        }
    }
}

Measured memory_bytes: 5296