Chef and the Cake II

All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian as well.
Your friend Chef has made a cake for you on your birthday. Before having the cake you two have made some cuts on the cake. If you consider the cake as a rectangular grid with coordinates from (0,0) to (n,n), then each of the cut can be considered as a rectilinear polygon (a polygon with sides parallel to the axis and possibly selfintersecting). These cuts turn the cake into several pieces. Now you two want to distribute the pieces among yourself. Since it is your birthday, your friend has given you a choice of choosing some pieces for yourself. You are hungry and you want to have the whole cake. But you don't want to make your friend suspicious. So instead of having all the pieces you want to select the set of pieces such that no pair of pieces have a common side. You want to maximize the total amount of cakes you can have. This amount can be measured as the sum of the areas of the top surface for each piece (or the amount of cake in each piece is the area of the polygon that define that piece).
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case starts with 2 integers n and m (the number of cuts). Each of the next m lines contains the description of a cuts. Each cut starts with an integer p denoting the number of points in the polygon, followed by p pairs of integers denoting the coordinates of the polygon.
Output
For each test case, output a single line containing an integer denoting the maximum amount of cake you can have.
Constraints
 1 ≤ T ≤ 50
 1 ≤ n ≤ 10000
 1 ≤ m ≤ 20
 The polygons described by each cut will be rectilinear and will possibly be self intersecting. Each nonempty segment on the plane belongs to the border of at most one polygon. Also, a single polygon cannot go through a single segment more than once. If any pair of polygons intersect, they will intersect only at a point (not a segment). It may happen that same pair of polygons intersect at multiple points.
 The points describing the polygonal cuts will be between 1 to n1 inclusive.
 For each of T test cases the sum of number of points in the polygons defined by each cut will be at most 80.
Example
Input: 2 5 1 4 1 1 1 4 4 4 4 1 5 2 4 1 1 1 3 3 3 3 1 4 2 2 2 4 4 4 4 2 Output: 16 19
Explanation
Example 1: You have only a single cut and the cake now have two pieces(as seen by the figure in the left). The amount of cake in piece 1 is 9 and the amount of cake in piece 2 is 16. So you will take the piece 2 and leave piece 1 for your friend. 

Example 2: You have two cuts and the cake now have four pieces(as seen by the figure in the left). The amount of cake in piece 1 is 18, the amount of cake in piece 2 is 3, the amount of cake in piece 3 is 1 and the amount of cake in piece 4 is 3. So you will take piece 1 and piece 3 for yourself and leave the piece 2 and piece 4 for your friend. 
Author:  satej 
Tester:  gerald 
Editorial  http://discuss.codechef.com/problems/CAKE2AM 
Tags  bipartite, cook42, geometry, maxindepset, maxflow, mediumhard, satej 
Date Added:  6012014 
Time Limit:  3 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 