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

class Dice
{
    public int[] Number { get; set; }

    private int[] tmp = new int[6];

    public void SetNumber(int[] a)
    {
        for (int i = 0; i < 6; i++)
        {
            Number[i] = a[i];
        }
    }

    public void Rotate(char c)
    {

        switch (c)
        {
            case 'N':
                tmp[0] = Number[1];
                tmp[1] = Number[5];
                tmp[2] = Number[2];
                tmp[3] = Number[3];
                tmp[4] = Number[0];
                tmp[5] = Number[4];
                SetNumber(tmp);
                break;
            case 'E':
                tmp[0] = Number[3];
                tmp[1] = Number[1];
                tmp[2] = Number[0];
                tmp[3] = Number[5];
                tmp[4] = Number[4];
                tmp[5] = Number[2];
                SetNumber(tmp);
                break;
            case 'W':
                tmp[0] = Number[2];
                tmp[1] = Number[1];
                tmp[2] = Number[5];
                tmp[3] = Number[0];
                tmp[4] = Number[4];
                tmp[5] = Number[3];
                SetNumber(tmp);
                break;
            case 'S':
                tmp[0] = Number[4];
                tmp[1] = Number[0];
                tmp[2] = Number[2];
                tmp[3] = Number[3];
                tmp[4] = Number[5];
                tmp[5] = Number[1];
                SetNumber(tmp);
                break;
            default:
                break;
        }
    }

    public void Spin()
    {
        tmp[0] = Number[0];
        tmp[1] = Number[2];
        tmp[2] = Number[4];
        tmp[3] = Number[1];
        tmp[4] = Number[3];
        tmp[5] = Number[5];
        SetNumber(tmp);
    }

    public void SetTop(int a)
    {
        if (a == Number[0])
        {

        }
        else if(a == Number[1])
        {
            Rotate('N');
        }
        else if (a == Number[2])
        {
            Rotate('W');
        }
        else if (a == Number[3])
        {
            Rotate('E');
        }
        else if (a == Number[4])
        {
            Rotate('S');
        }
        else if (a == Number[5])
        {
            Rotate('N');
            Rotate('N');
        }
    }

    public void SetFront(int a)
    {
        if (a == Number[0])
        {
            Rotate('S');
        }
        else if (a == Number[1])
        {
        }
        else if (a == Number[2])
        {
            Spin();
        }
        else if (a == Number[3])
        {
            Spin();
            Spin();
            Spin();
        }
        else if (a == Number[4])
        {
            Spin();
            Spin();
        }
        else if (a == Number[5])
        {
            Rotate('N');
        }
    }
}

class MyClass
{
    public static void Main(string[] args)
    {
        var dice = new Dice()
        { Number = Console.ReadLine().Split().Select(int.Parse).ToArray() };
        var q = int.Parse(Console.ReadLine());
        for (int i = 0; i < q; i++)
        {
            var data = Console.ReadLine().Split().Select(int.Parse).ToArray();
            dice.SetTop(data[0]);
            dice.SetFront(data[1]);
            Console.WriteLine(dice.Number[2]);
        }
    }
}


Measured memory_bytes: 18432