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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//#define READ_NUM_TEST_CASES
//#define ASSERTS		// force asserts on online judge
//#define OLD_COMPILER

// (SOLUTION IS AT THE END OF THIS FILE)

////////////////////////// PROTOTYPING TEMPLATE /////////////////////////////////
//
// Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014
/*
    This program is licensed under GNU General Public License version 3.
    See <http://www.gnu.org/licenses/>.
*/
//
//

// enable c++ iostream; only if no fastio
//#define IOSTREAM

//#define NO_UNLOCKED	// fread_unlocked, fwrite_unlocked

#define mp make_pair
#define it iterator
#define be begin()
#define en end()
#define sz size()
#define fi first
#define se second
#define pub push_back
#define puf push_front
#define pob pop_back()
#define pof pop_front()
#define bs binary_search
#define lb lower_bound
#define ub upper_bound

#define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i)
#define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i)

#define fo2(i,n) fo3(i,0,n)
#define of2(i,n) of3(i,0,n)

#define fo(...) VA_SEL(fo,__VA_ARGS__)
#define of(...) VA_SEL(of,__VA_ARGS__)

#define inl		inline __attribute__ ((always_inline))
#define ninl	__attribute__ ((noinline))



#ifdef OLD_COMPILER
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<string>
#include<stdint.h>
#include<cstring>
#include<iostream>
#else
#include<bits/stdc++.h>
#endif





#ifdef OLD_COMPILER

#else
#include<cstdint>
#endif

#include<utility>

namespace ab{

typedef int8_t i8;
typedef uint8_t ui8;
typedef int16_t i16;
typedef uint16_t ui16;
typedef int32_t i32;
typedef uint32_t ui32;
typedef int64_t i64;
typedef uint64_t ui64;


// old:
typedef i64 ll;
typedef ui64 ull;

typedef std::pair<i32,i32> pii;

typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;

struct VOID {};


template<typename I>struct FloatFrom{typedef VOID R;};
template<>struct FloatFrom<i32>{typedef double R;};
template<>struct FloatFrom<ui32>{typedef double R;};
template<>struct FloatFrom<i64>{typedef long double R;};
template<>struct FloatFrom<ui64>{typedef long double R;};
template<>struct FloatFrom<i16>{typedef float R;};
template<>struct FloatFrom<ui16>{typedef float R;};
template<>struct FloatFrom<i8>{typedef float R;};
template<>struct FloatFrom<ui8>{typedef float R;};
template<>struct FloatFrom<float>{typedef float R;};
template<>struct FloatFrom<double>{typedef double R;};
template<>struct FloatFrom<long double>{typedef long double R;};

template<typename I>struct Double{typedef VOID R;};
template<>struct Double<i8>{typedef i16 R;};
template<>struct Double<ui8>{typedef ui16 R;};
template<>struct Double<i16>{typedef i32 R;};
template<>struct Double<ui16>{typedef ui32 R;};
template<>struct Double<i32>{typedef i64 R;};
template<>struct Double<ui32>{typedef ui64 R;};

template<>struct Double<float>{typedef float R;};
template<>struct Double<double>{typedef double R;};
template<>struct Double<long double>{typedef long double R;};


#if defined __GNUC__ and ( __WORDSIZE == 64 )
typedef int128_t i128;
typedef uint128_t ui128;
template<>struct Double<i64>{typedef i128 R;};
template<>struct Double<ui64>{typedef ui128 R;};
#endif


}

#define FASTIO

#ifdef NO_FASTIO
#undef FASTIO
#endif

#ifdef DEBUG
#undef NDEBUG
#pragma message "Compiling in DEBUG mode."
#undef FASTIO

#ifdef _WIN32
#include<windows.h>
inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);}
inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);}
#else
// *nix includes for debug
#endif

#else
#define NDEBUG
#define ec(x)
#define oc(x)
#endif



#include <cstdio>
#include <cfloat>



#ifdef OLD_COMPILER
// TODO
#else
#include<type_traits>
#endif


#define _CAT(a,b) a##b
#define CAT(a,b) _CAT(a,b)
#define _10TH(a,b,c,d,e,f,g,h,i,CNT,...) CNT
#define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1)
#define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__)

