<?php
$str = trim(fgets(STDIN));
$d = new dice($str);
$n = trim(fgets(STDIN));

while($n)
{
    list($t, $f) = explode(" ",trim(fgets(STDIN)));
    $d->makeTop($t);
    $d->spinToMakeFront($f);
    echo $d->right();
    echo PHP_EOL;
    $n--;
}

class dice
{
    private $faces = [1,2,3,4,5,6,];
    private $view = [1,2,3,4,5,6,];

    public function __construct($str)
    {
        $this->faces = explode(" ",$str);
        $this->view = $this->faces;
    }
    
    public function rotate($str)
    {
        $direction = str_split($str);
        foreach($direction as $dir)
        {
            $this->turn($dir);
        }
    }

    private function turn($way)
    {
        switch($way)
        {
            case "N":
                $tmp = $this->view[4];
                $this->view[4] = $this->view[0];
                $this->view[0] = $this->view[1];
                $this->view[1] = $this->view[5];
                $this->view[5] = $tmp;
                break;
            case "E":
                $tmp = $this->view[2];
                $this->view[2] = $this->view[0];
                $this->view[0] = $this->view[3];
                $this->view[3] = $this->view[5];
                $this->view[5] = $tmp;
                break;
            case "S":
                $tmp = $this->view[4];
                $this->view[4] = $this->view[5];
                $this->view[5] = $this->view[1];
                $this->view[1] = $this->view[0];
                $this->view[0] = $tmp;
                break;
            case "W":
                $tmp = $this->view[2];
                $this->view[2] = $this->view[5];
                $this->view[5] = $this->view[3];
                $this->view[3] = $this->view[0];
                $this->view[0] = $tmp;
                break;
        }
    }
    private function clockwise($times)
    {
        while($times)
        {
            $tmp = $this->view[1];
            $this->view[1] = $this->view[2];
            $this->view[2] = $this->view[4];
            $this->view[4] = $this->view[3];
            $this->view[3] = $tmp;
            $times--;
        }
    }
    
    public function top()
    {
        return $this->view[0];
    }
    public function right()
    {
        return $this->view[2];
    }
    
    
    public function makeTop($num)
    {
        $curPos = $this->posOf($num);
        switch($curPos)
        {
            case 0:
                break;
            case 5:
                $this->turn("N");
            case 1:
                $this->turn("N");
                break;
            case 2:
                $this->turn("W");
                break;
            case 3:
                $this->turn("E");
                break;
            case 4:
                $this->turn("S");
                break;
        }
    }
    
    public function spinToMakeFront($num)
    {
        $curPos = $this->posOf($num);
        switch($curPos)
        {
            case 0:
            case 5:
                throw new Exception();
                break;
            case 1:
                break;
            case 2:
                $this->clockwise(1);
                break;
            case 3:
                $this->clockwise(3);
                break;
            case 4:
                $this->clockwise(2);
                break;
        }
    }

    private function posOf($num)
    {
        foreach($this->view as $key => $value)
        {
            if($num == $value) {return $key;}
        }
    }
}

Measured memory_bytes: 15476