<?php

//統一性を持たすためにやり直した
namespace egg1to3;

class Dice
{
    protected $top;
    protected $front;
    protected $left;
    protected $right;
    protected $back;
    protected $bottom;
    protected $save;

    public function __construct($arr)
    {
        $this->save = $arr;
        $this->init();
    }

    public function init()
    {
        foreach ($this->save as $key => $val) {
            $this->{$key} = (int)$val;
        }
    }

    public function parse($str)
    {
        for ($i = 0, $len = strlen($str); $i < $len; $i++) {
            $direction = $str[$i];
            $this->move($direction);
        }
    }

    //指定された面を上面に移動させる
    public function positionChange($from)
    {
        switch ($from) {
            case 'top':
                break;
            case 'front':
                $this->move('N');
                break;
            case 'right':
                $this->move('W');
                break;
            case 'left':
                $this->move('E');
                break;
            case 'back':
                $this->move('S');
                break;
            case 'bottom':
                $this->parse('NN');
                break;
        }
    }

    //90度回転させる
    public function rotate($left = false)
    {
        $this->parse($left ? 'NES' : 'NWS');
    }

    //指定された面の値を取得
    public function getNumber($from)
    {
        return $this->{$from};
    }

    //面の値を配列にして返す
    public function getArray()
    {
        $arr = array();
        foreach ($this as $key => $val) {
            if (preg_match('/top|front|right|left|back|bottom/', $key)) {
                $arr[$key] = $val;
            }
        }
        return $arr;
    }

    //同じサイコロかチェック
    public function isEqual($dice)
    {
        $arr1 = $this->getArray();
        $arr2 = $dice->getArray();
        $diff = array_diff($arr1, $arr2);
        //片方にしか存在しない値がある
        if (count($diff)) {
            return false;
        }

        $keys = explode(',', 'top,front,right,left,back,bottom');
        foreach ($keys as $key) {
            //ある面を上面に移動する
            $dice->positionChange($key);
            $cnt = 0;
            //前面を同じ値にする(合わなければ一致しない)
            while ($this->front !== $dice->getNumber('front')) {
                //前面を同じ値にできなかった
                if ($cnt++ === 4) {
                    break;
                }
                $dice->rotate();
            }

            //初期化して次の面へ
            if ($cnt === 4) {
                $dice->init();
                continue;
            }

            $arr2 = $dice->getArray();
            $diff = array_diff_assoc($arr1, $arr2);
            //全ての面が一致すれば返り値は0
            if (count($diff) === 0) {
                return true;
            }

            $dice->init();
        }

        //一致しなかった
        return false;
    }

    protected function move($direction)
    {
        $top = $this->top;
        switch ($direction) {
            case 'S':
                $this->top = $this->back;
                $this->back = $this->bottom;
                $this->bottom = $this->front;
                $this->front = $top;
                break;
            case 'N':
                $this->top = $this->front;
                $this->front = $this->bottom;
                $this->bottom = $this->back;
                $this->back = $top;
                break;
            case 'E':
                $this->top = $this->left;
                $this->left = $this->bottom;
                $this->bottom = $this->right;
                $this->right = $top;
                break;
            case 'W':
                $this->top = $this->right;
                $this->right = $this->bottom;
                $this->bottom = $this->left;
                $this->left = $top;
                break;
            default:
                new Error('error');
        }
    }
}

$line = trim(fgets(STDIN));
$nums = explode(' ', $line);
$keys = explode(',', 'top,front,right,left,back,bottom');
$arr = array_combine($keys, $nums);
$dice = new Dice($arr);
fscanf(STDIN, '%d', $q);

for ($i = 0; $i < $q; $i++) {
    fscanf(STDIN, '%d %d', $top, $front);
    foreach ($keys as $key) {
        $dice->positionChange($key);
        //上面が一致しない
        if ($dice->getNumber('top') !== $top) {
            $dice->init();
            continue;
        }

        $cnt = 0;
        //前面を同じ値にする(合わなければ一致しない)
        while ($dice->getNumber('front') !== $front) {
            //前面を同じ値にできなかった
            if ($cnt++ === 4) {
                break;
            }
            $dice->rotate();
        }

        //4に達していない時は条件が満たされた時
        if ($cnt !== 4) {
            break;
        }
        $dice->init();
    }

    echo $dice->getNumber('right'), PHP_EOL;
    $dice->init();
}

Measured memory_bytes: 6744