#include <iostream>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <string>
#include <stdio.h>
#include <sstream>

using namespace std;

class Dice
{
public:
	Dice() {};
	Dice(int s1, int s2, int s3, int s4, int s5, int s6)
	{
		Upper = s1;
		South = s2;
		East = s3;
		West = s4;
		North = s5;
		Down = s6;
	};
	Dice(int *v)
	{
		Upper = v[0];
		South = v[1];
		East = v[2];
		West = v[3];
		North = v[4];
		Down = v[5];
	};
	~Dice() {};
public:
	int Upper, Down;
	int South, North;
	int East, West;
public:
	void RolltoNorth();
	void RolltoSouth();
	void RolltoEast();
	void RolltoWest();
	void RollalongCW();
	void RollalongUCW();

public:
	int isMatched(int upper, int front);
	int getUpper() { return this->Upper; }
	int getEast() { return this->East; }
	int getSouth() { return this->South; }

public:
	void Disp() 
	{
		cout <<"U: " << this->Upper << " "
			<< "S: " << this->South << " "
			<< "E: " << this->East << " "
			<< "W: " << this->West << " "
			<< "N: " << this->North << " "
			<< "D: " << this->Down << endl;
	};
	void DispUpper()
	{
		cout << this->Upper << endl;
	};
};

void Dice::RolltoNorth()
{
	Dice a;
	a.Down = this->North;
	a.North = this->Upper;
	a.Upper = this->South;
	a.South = this->Down;
	a.East = this->East;
	a.West = this->West;
	*this = a;
}
void Dice::RolltoSouth()
{
	Dice a;
	a.Down = this->South;
	a.South = this->Upper;
	a.Upper = this->North;
	a.North = this->Down;
	a.East = this->East;
	a.West = this->West;
	*this = a;
}

void Dice::RolltoEast()
{
	Dice a;
	a.Down = this->East;
	a.East = this->Upper;
	a.Upper = this->West;
	a.West = this->Down;
	a.South = this->South;
	a.North = this->North;
	*this = a;
}

void Dice::RolltoWest()
{
	Dice a;
	a.Down = this->West;
	a.West = this->Upper;
	a.Upper = this->East;
	a.East = this->Down;
	a.South = this->South;
	a.North = this->North;
	*this = a;
}

void Dice::RollalongCW()
{
	Dice a;
	a.East = this->North;
	a.North = this->West;
	a.West = this->South;
	a.South = this->East;
	a.Upper = this->Upper;
	a.Down = this->Down;
	*this = a;
}

void Dice::RollalongUCW()
{
	Dice a;
	a.West = this->North;
	a.North = this->East;
	a.East = this->South;
	a.South = this->West;
	a.Upper = this->Upper;
	a.Down = this->Down;
	*this = a;
}

int Dice::isMatched(int upper, int front)
{
	int v[6];
	v[0] = this->Upper;
	v[1] = this->South;
	v[2] = this->East;
	v[3] = this->West;
	v[4] = this->North;
	v[5] = this->Down;

	int temp = -1;
	for (int i = 0; i < 6; ++i)
	{
		if (v[i] == upper)
			temp = i;
	}
	if (temp == -1)
		return -1;

	switch (temp)
	{
	case 2:
	case 3:
		while (true)
		{
			if (upper != this->getUpper())
				this->RolltoEast();
			else
				break;
		}
		break;
	case 0:
	case 1:
	case 4:
	case 5:
		while (true)
		{
			if (upper != this->getUpper())
				this->RolltoNorth();
			else
				break;
		}
		break;
	default:
		return -1;
	}

	temp = -1;
	for (int i = 0; i < 6; ++i)
	{
		if (v[i] == front)
			temp = i;
	}
	if (temp == -1)
		return -1;

	while (true)
	{
		if (front != this->getSouth())
			this->RollalongCW();
		else
			break;
	}

	return this->getEast();
}


int main()
{
	
	int v[6];
	for (int i = 0; i < 6; ++i)
		cin >> v[i];
	

	int numQ;
	cin >> numQ;

	for (int i = 0; i < numQ; ++i)
	{
		Dice minion(v);

		int upper, front;
		cin >> upper >> front;

		int result = minion.isMatched(upper, front);
		if (result < 0)
			return 0;
		
		cout << result << endl;

	}

	return 0;
}





Measured memory_bytes: 3140