#include #include #include #include using namespace std; map, int>g; int t, r, c, w; pairp1[200020]; pairp2[200020]; set, pair > >shifengrixia; int f[400020]; int F(int x) { return f[x] != x ? f[x] = F(f[x]) : x; } int find(int x, int y, bool add = true) { if (x == 0 || y == 0 || x == r || y == c) { x = y = 0; } if (g.find(make_pair(x, y)) == g.end()) { if (add) { int t = g.size(); g[make_pair(x, y)] = t; } else { return -1; } } return g[make_pair(x, y)]; } int main() { scanf("%d", &t); for (int tt = 1; tt <= t; tt++) { // fprintf(stderr, "%d\n", tt); g.clear(); shifengrixia.clear(); scanf("%d%d%d", &r, &c, &w); for (int i = 0; i <= 2 * w; i++) { f[i] = i; } int ans = 2; for (int i = 1; i <= w; i++) { int p; scanf("%d%d", &p1[i].first, &p1[i].second); scanf("%d%d", &p2[i].first, &p2[i].second); if (p1[i].first == p2[i].first) { p1[i].first--; p2[i].second--; } else { p1[i].second--; p2[i].first--; } int f1 = F(find(p1[i].first, p1[i].second)); int f2 = F(find(p2[i].first, p2[i].second)); if (f1 != f2) { f[f1] = f2; } else { ans = 0; } shifengrixia.insert(make_pair(p1[i], p2[i])); shifengrixia.insert(make_pair(p2[i], p1[i])); } if (r == 1 || c == 1) { if (r == 1 && c == 1) { printf("Chefland is doomed\n"); } else { printf("%d\n", !w); } } else { if (ans != 0) { int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; for (__typeof(g.begin()) i = g.begin(); i != g.end(); i++) { if (i->first.first == 0 || i->first.first == r || i->first.second == 0 || i->first.second == c) { continue; } else { for (int k = 0; k < 4; k++) { int nx = i->first.first + dx[k]; int ny = i->first.second + dy[k]; if (0 <= nx && nx <= r && 0 <= ny && ny <= c && shifengrixia.find(make_pair(i->first, make_pair(nx, ny))) == shifengrixia.end()) { int id = find(nx, ny, false); if (id >= 0) { if (F(id) == F(i->second)) { // printf("%d %d %d %d\n", i->first.first, i->first.second, nx, ny); ans = 1; goto xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; } } } } } } } xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: printf("%d\n", ans); } } }