<?php
$labels = fscanf(STDIN, '%d %d %d %d %d %d:w');
fscanf(STDIN, '%d', $n);

$dice = new Dice($labels);

for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%d %d", $top, $front);

    $dice->setDefault();
    echo $dice->moveTopTo($top)->rotateFrontTo($front)->right()."\n";
}

class Dice
{
    private $labels;

    private $top;
    private $bottom;
    private $front;
    private $back;
    private $left;
    private $right;

    public function __construct(array $labels=array(1,2,3,4,5,6))
    {
        $this->labels = $labels;
        $this->setDefault();
    }

    public function setDefault()
    {
        $this->top    = 1;
        $this->bottom = 6;
        $this->front  = 2;
        $this->back   = 5;
        $this->left   = 4;
        $this->right  = 3;
    }

    public function moveTopTo($top)
    {
        if ($this->top() == $top) return $this;

        $this->moveToDown();
        if ($this->top() == $top) return $this;

        $this->moveToDown();
        if ($this->top() == $top) return $this;

        $this->moveToDown();
        if ($this->top() == $top) return $this;

        $this->moveToLeft();
        if ($this->top() == $top) return $this;

        $this->moveToLeft();
        $this->moveToLeft();

        return $this;
    }

    public function rotateFrontTo($front)
    {
        if ($this->front() == $front) return $this;

        $this->rotateToRight();
        if ($this->front() == $front) return $this;

        $this->rotateToRight();
        if ($this->front() == $front) return $this;

        $this->rotateToRight();
        if ($this->front() == $front) return $this;

        $this->moveToDown();
        if ($this->front() == $front) return $this;

        $this->moveToDown();
        $this->moveToDown();
        return $this;
    }

    public function moveToDown()
    {
        $this->front  = $this->top;
        $this->top    = $this->back;
        $this->back   = 7 - $this->front;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function moveToUp()
    {
        $this->back   = $this->top;
        $this->top    = $this->front;
        $this->front  = 7 - $this->back;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function moveToRight()
    {
        $this->right  = $this->top;
        $this->top    = $this->left;
        $this->left   = 7 - $this->right;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function moveToLeft()
    {
        $this->left   = $this->top;
        $this->top    = $this->right;
        $this->right  = 7 - $this->left;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function rotateToRight()
    {
        $this->right = $this->back;
        $this->back  = $this->left;
        $this->left  = 7 - $this->right;
        $this->front = 7 - $this->back;

        return $this;
    }

    public function rotateToLeft()
    {
        $this->left  = $this->back;
        $this->back  = $this->right;
        $this->right = 7 - $this->left;
        $this->front = 7 - $this->back;

        return $this;
    }

    public function top()
    {
        return $this->label($this->top);
    }

    public function bottom()
    {
        return $this->label($this->bottom);
    }

    public function front()
    {
        return $this->label($this->front);
    }

    public function back()
    {
        return $this->label($this->back);
    }

    public function left()
    {
        return $this->label($this->left);
    }

    public function right()
    {
        return $this->label($this->right);
    }

    private function label($index)
    {
        return $this->labels[$index-1];
    }

    private function index($label)
    {
        foreach ($this->labels as $index => $val) {
            if ($val == $label) {
                return $key + 1;
            }
        }
    }
}

Measured memory_bytes: 6700