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

class Program
{
    public class Dice
    {
        public int[] Face {get; set;}
        public Dice Nua(char dir)
        {
            int[] _f = Face.Clone() as int[];
            var f = Face;
            Face = _f;
            switch (dir)
            {
                case 'N':
                {
                    Face[0] = f[1];
                    Face[1] = f[5];
                    Face[5] = f[4];
                    Face[4] = f[0];
                }
                break;
                case 'E':
                {
                    Face[2] = f[0];
                    Face[0] = f[3];
                    Face[3] = f[5];
                    Face[5] = f[2];
                }
                break;
                case 'W':
                {
                    Face[3] = f[0];
                    Face[0] = f[2];
                    Face[2] = f[5];
                    Face[5] = f[3];
                }
                break;
                case 'S':
                {
                    Face[1] = f[0];
                    Face[5] = f[1];
                    Face[4] = f[5];
                    Face[0] = f[4];
                }
                break;
            }
            _f = Face;
            Face = f;
            return new Dice { Face = _f};
        }
    }
    static void Main(string[] args)
    {
        var aid = new Dice { Face = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray()};
        var k = int.Parse(Console.ReadLine());
        for (int i = 0; i < k; i++)
        {
            var nuj = Console.ReadLine().Split(' ');
            var rad = int.Parse(nuj[0]);
            var yui = int.Parse(nuj[1]);
            var bf = false;
            //5^4 = 625
            foreach (var f0 in "_NSWE")
            {
                if (bf)
                    break;
                foreach (var f1 in "_NSWE")
                {
                    if (bf)
                        break;
                    foreach (var f2 in "_NSWE")
                    {
                        if (bf)
                            break;
                        foreach (var f3 in "_NSWE")
                        {
                            var rolled = aid.Nua(f0).Nua(f1).Nua(f2).Nua(f3);
                            if (rolled.Face[0] == rad && rolled.Face[1] == yui)
                            {
                                Console.WriteLine(rolled.Face[2]);
                                bf = true;
                                break;
                            }
                        }   
                    }
                }
            }
        }
    }
}
















































































































































































































































































































































































































































































































































































































































































































































































Measured memory_bytes: 9572