#define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0)
#define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__)

#define foreach(e,c) for(__typeof((c).begin())e=(c).begin();e!=(c).end();++e)


#define MIN_GCC(a,b,c) (__GNUC__ > (a) || \
              (__GNUC__ == (a) && (__GNUC_MINOR__ > (b) || \
                                 (__GNUC_MINOR__ == (b) && \
                                  __GNUC_PATCHLEVEL__ >= (c)))))
								  

#define  L0(str) 			fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__)
#define  L1(str,...)		fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__)

#define L(...) VA_GTONE(L,__VA_ARGS__)

#ifdef NDEBUG

#define D if(0)
#define A(x)
#define TEST_FLOAT(x)
#define ASSERT_FLOAT(x)

#else

#define A(x) assert(x);

#define _TEST_FLOAT(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX)
#define ASSERT_FLOAT(f) A(_TEST_FLOAT(f))

#define D

#endif

#define V(x) (E<<#x<<": "<<(x)<<N)
#define XX {E<<"XX LINE "<<__LINE__<<N;}

#if defined DEBUG or defined ASSERTS
#undef NDEBUG
#else
#define NDEBUG
#endif

#ifdef _WIN32

// TODO
#ifndef NO_UNLOCKED
#define NO_UNLOCKED
#endif

//#define fread_unlocked _fread_nolock
//#define fwrite_unlocked _fwrite_nolock

#endif

#define all(x) (x).be,(x).en
#define type(x) __typeof(x)

const char N='\n';
const char S=' ';

using namespace std;
using namespace __gnu_cxx;

namespace ab{}
using namespace ab;



template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));}

namespace std{




#if defined __GNUC__ and not MIN_GCC(4,2,0)
template<class T>struct hash<T>{}
#endif

// 1,2,last (!!!!!)
template<>template<class A,class B>struct hash<map<A,B>>{
    size_t operator()(const map<A,B>& m) const{
        size_t r = m.size();
        if(!r)return r;
        auto i=m.be;
        r^=hash<type(i)>()(i);
        ++i;
        r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/4);
        i=m.end();
        --i;
        r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/2);
        return r;
    }
};
template<>template<class A, class B>struct hash<pair<A,B>>{
    size_t operator()(const pair<A,B>& p) const{
        return hash<A>()(p.first) ^ cbitl(hash<B>()(p.second),sizeof(size_t)*8/2);
    }
};
}

#define rint _rint()
#define ruint _ruint()
#define rll _rll()
#define rull _rull()
#define rchar _rchar()

#ifndef FASTIO
inline void fin(int){}inline void fout(int){}
#ifdef IOSTREAM
inline int _rint(){int r;cin>>r;return r;}
inline uint _ruint(){uint r;cin>>r;return r;}
inline ull _rull(){ull r;cin>>r;return r;}
inline char _rchar(){return cin.get();}
template<typename T>inline void W(const T& i){cout<<i;}
inline void W(){W('\n');}
#else
inline int _rint(){int r;scanf("%d",&r);return r;}
inline uint _ruint(){uint r;scanf("%u",&r);return r;}
inline ull _rull(){ull r;scanf("%llu",&r);return r;}
inline ull _rll(){ll r;scanf("%lld",&r);return r;}
inline char _rchar(){return getchar();}
inline void W(char a='\n'){printf("%c",a);}
inline void W(int a){printf("%d",a);}
inline void W(uint a){printf("%u",a);}
inline void W(const ull& a){printf("%llu",a);}
inline void W(const ll& a){printf("%lld",a);}
inline void W(const char *str){fputs(str,stdout);}
inline void W(const double& d){printf("%.20f",d);}
inline void W(const long double& d){printf("%.50Lf",d);}
#endif
#else

#ifndef NO_UNLOCKED
#define FREAD fread_unlocked
#define FWRITE fwrite_unlocked
#else
#define FREAD fread
#define FWRITE fwrite
#endif

