Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6

Solution in C# (judge verdict: Accepted)

using LIB;
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

class Program
{
    public class dice
    {
        public int n, e, w, s, t, b;
        public dice(int t, int s, int e, int w, int n, int b)
        {
            this.n = n;
            this.e = e;
            this.w = w;
            this.s = s;
            this.t = t;
            this.b = b;
        }
        public void roll(char r)
        {
            int buf;
            switch (r)
            {
                case 'N':
                    buf = t;
                    t = s;
                    s = b;
                    b = n;
                    n = buf;
                    break;

                case 'E':
                    buf = t;
                    t = w;
                    w = b;
                    b = e;
                    e = buf;
                    break;

                case 'W':
                    buf = t;
                    t = e;
                    e = b;
                    b = w;
                    w = buf;
                    break;

                case 'S':
                    buf = t;
                    t = n;
                    n = b;
                    b = s;
                    s = buf;
                    break;
            }
        }
    }
    static void Main(string[] args)
    {
        int[] d = io.r<int>(' ');
        int c = io.r<int>();
        int[][] m = io.r<int>(c, ' '); 
        dice dice = new dice(d[0], d[1], d[2], d[3], d[4], d[5]);

        for (int i = 0; i < c; i++)
        {
            while (true)
            {
                if (dice.t == m[i][0]) { break; }
                dice.roll('N');
                if (dice.t == m[i][0]) { break; }
                dice.roll('E');
            }
            if (dice.n == m[i][1]) { io.w(dice.w); }
            else if (dice.w == m[i][1]) { io.w(dice.s); }
            else if (dice.s == m[i][1]) { io.w(dice.e); }
            else if (dice.e == m[i][1]) { io.w(dice.n); }
        }
        io.wflush();
    }
}

namespace LIB
{
    public class io
    {
        private const int WMAX = 1000;
        private static StringBuilder S = new StringBuilder();
        public static T r<T>() { return util.parse<T>(r()); }
        public static T[] r<T>(char s = ' ') { return r().Split(s).Select(util.parse<T>).ToArray(); }
        public static T[] r<T>(int l) { T[] r = new T[l]; for (int i = 0; i < l; i++) { r[i] = r<T>(); } return r; }
        public static T[][] r<T>(int l, char s = ' ') { T[][] r = new T[l][]; for (int i = 0; i < l; i++) { r[i] = r<T>(s); } return r; }
        private static string r() { return Console.ReadLine(); }
        public static void w(object v, bool lf = true) { S.Append(util.parse<string>(v)); if (lf == true) { S.Append('\n'); } if (S.Length >= WMAX) { wflush(); } }
        public static void wflush() { Console.Write(S.ToString()); S.Length = 0; }
    }
    public class util
    {
        public static T parse<T>(object value) { return (T)(Convert.ChangeType(value, typeof(T))); }
    }
    public class memo<Key, Result>
    {
        private Dictionary<Key, Result> R;
        public memo() { R = new Dictionary<Key, Result>(); }
        public Result exec(Key k, Func<Key, Result> f) { Result r; if (R.ContainsKey(k)) { r = R[k]; } else { r = f(k); R.Add(k, r); } return r; }
    }
}