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

struct Dise
{
    public int D1 { set; get; }
    public int D2 { set; get; }
    public int D3 { set; get; }
    public int D4 { set; get; }
    public int D5 { set; get; }
    public int D6 { set; get; }
    public char[] Order { set; get; }
}


class Program
{
    static void Rotate(Dise[] dise , char order)
    {
        int a, b;
        switch (order)
        {
            case 'N':
                a = dise[0].D1;
                b = dise[0].D6;
                dise[0].D1 = dise[0].D2;
                dise[0].D2 = b;
                dise[0].D6 = dise[0].D5;
                dise[0].D5 = a;
                break;
            case 'E':
                a = dise[0].D1;
                b = dise[0].D6;
                dise[0].D6 = dise[0].D3;
                dise[0].D1 = dise[0].D4;
                dise[0].D3 = a;
                dise[0].D4 = b;
                break;
            case 'S':
                a = dise[0].D1;
                dise[0].D1 = dise[0].D5;
                dise[0].D5 = dise[0].D6;
                dise[0].D6 = dise[0].D2;
                dise[0].D2 = a;
                break;
            case 'W':
                a = dise[0].D4;
                b = dise[0].D3;
                dise[0].D4 = dise[0].D1;
                dise[0].D3 = dise[0].D6;
                dise[0].D6 = a;
                dise[0].D1 = b;
                break;
        }
    }
    
    static bool Confir(Dise[] dise, int front, List<int> result)
    {
        if (dise[0].D2 == front)
        {
            result.Add(dise[0].D3);
            return true;
        }
        else if (dise[0].D3 == front)
        {
            result.Add(dise[0].D5);
            return true;
        }
        else if (dise[0].D5 == front)
        {
            result.Add(dise[0].D4);
            return true;
        }
        else if (dise[0].D4 == front)
        {
            result.Add(dise[0].D2);
            return true;
        }
        else
        {
            return false;
        }
    }

    static void Main(string[] args)
    {       
        var dise = new Dise[1];   //サイコロの情報を持Aつ変数       

        string[] input = Console.ReadLine().Split(' ');  //サイコロの目

        dise[0].D1 = int.Parse(input[0]);
        dise[0].D2 = int.Parse(input[1]);
        dise[0].D3 = int.Parse(input[2]);
        dise[0].D4 = int.Parse(input[3]);
        dise[0].D5 = int.Parse(input[4]);
        dise[0].D6 = int.Parse(input[5]);

        int n = int.Parse(Console.ReadLine());
        var result = new List<int>();
        bool flag;

        for(int i = 0; i < n; i++)
        {
            flag = false;
            string[] str = Console.ReadLine().Split(' ');
            int above = int.Parse(str[0]);
            int front = int.Parse(str[1]);

            for(int j = 0; j < 3; j++)
            {
                if (dise[0].D1 == above)
                {
                    flag = Confir(dise, front, result);
                    break;
                }
                Rotate(dise, 'N');              
            }

            if (flag)
            {
                continue;
            }
            
            for(int j = 0; j < 3; j++)
            {
                if (dise[0].D1 == above)
                {
                    Confir(dise, front, result);
                    break;
                }
                Rotate(dise, 'W');                
            }           
        }

        foreach (var s in result)
        {
            Console.WriteLine(s);
        }
    }
}


Measured memory_bytes: 17848