/**
* rotation - 1: 시계, -1: 반시계
*/
void rotate(vector<int> &wheel, int rotation) {
// 시계 방향 회전
if(rotation == 1) {
int temp = wheel[7];
for(int i = 6; i >= 0; i--) {
wheel[i+1] = wheel[i];
}
wheel[0] = temp;
}
// 반시계 회전
else {
int temp = wheel[0];
for(int i = 0; i<=7; i++) {
wheel[i] = wheel[i+1];
}
wheel[7] = temp;
}
}
int findWheelNumRequiredRotation(vector<int> wheel[], string command, int startWheelNum) {
int resultWheelNum = startWheelNum;
if(command == "Left") {
while(resultWheelNum >= 0) {
// 왼쪽으로 바퀴 인덱스 -1씩 해가며 회전시키기
int prevNum = resultWheelNum - 1;
if(prevNum < 1) {
break;
}
// 2번과 6번이 다르면 왼쪽으로 진행
if(wheel[resultWheelNum][6] != wheel[prevNum][2]) {
resultWheelNum -= 1;
} else {
// 얘만 회전
break;
}
}
}
else if(command == "Right") {
while(resultWheelNum < 4) {
// 오른쪽으로 바퀴 인덱스 +1씩 해가며 회전시키기
int nextNum = resultWheelNum + 1;
if(nextNum > 4) {
break;
}
if(wheel[resultWheelNum][2] != wheel[nextNum][6]) {
resultWheelNum += 1;
} else {
break;
}
}
}
return resultWheelNum;
}
int main(int argc, const char * argv[]) {
vector<int> wheel[9];
// 톱니 4개 입력 받기
for(int i = 1; i<=4; i++) {
for(int j = 1; j<=8; j++) {
int temp;
scanf("%1d", &temp);
wheel[i].push_back(temp);
}
}
// 움직이는 횟수
int moveLength;
cin>>moveLength;
// 톱니 번호, 시계/반시계
for(int i = 0; i<moveLength; i++) {
int wNum, rotation;
cin>>wNum>>rotation;
int leftEndNum = findWheelNumRequiredRotation(wheel, "Left", wNum);
int rightEndNum = findWheelNumRequiredRotation(wheel, "Right", wNum);
// 왼쪽(본인 포함) 회전
int tempRotation = rotation;
for(int i = wNum; i>=leftEndNum; i--) {
rotate(wheel[i], tempRotation);
tempRotation = tempRotation == 1 ? -1 : 1;
}
tempRotation = rotation;
for(int i = wNum + 1; i<=rightEndNum; i++) {
tempRotation = tempRotation == 1 ? -1 : 1;
rotate(wheel[i], tempRotation);
}
// 오른쪽 (본인 제외) 회전
}
int result = 0;
int pow = 1;
for(int i = 1; i<=4; i++) {
result = result + (wheel[i][0]) * pow;
pow <<= 1;
}
cout<<result<<endl;
return 0;
}