// todo: test on pointers
const int _IB=32768;const int _OB=32768;
char _iB[_IB];int _iP = _IB;char _oB[_OB];int _oP = 0;
inline void fin(int mc){int r=_IB-_iP;if(r>mc)return;fo(i,r)_iB[i]=_iB[i+_iP];	// TODO: memcpy
    FREAD(_iB+r,1,_iP,stdin);_iP = 0;}
inline void fout(int mc){if(_OB-_oP>mc)return;FWRITE(_oB,1,_oP,stdout);_oP = 0;}
inline void W(char c='\n'){_oB[_oP++]=c;}
inline char _rchar(){return _iB[_iP++];}
template<class T>inline T _rur(){
    T r=0;char c=rchar;
    do c-='0',r=r*10+c,c=rchar;while(c>='0');
    return r;}
template<class T>inline T _rsi(){
	char c=rchar;
	while(c<'-')c=rchar;
	if(c=='-')return -_rur<T>();
	else{--_iP;return _rur<T>();}}
template<class T>inline T _ru(){while(rchar<'0');--_iP;return _rur<T>();}

inline int _rint(){fin(15);return _rsi<int>();}
inline uint _ruint(){fin(15);return _ru<uint>();}
inline ll _rll(){fin(25);return _rsi<ll>();}
inline ull _rull(){fin(25);return _ru<ull>();}

const int _rs = 20;char _r[_rs];
template<class T>inline void _w(T i){
	if(numeric_limits<T>::is_signed && i<0)W('-'),i=-i;
	else if(i==0){W('0');return;}
	int pos = _rs;
	do{_r[--pos]='0'+i%10;i/=10;}while(i);
	do W(_r[pos]),++pos;while(pos<_rs);}
inline void W(const int32_t& i){fout(15);_w(i);}
inline void W(const uint32_t& i){fout(15);_w(i);}
inline void W(const int64_t& i){fout(25);_w(i);}
inline void W(const uint64_t& i){fout(25);_w(i);}
inline void W(const char*str){int l=strlen(str);fout(l);memcpy(&_oB[_oP],str,l);_oP+=l;}
inline void W(double d){fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);}	// todo
#endif
template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);}
template<class A,class B,class C>
inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);}
template<class A,class B,class C,class DD>
inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);}
template<class T>inline void sortall(T&t){sort(t.be,t.en);}
template<class T>inline void uniqall(T&t){t.resize(unique(t.be,t.en)-t.be);}

inline int count1(uint i){return __builtin_popcount(i);}
inline int count1(ull i){return __builtin_popcountll(i);}
inline int parity(uint i){return __builtin_parity(i);}
inline int parity(ull i){return __builtin_parityll(i);}
inline int ilog(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i))
inline int ilog(ull i){return 63-__builtin_clzll(i);}
#define powerof2(x) !(x&(x-1))
#define subset(a,b) (a&b==a)
#define kbegin(k) ((1<<k)-1)
#define kend(k,n) ((kbegin(k)<<n-k)+1)
inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));}
#define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x))


template<class A,class B>inline ostream&operator<<(ostream&o,const pair<A,B>&p){o<<'<'<<p.fi<<", "<<p.se<<'>';return o;}
template<class A,class B>inline ostream&operator<<(ostream&o,const map<A,B>&m){o<<N;for(auto&k:m)o<<"map["<<k.fi<<"] -> "<<k.se<<N;return o;}
template<class A>inline ostream&operator<<(ostream&o,const vector<A>&_v){o<<N;fo(i,_v.sz)o<<"vector["<<i<<"] = "<<_v[i]<<N;return o;}

struct _O{template<class T>_O&operator<<(const T&t){oc(1);W(t);oc(0);return*this;}}O;
struct _I{
	_I&operator>>(int32_t&t){t=rint;return*this;}
	_I&operator>>(uint32_t&t){t=ruint;return*this;}
	_I&operator>>(int64_t&t){t=rll;return*this;}
	_I&operator>>(uint64_t&t){t=rull;return*this;}
}I;

struct _E{template<class T>_E&operator<<(const T&t){D{ec(1);cerr<<t;ec(0);}return*this;}}E;



inline void tc();
int main()
{
#ifdef FASTIO
    setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0);
