#include <iostream>
#include <cstring>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
        string s, str;
        char c;
        stringstream ss;
        vector<string> dice, ans;
        int cnt, t_ix, f_ix;

        getline(cin, s);

        ss << s;
        while(getline(ss, str, ' ')) {
                dice.push_back(str);
        }

        cin >> cnt;
        string Din[cnt][2];

        for(int i = 0; i < cnt; i++) {
                for(int j = 0; j < 2; j++) {
                        cin >> Din[i][j];
                }

                for(int k = 0; k < 6; k++) {
                        if(Din[i][0] == dice[k]) {
                                t_ix = k;
                        } else if(Din[i][1] == dice[k]) {
                                f_ix = k;
                        }
                }

                if(t_ix == 0) {
                        if(f_ix == 1) {
                                ans.push_back(dice[2]);
                        } else if(f_ix == 2) {
                                ans.push_back(dice[4]);
                        } else if(f_ix == 4) {
                                ans.push_back(dice[3]);
                        } else if(f_ix == 3) {
                                ans.push_back(dice[1]);
                        }
                } else if(t_ix == 1) {
                        if(f_ix == 5) {
                                ans.push_back(dice[2]);
                        } else if(f_ix == 2) {
                                ans.push_back(dice[0]);
                        } else if(f_ix == 0) {
                                ans.push_back(dice[3]);
                        } else if(f_ix == 3) {
                                ans.push_back(dice[5]);
                        }
                } else if(t_ix == 2) {
                        if(f_ix == 5) {
                                ans.push_back(dice[4]);
                        } else if(f_ix == 4) {
                                ans.push_back(dice[0]);
                        } else if(f_ix == 0) {
                                ans.push_back(dice[1]);
                        } else if(f_ix == 1) {
                                ans.push_back(dice[5]);
                        }
                } else if(t_ix == 3) {
                        if(f_ix == 0) {
                                ans.push_back(dice[4]);
                        } else if(f_ix == 4) {
                                ans.push_back(dice[5]);
                        } else if(f_ix == 5) {
                                ans.push_back(dice[1]);
                        } else if(f_ix == 1) {
                                ans.push_back(dice[0]);
                        }
                } else if(t_ix == 4) {
                        if(f_ix == 0) {
                                ans.push_back(dice[2]);
                        } else if(f_ix == 2) {
                                ans.push_back(dice[5]);
                        } else if(f_ix == 5) {
                                ans.push_back(dice[3]);
                        } else if(f_ix == 3) {
                                ans.push_back(dice[0]);
                        }
                } else if(t_ix == 5) {
                        if(f_ix == 4) {
                                ans.push_back(dice[2]);
                        } else if(f_ix == 2) {
                                ans.push_back(dice[1]);
                        } else if(f_ix == 1) {
                                ans.push_back(dice[3]);
                        } else if(f_ix == 3) {
                                ans.push_back(dice[4]);
                        }
                }
        }

        for(vector<string>::iterator ite = ans.begin(); ite != ans.end(); ++ite) {
                cout << *ite << endl;
        }

        return 0;
}


Measured memory_bytes: 3176