#pragma region 라이브러리
#include <iostream>
#include <cmath>
#include <string>
#include <functional>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
#pragma endregion
#pragma region 최적화용
#define FAST_IO() ios::sync_with_stdio(0); cin.tie(0);
#define y first
#define x second
using IntPair = pair<int, int>;
#pragma endregion
int main()
{
deque<int> dq;
map<string, function<void(int)>> funcs
{
{"push_front", [&](int x) { dq.push_front(x); } },
{"push_back", [&](int x) { dq.push_back(x); } },
{"pop_front", [&](int x) { if (dq.empty()) cout << "-1\n"; else {
int val = dq.front();
dq.pop_front();
cout << val << '\n';
}}},
{"pop_back", [&](int x) { if (dq.empty()) cout << "-1\n"; else
{
int val = dq.back();
dq.pop_back();
cout << val << '\n';
}}},
{"size", [&](int x) { cout << dq.size() << '\n'; }},
{"empty", [&](int x) { cout << dq.empty() << '\n'; }},
{"front", [&](int x) { if (dq.empty()) cout << "-1\n"; else cout << dq.front() << '\n'; }},
{"back", [&](int x) { if (dq.empty()) cout << "-1\n"; else cout << dq.back() << '\n'; } },
};
int t;
cin >> t;
while (t--)
{
string str;
int n = 0;
cin >> str;
// 파라미터 넘겨야함
if (str.starts_with("push"))
cin >> n;
funcs[str](n);
}
}