#elif defined IOSTREAM
	ios_base::sync_with_stdio(0);cin.tie(0);cout.precision(99);cout.setf(ios::fixed,ios::floatfield);
#endif

#ifdef DEBUG
#ifdef _WIN32
	ec(0);
	oc(0);
#else
// TODO: *nix
#endif
#endif
	
#ifdef READ_NUM_TEST_CASES
	int nz=rint;fo(i,nz)
#endif
	tc();
#ifdef FASTIO
    FWRITE(_oB,1,_oP,stdout);
#endif
	return 0;
}
// END OF TEMPLATE







void merge(int child, int parent);

class UnionFind
{
public:
	UnionFind(size_t univSize) : parents(univSize), ranks(univSize,0)
	{
		for(size_t i=0; i<univSize; ++i) parents[i] = i;
	}
	size_t find(size_t x)
	{
		size_t root = x;
		while(parents[root] != root) root = parents[root];
		while(x != root)
		{
			size_t& curr = parents[x];
			x = parents[x];
			curr = root;
		}
		return root;
	}
	bool makeUnion(size_t a, size_t b) // false if union already made
	{
		a = find(a);
		b = find(b);
		if(a==b)return false;
		if(ranks[a] < ranks[b]){
			parents[a] = b, ++ranks[b];
			merge(a,b);
		}
		else{
			parents[b] = a, ++ranks[a];
			merge(b,a);
		}
		return true;
	}
private:
	std::vector<size_t> parents;
	std::vector<size_t> ranks;
} uf(100009);




struct point{
	bool operator==(const point&o)const{
		return x==o.x && y==o.y;
	}
	int x,y;
};

// xy,xy
typedef pair<point,point> rect;

struct plem{
	rect r;
	plem* to[8];
};

vector<plem> plems;

inline void rotate(point&p){
	int t = p.y;
	p.y = p.x;
	p.x = -t;
}

inline void rotate(rect&r){
	swap(r.fi.y,r.se.y);
	rotate(r.fi);
	rotate(r.se);
}

inline void rotate(plem&p){
	rotate(p.r);
	
	// cycle
	auto a = p.to[6];
	auto b = p.to[7];
	of(i,8-2){
		p.to[i+2] = p.to[i];
	}
	p.to[0] = a;
	p.to[1] = b;
}

// left
void rotate(){
	fo(i,plems.sz)rotate(plems[i]);
}

struct node{
	bool operator<(const node&o)const{
		return make_pair(xfirst,what) < make_pair(o.xfirst,o.what);
	}
	pii xfirst;
	plem* what;
};

inline void link(plem&e, set<node>&ys){
	auto it = ys.ub(node{pii(e.r.fi.y,0),0});
	if(it != ys.be)e.to[5] = (--it)->what;
	
	// se
	it = ys.lb(node{pii(e.r.se.y,1),0});
	if(it != ys.en)e.to[2] = it->what;
}

int dbgrefine = 0;
inline bool intersect(rect&a, rect&b){
	bool result = b.fi.x < a.se.x && a.fi.x < b.se.x &&
			b.fi.y < a.se.y && a.fi.y < b.se.y;
			
	if(dbgrefine && result){
		bool ainb = b.fi.x <= a.fi.x && a.se.x <= b.se.x &&
			b.fi.y <= a.fi.y && a.se.y <= b.se.y;
		swap(a,b);
		bool bina = b.fi.x <= a.fi.x && a.se.x <= b.se.x &&
			b.fi.y <= a.fi.y && a.se.y <= b.se.y;
		swap(a,b);
		if(ainb || bina);
		else{
			E<<ainb<<N;
			E<<bina<<N;
			E << a.fi.x <<","<< a.fi.y<<" -> "<<a.se.x<<","<<a.se.y<<N;
			E << b.fi.x <<","<< b.fi.y<<" -> "<<b.se.x<<","<<b.se.y<<N;
			E<<N;
			A(false);
		}
	}
	
	return result;
}

inline rect rectmerge(rect&a,rect&b){
	return 	rect(point{min(a.fi.x,b.fi.x),min(a.fi.y,b.fi.y)},
			point{max(a.se.x,b.se.x),max(a.se.y,b.se.y)});
}

