#include <bits/stdc++.h>
using namespace std;
using ll = long long;
deque<pair<int, int>> snake;
// 移动函数
bool moveSnake(int dir) {
if (snake.empty()) return false;
// 获取当前蛇头坐标
pair<int, int> head = snake.back();
int new_x = head.first;
int new_y = head.second;
// 根据方向计算新的蛇头坐标
if (dir == 1) { // 上
new_y++;
} else if (dir == 2) { // 下
new_y--;
} else if (dir == 3) { // 左
new_x--;
} else if (dir == 4) { // 右
new_x++;
}
// 创建新蛇头
pair<int, int> new_head = {new_x, new_y};
// 检查是否撞到身体
// 注意:移动时蛇尾会被移除,所以新蛇头可以和当前蛇尾重合
bool collide = false;
auto it = snake.begin();
it++; // 跳过蛇尾
for (; it != snake.end(); ++it) {
if (it->first == new_head.first && it->second == new_head.second) {
collide = true;
break;
}
}
if (collide) {
return true; // 撞到自己
}
// 移动蛇:移除蛇尾,添加新蛇头
snake.pop_front();
snake.push_back(new_head);
return false;
}
// 吃食物函数
bool eatSnake(int dir) {
if (snake.empty()) return false;
// 获取当前蛇头坐标
pair<int, int> head = snake.back();
int new_x = head.first;
int new_y = head.second;
// 根据方向计算新的蛇头坐标
if (dir == 1) { // 上
new_y++;
} else if (dir == 2) { // 下
new_y--;
} else if (dir == 3) { // 左
new_x--;
} else if (dir == 4) { // 右
new_x++;
}
// 创建新蛇头
pair<int, int> new_head = {new_x, new_y};
// 检查是否撞到身体
// 注意:吃食物时蛇尾不会移除,所以新蛇头不能和任何身体部分重合
for (const auto& part : snake) {
if (part.first == new_head.first && part.second == new_head.second) {
return true; // 撞到自己
}
}
// 吃食物:不移除蛇尾,直接添加新蛇头
snake.push_back(new_head);
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
// 读取初始蛇身坐标
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
snake.push_back({x, y}); // 队列末尾是蛇头
}
// 处理操作
for (int i = 0; i < q; i++) {
int op, dir;
cin >> op >> dir;
bool collide = false;
if (op == 1) {
collide = moveSnake(dir);
} else { // op == 2
collide = eatSnake(dir);
}
if (collide) {
cout << -1 << endl;
return 0;
} else {
// 从蛇头到蛇尾输出坐标
for (auto it = snake.rbegin(); it != snake.rend(); ++it) {
cout << it->first << " " << it->second << "\n";
}
}
}
return 0;
}