File size: 1,818 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
****************************************************************
****************************************************************
-> Coded by Stavros Chryselis				   
-> Visit my github for more solved problems over multiple sites 
-> https://github.com/StavrosChryselis			  
-> Feel free to email me at stavrikios@gmail.com	
****************************************************************
****************************************************************
*/

#include <stdio.h>
#include <deque>
#include <string>
#include <ctype.h>
 
#define gc() getchar()
using namespace std;
 
inline void getstring(string &A)
{
	char c = gc();
 
	A.clear();
 
	while (isspace(c))
		c = gc();
 
	while (!isspace(c) && c != EOF)
	{
		A.push_back(c);
		c = gc();
	}
}
 
deque<int> Q;
bool rev;
 
inline void tofront(int num)
{
	if (rev)
		Q.push_back(num);
	else
		Q.push_front(num);
}
 
inline void toback(int num)
{
	if (rev)
		Q.push_front(num);
	else
		Q.push_back(num);
}
 
inline void front()
{
	if (Q.empty())
		printf("No job for Ada?\n");
	else
	{
		if (rev)
		{
			printf("%d\n", Q.back());
			Q.pop_back();
		}
 
		else
		{
			printf("%d\n", Q.front());
			Q.pop_front();
		}
	}
}
 
inline void back()
{
	if (Q.empty())
		printf("No job for Ada?\n");
	else
	{
		if (rev)
		{
			printf("%d\n", Q.front());
			Q.pop_front();
		}
 
		else
		{
			printf("%d\n", Q.back());
			Q.pop_back();
		}
	}
}
 
inline void command()
{
	string S;
	int num;
 
	getstring(S);
 
	if (S == "toFront")
	{
		scanf("%d", &num);
		tofront(num);
	}
 
	else if (S == "front")
		front();
 
	else if (S == "back")
		back();
 
	else if (S == "push_back")
	{
		scanf("%d", &num);
		toback(num);
	}
 
	else
		rev = !rev;
}
 
int main()
{
	int N;
 
//	freopen("input.txt", "r", stdin);
 
	scanf("%d", &N);
 
	while (N--)
		command();
 
	return 0;
}