inline void merge(int child, int parent){
	//E<<"uf merge "<<child<<" "<<parent<<N;
	
	auto&pp = plems[parent];
	auto&pc = plems[child];
	
	if(!intersect(pp.r,pc.r)){
		E << pp.r.fi.x <<","<< pp.r.fi.y<<" -> "<<pp.r.se.x<<","<<pp.r.se.y<<N;
		E << pc.r.fi.x <<","<< pc.r.fi.y<<" -> "<<pc.r.se.x<<","<<pc.r.se.y<<N;
		A(0);
	}
	
	if(pii(pc.r.se.y,  pc.r.se.x) > pii(pp.r.se.y,  pp.r.se.x)) pp.to[0] = pc.to[0];
	if(pii(pc.r.se.y, -pc.r.fi.x) > pii(pp.r.se.y, -pp.r.fi.x)) pp.to[3] = pc.to[3];
	
	if(pii(-pc.r.fi.y,  pc.r.se.x) > pii(-pp.r.fi.y,  pp.r.se.x)) pp.to[7] = pc.to[7];
	if(pii(-pc.r.fi.y, -pc.r.fi.x) > pii(-pp.r.fi.y, -pp.r.fi.x)) pp.to[4] = pc.to[4];
	
	if(pii(pc.r.se.x,  pc.r.se.y) > pii(pp.r.se.x,  pp.r.se.y)) pp.to[1] = pc.to[1];
	if(pii(pc.r.se.x, -pc.r.fi.y) > pii(pp.r.se.x, -pp.r.fi.y)) pp.to[6] = pc.to[6];
	
	if(pii(-pc.r.fi.x,  pc.r.se.y) > pii(-pp.r.fi.x,  pp.r.se.y)) pp.to[2] = pc.to[2];
	if(pii(-pc.r.fi.x, -pc.r.fi.y) > pii(-pp.r.fi.x, -pp.r.fi.y)) pp.to[5] = pc.to[5];
	
	D if(dbgrefine){
		A(rectmerge(pp.r, pc.r)==pp.r || rectmerge(pp.r, pc.r)==pc.r);
	}
	
	pp.r = rectmerge(pp.r, pc.r);
}

inline void mergeall(int _i){
	int i = uf.find(_i);
	
	unordered_set<int> q;
	fo(k,8)if(plems[i].to[k]){
		int j = uf.find(plems[i].to[k] - &*plems.be);
		if(j != i) q.insert(j);
	}
	
	//E<<"merge "<<_i<<" -> "<<i<<N;
	
	while(!q.empty()){
		int _dest = *q.begin();
		q.erase(_dest);
		
		int dest = uf.find(_dest);
		if(i==dest)continue;
		
		if(intersect(plems[i].r, plems[dest].r)){
			fo(k,8)if(plems[i].to[k]){
				int j = uf.find(plems[i].to[k] - &*plems.be);
				if(j != i)q.insert(j);
			}
			fo(k,8)if(plems[dest].to[k]){
				int j = uf.find(plems[dest].to[k] - &*plems.be);
				if(j != i)q.insert(j);
			}
			//E<<"intersect"<<N;
			uf.makeUnion(i,dest);
			i = uf.find(i);
		}
	}
}

bool vis[100009];

void refine()
{
	vector<int> _ns;
	fo(_i,plems.sz){
		int i = uf.find(_i);
		_ns.pub(i);
	}
	sortall(_ns);
	uniqall(_ns);
	
	vector<node> ns;
	for(auto&i:_ns)ns.pub(node{pii(plems[i].r.fi.x,1),&plems[i]});
	sortall(ns);
	
	set<node> ys;
	
	fo(i,ns.sz){
		auto&e=ns[i];
		//if(e.xfirst.se){
			auto _it0 = ys.insert(node{pii(e.what->r.fi.y,1),e.what});
			auto _it1 = ys.insert(node{pii(e.what->r.se.y,0),e.what});
			A(_it0.se);
			A(_it1.se);
			auto it0 = _it0.fi;
			auto it1 = _it1.fi;
			// pomiedzy
			int maxx = -INT_MAX;
			//int dbgpom = 0;
			for(;;){
				auto t = it0;
				++t;
				if(t==it1)break;
				if(t->what->r.se.x > e.what->r.fi.x){
					A(intersect(e.what->r,t->what->r));
					maxx = max(maxx,t->what->r.se.x);
					uf.makeUnion(e.what-&*plems.be, t->what-&*plems.be);
				}
				else A(!intersect(e.what->r,t->what->r));
				ys.erase(t);
				//++dbgpom;
			}
			if(it0 != ys.be){
				auto t = it0;
				--t;
				if(t->xfirst.se == it0->xfirst.se){
					if(intersect(e.what->r,t->what->r)){
						A(intersect(e.what->r,t->what->r));
						maxx = max(maxx,t->what->r.se.x);
						uf.makeUnion(e.what-&*plems.be, t->what-&*plems.be);
						ys.erase(it0);
					}
					else{
						A(!intersect(e.what->r,t->what->r));
						ys.erase(t);
					}
				}
			}
			auto t = it1;
			if(++t != ys.en){
				if(t->xfirst.se == it1->xfirst.se){
					if(intersect(e.what->r,t->what->r)){
						A(intersect(e.what->r,t->what->r));
						maxx = max(maxx,t->what->r.se.x);
						uf.makeUnion(e.what-&*plems.be, t->what-&*plems.be);
						ys.erase(it1);
					}
					else{
						A(!intersect(e.what->r,t->what->r));
						ys.erase(t);
					}
				}
			}
			//e.what = &plems[uf.find(e.what-&*plems.be)];
			//e.what->r.se.x = max(e.what->r.se.x, maxx);
			
			D{
				bool cfi = true;
				for(auto&ee:ys){
					if(cfi != ee.xfirst.se)A(false);
					cfi = !cfi;
				}
			}
		//}
	}
}

inline void tc()
{
	int n;
	I>>n;
	
	plems.resize(n);
	fo(i,n){
		I >> plems[i].r.fi.x >> plems[i].r.se.x >> plems[i].r.fi.y >> plems[i].r.se.y;
	}
	
	fo(dir,4){
		//E<<"dir "<<dir<<N;
		vector<node> ns;
		for(auto&e:plems)ns.pub(node{pii(e.r.fi.x,1),&e});
		for(auto&e:plems)ns.pub(node{pii(e.r.se.x,0),&e});
		sort(all(ns));
		
		set<node> ys;
		
		fo(i,ns.sz){
			auto&e=ns[i];
			if(e.xfirst.se){
				link(*e.what,ys);
				ys.insert(node{pii(e.what->r.fi.y,1),e.what});
				ys.insert(node{pii(e.what->r.se.y,0),e.what});
			}
			else{
				ys.erase(node{pii(e.what->r.fi.y,1),e.what});
				ys.erase(node{pii(e.what->r.se.y,0),e.what});
			}
		}
		rotate();
	}
	
	refine();
	
	fo(i,plems.sz){
		mergeall(i);
	}
	
	E<<N<<"REFINE"<<N<<N;
	dbgrefine = 1;
	
	// fix
	//if(false)
	//fo(xxx,200)
	refine();
	//
	
	vector<pair<pii,pii>> rr;
	
	fo(_i,plems.sz){
		int i = uf.find(_i);
		if(vis[i])continue;
		vis[i]=1;
		
		auto&e = plems[i].r;
		D{
			rect re;
			re.fi.x = 713370;
			re.se.x = 715126;
			re.fi.y = 27085;
			re.se.y = 31855;
			if(intersect(re,e)){
				E << "UPS " << e.fi.x <<","<< e.fi.y<<" -> "<<e.se.x<<","<<e.se.y<<N;
			}
		}
		rr.pub(mp(mp(e.fi.x,e.se.x),mp(e.fi.y,e.se.y)));
	}
	sort(all(rr));
	O<<rr.sz<<N;
	for(auto&e:rr) O << e.fi.fi<<S<<e.fi.se<<S<<e.se.fi<<S<<e.se.se<<N;
}