var l={Identity:function(t){return t},True:function(){return!0},Blank:function(){}},m={Boolean:"boolean",Number:"number",String:"string",Object:typeof{},Undefined:"undefined",Function:"function"},L={"":l.Identity},o={createLambda:function(t){if(t==null)return l.Identity;if(typeof t===m.String){let n=L[t];if(n!=null)return n;if(t.indexOf("=>")===-1){const e=new RegExp("[$]+","g");let r=0,i;for(;(i=e.exec(t))!=null;)i[0].length>r&&(r=i[0].length);const a=[];for(let c=1;c<=r;c++){let p="";for(let h=0;h(.*)/);return n=new Function(e[1],e[2].match(/\breturn\b/)?e[2]:"return "+e[2]),L[t]=n,n}}return t},defineProperty:function(t,n,e){Object.defineProperty(t,n,{enumerable:!1,configurable:!0,writable:!0,value:e})},compare:function(t,n){return t===n?0:t>n?1:-1},dispose:function(t){t!=null&&t.dispose()},hasNativeIteratorSupport:function(){return typeof Symbol<"u"&&typeof Symbol.iterator<"u"}},w={Before:0,Running:1,After:2},s=function(t,n,e){var r=new B,i=w.Before;this.current=r.current,this.moveNext=function(){try{switch(i){case w.Before:i=w.Running,t();case w.Running:return n.apply(r)?!0:(this.dispose(),!1);case w.After:return!1}}catch(a){throw this.dispose(),a}},this.dispose=function(){if(i==w.Running)try{e()}finally{i=w.After}}},B=function(){var t=null;this.current=function(){return t},this.yieldReturn=function(n){return t=n,!0},this.yieldBreak=function(){return!1}},u=function(t){this.getEnumerator=t};u.Utils={};u.Utils.createLambda=function(t){return o.createLambda(t)};u.Utils.createEnumerable=function(t){return new u(t)};u.Utils.createEnumerator=function(t,n,e){return new s(t,n,e)};u.Utils.extendTo=function(t){var n=t.prototype,e;t===Array?(e=d.prototype,o.defineProperty(n,"getSource",function(){return this})):(e=u.prototype,o.defineProperty(n,"getEnumerator",function(){return u.from(this).getEnumerator()}));for(let r in e){const i=e[r];n[r]!=i&&(n[r]!=null&&(r=r+"ByLinq",n[r]==i)||i instanceof Function&&o.defineProperty(n,r,i))}};u.Utils.recallFrom=function(t){var n=t.prototype,e;t===Array?(e=d.prototype,delete n.getSource):(e=u.prototype,delete n.getEnumerator);for(const r in e){const i=e[r];n[r+"ByLinq"]?delete n[r+"ByLinq"]:n[r]==i&&i instanceof Function&&delete n[r]}};u.choice=function(){var t=arguments;return new u(function(){return new s(function(){t=t[0]instanceof Array?t[0]:t[0].getEnumerator!=null?t[0].toArray():t},function(){return this.yieldReturn(t[Math.floor(Math.random()*t.length)])},l.Blank)})};u.cycle=function(){var t=arguments;return new u(function(){var n=0;return new s(function(){t=t[0]instanceof Array?t[0]:t[0].getEnumerator!=null?t[0].toArray():t},function(){return n>=t.length&&(n=0),this.yieldReturn(t[n++])},l.Blank)})};u.empty=function(){return new u(function(){return new s(l.Blank,function(){return!1},l.Blank)})};u.from=function(t){if(t==null)return u.empty();if(t instanceof u)return t;if(typeof t==m.Number||typeof t==m.Boolean)return u.repeat(t,1);if(typeof t==m.String)return new u(function(){var n=0;return new s(l.Blank,function(){return n=n?this.yieldReturn(i):this.yieldBreak()},l.Blank)})};u.repeat=function(t,n){return n!=null?u.repeat(t).take(n):new u(function(){return new s(l.Blank,function(){return this.yieldReturn(t)},l.Blank)})};u.repeatWithFinalize=function(t,n){return t=o.createLambda(t),n=o.createLambda(n),new u(function(){var e;return new s(function(){e=t()},function(){return this.yieldReturn(e)},function(){e!=null&&(n(e),e=null)})})};u.generate=function(t,n){return n!=null?u.generate(t).take(n):(t=o.createLambda(t),new u(function(){return new s(l.Blank,function(){return this.yieldReturn(t())},l.Blank)}))};u.toInfinity=function(t,n){return t==null&&(t=0),n==null&&(n=1),new u(function(){var e;return new s(function(){e=t-n},function(){return this.yieldReturn(e+=n)},l.Blank)})};u.toNegativeInfinity=function(t,n){return t==null&&(t=0),n==null&&(n=1),new u(function(){var e;return new s(function(){e=t+n},function(){return this.yieldReturn(e-=n)},l.Blank)})};u.unfold=function(t,n){return n=o.createLambda(n),new u(function(){var e=!0,r;return new s(l.Blank,function(){return e?(e=!1,r=t,this.yieldReturn(r)):(r=n(r),this.yieldReturn(r))},l.Blank)})};u.defer=function(t){return new u(function(){var n;return new s(function(){n=u.from(t()).getEnumerator()},function(){return n.moveNext()?this.yieldReturn(n.current()):this.yieldBreak()},function(){o.dispose(n)})})};u.prototype.traverseBreadthFirst=function(t,n){var e=this;return t=o.createLambda(t),n=o.createLambda(n),new u(function(){var r,i=0,a=[];return new s(function(){r=e.getEnumerator()},function(){for(;;){if(r.moveNext())return a.push(r.current()),this.yieldReturn(n(r.current(),i));const f=u.from(a).selectMany(function(c){return t(c)});if(f.any())i++,a=[],o.dispose(r),r=f.getEnumerator();else return!1}},function(){o.dispose(r)})})};u.prototype.traverseDepthFirst=function(t,n){var e=this;return t=o.createLambda(t),n=o.createLambda(n),new u(function(){var r=[],i;return new s(function(){i=e.getEnumerator()},function(){for(;;){if(i.moveNext()){const a=n(i.current(),r.length);return r.push(i),i=u.from(t(i.current())).getEnumerator(),this.yieldReturn(a)}if(r.length<=0)return!1;o.dispose(i),i=r.pop()}},function(){try{o.dispose(i)}finally{u.from(r).forEach(function(a){a.dispose()})}})})};u.prototype.flatten=function(){var t=this;return new u(function(){var n,e=null;return new s(function(){n=t.getEnumerator()},function(){for(;;){if(e!=null){if(e.moveNext())return this.yieldReturn(e.current());e=null}if(n.moveNext())if(n.current()instanceof Array){o.dispose(e),e=u.from(n.current()).selectMany(l.Identity).flatten().getEnumerator();continue}else return this.yieldReturn(n.current());return!1}},function(){try{o.dispose(n)}finally{o.dispose(e)}})})};u.prototype.pairwise=function(t){var n=this;return t=o.createLambda(t),new u(function(){var e;return new s(function(){e=n.getEnumerator(),e.moveNext()},function(){var r=e.current();return e.moveNext()?this.yieldReturn(t(r,e.current())):!1},function(){o.dispose(e)})})};u.prototype.scan=function(t,n){var e;n==null?(n=o.createLambda(t),e=!1):(n=o.createLambda(n),e=!0);var r=this;return new u(function(){var i,a,f=!0;return new s(function(){i=r.getEnumerator()},function(){if(f){if(f=!1,e)return this.yieldReturn(a=t);if(i.moveNext())return this.yieldReturn(a=i.current())}return i.moveNext()?this.yieldReturn(a=n(a,i.current())):!1},function(){o.dispose(i)})})};u.prototype.select=function(t){if(t=o.createLambda(t),t.length<=1)return new b(this,null,t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()?this.yieldReturn(t(e.current(),r++)):!1},function(){o.dispose(e)})})};u.prototype.selectMany=function(t,n){var e=this;return t=o.createLambda(t),n==null&&(n=function(r,i){return i}),n=o.createLambda(n),new u(function(){var r,i=void 0,a=0;return new s(function(){r=e.getEnumerator()},function(){if(i===void 0&&!r.moveNext())return!1;do{if(i==null){const f=t(r.current(),a++);i=u.from(f).getEnumerator()}if(i.moveNext())return this.yieldReturn(n(r.current(),i.current()));o.dispose(i),i=null}while(r.moveNext());return!1},function(){try{o.dispose(r)}finally{o.dispose(i)}})})};u.prototype.where=function(t){if(t=o.createLambda(t),t.length<=1)return new E(this,t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();)if(t(e.current(),r++))return this.yieldReturn(e.current());return!1},function(){o.dispose(e)})})};u.prototype.choose=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();){const i=t(e.current(),r++);if(i!=null)return this.yieldReturn(i)}return this.yieldBreak()},function(){o.dispose(e)})})};u.prototype.ofType=function(t){var n;switch(t){case Number:n=m.Number;break;case String:n=m.String;break;case Boolean:n=m.Boolean;break;case Function:n=m.Function;break;default:n=null;break}return n===null?this.where(function(e){return e instanceof t}):this.where(function(e){return typeof e===n})};u.prototype.zip=function(){var t=arguments,n=o.createLambda(arguments[arguments.length-1]),e=this;if(arguments.length==2){const r=arguments[0];return new u(function(){var i,a,f=0;return new s(function(){i=e.getEnumerator(),a=u.from(r).getEnumerator()},function(){return i.moveNext()&&a.moveNext()?this.yieldReturn(n(i.current(),a.current(),f++)):!1},function(){try{o.dispose(i)}finally{o.dispose(a)}})})}else return new u(function(){var r,i=0;return new s(function(){var a=u.make(e).concat(u.from(t).takeExceptLast().select(u.from)).select(function(f){return f.getEnumerator()}).toArray();r=u.from(a)},function(){if(r.all(function(a){return a.moveNext()})){const a=r.select(function(f){return f.current()}).toArray();return a.push(i++),this.yieldReturn(n.apply(null,a))}else return this.yieldBreak()},function(){u.from(r).forEach(o.dispose)})})};u.prototype.merge=function(){var t=arguments,n=this;return new u(function(){var e,r=-1;return new s(function(){e=u.make(n).concat(u.from(t).select(u.from)).select(function(i){return i.getEnumerator()}).toArray()},function(){for(;e.length>0;){r=r>=e.length-1?0:r+1;const i=e[r];if(i.moveNext())return this.yieldReturn(i.current());i.dispose(),e.splice(r--,1)}return this.yieldBreak()},function(){u.from(e).forEach(o.dispose)})})};u.prototype.join=function(t,n,e,r,i){n=o.createLambda(n),e=o.createLambda(e),r=o.createLambda(r),i=o.createLambda(i);var a=this;return new u(function(){var f,c,p=null,h=0;return new s(function(){f=a.getEnumerator(),c=u.from(t).toLookup(e,l.Identity,i)},function(){for(;;){if(p!=null){let v=p[h++];if(v!==void 0)return this.yieldReturn(r(f.current(),v));v=null,h=0}if(f.moveNext()){const v=n(f.current());p=c.get(v).toArray()}else return!1}},function(){o.dispose(f)})})};u.prototype.leftJoin=function(t,n,e,r,i){n=o.createLambda(n),e=o.createLambda(e),r=o.createLambda(r),i=o.createLambda(i);var a=this;return new u(function(){var f,c,p=null,h=0;return new s(function(){f=a.getEnumerator(),c=u.from(t).toLookup(e,l.Identity,i)},function(){for(;;){if(p!=null){let v=p[h++];if(v!==void 0)return this.yieldReturn(r(f.current(),v));v=null,h=0}if(f.moveNext()){const v=n(f.current());if(p=c.get(v).toArray(),p==null||p.length==0)return this.yieldReturn(r(f.current(),null))}else return!1}},function(){o.dispose(f)})})};u.prototype.groupJoin=function(t,n,e,r,i){n=o.createLambda(n),e=o.createLambda(e),r=o.createLambda(r),i=o.createLambda(i);var a=this;return new u(function(){var f=a.getEnumerator(),c=null;return new s(function(){f=a.getEnumerator(),c=u.from(t).toLookup(e,l.Identity,i)},function(){if(f.moveNext()){const p=c.get(n(f.current()));return this.yieldReturn(r(f.current(),p))}return!1},function(){o.dispose(f)})})};u.prototype.all=function(t){t=o.createLambda(t);var n=!0;return this.forEach(function(e){if(!t(e))return n=!1,!1}),n};u.prototype.any=function(t){t=o.createLambda(t);var n=this.getEnumerator();try{if(arguments.length==0)return n.moveNext();for(;n.moveNext();)if(t(n.current()))return!0;return!1}finally{o.dispose(n)}};u.prototype.isEmpty=function(){return!this.any()};u.prototype.concat=function(){var t=this;if(arguments.length==1){const n=arguments[0];return new u(function(){var e,r;return new s(function(){e=t.getEnumerator()},function(){if(r==null){if(e.moveNext())return this.yieldReturn(e.current());r=u.from(n).getEnumerator()}return r.moveNext()?this.yieldReturn(r.current()):!1},function(){try{o.dispose(e)}finally{o.dispose(r)}})})}else{const n=arguments;return new u(function(){var e;return new s(function(){e=u.make(t).concat(u.from(n).select(u.from)).select(function(r){return r.getEnumerator()}).toArray()},function(){for(;e.length>0;){const r=e[0];if(r.moveNext())return this.yieldReturn(r.current());r.dispose(),e.splice(0,1)}return this.yieldBreak()},function(){u.from(e).forEach(o.dispose)})})}};u.prototype.insert=function(t,n){var e=this;return new u(function(){var r,i,a=0,f=!1;return new s(function(){r=e.getEnumerator(),i=u.from(n).getEnumerator()},function(){return a==t&&i.moveNext()?(f=!0,this.yieldReturn(i.current())):r.moveNext()?(a++,this.yieldReturn(r.current())):!f&&i.moveNext()?this.yieldReturn(i.current()):!1},function(){try{o.dispose(r)}finally{o.dispose(i)}})})};u.prototype.alternate=function(t){var n=this;return new u(function(){var e,r,i,a;return new s(function(){t instanceof Array||t.getEnumerator!=null?i=u.from(u.from(t).toArray()):i=u.make(t),r=n.getEnumerator(),r.moveNext()&&(e=r.current())},function(){for(;;){if(a!=null){if(a.moveNext())return this.yieldReturn(a.current());a=null}if(e==null&&r.moveNext()){e=r.current(),a=i.getEnumerator();continue}else if(e!=null){const f=e;return e=null,this.yieldReturn(f)}return this.yieldBreak()}},function(){try{o.dispose(r)}finally{o.dispose(a)}})})};u.prototype.contains=function(t,n){n=o.createLambda(n);var e=this.getEnumerator();try{for(;e.moveNext();)if(n(e.current())===t)return!0;return!1}finally{o.dispose(e)}};u.prototype.defaultIfEmpty=function(t){var n=this;return t===void 0&&(t=null),new u(function(){var e,r=!0;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()?(r=!1,this.yieldReturn(e.current())):r?(r=!1,this.yieldReturn(t)):!1},function(){o.dispose(e)})})};u.prototype.distinct=function(t){return this.except(u.empty(),t)};u.prototype.distinctUntilChanged=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();){const i=t(e.current());if(r!==i)return r=i,this.yieldReturn(e.current())}return this.yieldBreak()},function(){o.dispose(e)})})};u.prototype.except=function(t,n){n=o.createLambda(n);var e=this;return new u(function(){var r,i;return new s(function(){r=e.getEnumerator(),i=new k(n),u.from(t).forEach(function(a){i.add(a)})},function(){for(;r.moveNext();){const a=r.current();if(!i.contains(a))return i.add(a),this.yieldReturn(a)}return!1},function(){o.dispose(r)})})};u.prototype.intersect=function(t,n){n=o.createLambda(n);var e=this;return new u(function(){var r,i,a;return new s(function(){r=e.getEnumerator(),i=new k(n),u.from(t).forEach(function(f){i.add(f)}),a=new k(n)},function(){for(;r.moveNext();){const f=r.current();if(!a.contains(f)&&i.contains(f))return a.add(f),this.yieldReturn(f)}return!1},function(){o.dispose(r)})})};u.prototype.sequenceEqual=function(t,n){n=o.createLambda(n);var e=this.getEnumerator();try{const r=u.from(t).getEnumerator();try{for(;e.moveNext();)if(!r.moveNext()||n(e.current())!==n(r.current()))return!1;return!r.moveNext()}finally{o.dispose(r)}}finally{o.dispose(e)}};u.prototype.union=function(t,n){n=o.createLambda(n);var e=this;return new u(function(){var r,i,a;return new s(function(){r=e.getEnumerator(),a=new k(n)},function(){var f;if(i===void 0){for(;r.moveNext();)if(f=r.current(),!a.contains(f))return a.add(f),this.yieldReturn(f);i=u.from(t).getEnumerator()}for(;i.moveNext();)if(f=i.current(),!a.contains(f))return a.add(f),this.yieldReturn(f);return!1},function(){try{o.dispose(r)}finally{o.dispose(i)}})})};u.prototype.orderBy=function(t,n){return new g(this,t,n,!1)};u.prototype.orderByDescending=function(t,n){return new g(this,t,n,!0)};u.prototype.reverse=function(){var t=this;return new u(function(){var n,e;return new s(function(){n=t.toArray(),e=n.length},function(){return e>0?this.yieldReturn(n[--e]):!1},l.Blank)})};u.prototype.shuffle=function(){var t=this;return new u(function(){var n;return new s(function(){n=t.toArray()},function(){if(n.length>0){const e=Math.floor(Math.random()*n.length);return this.yieldReturn(n.splice(e,1)[0])}return!1},l.Blank)})};u.prototype.weightedSample=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.choose(function(i){var a=t(i);return a<=0?null:(r+=a,{value:i,bound:r})}).toArray()},function(){if(e.length>0){const i=Math.floor(Math.random()*r)+1;let a=-1,f=e.length;for(;f-a>1;){const c=Math.floor((a+f)/2);e[c].bound>=i?f=c:a=c}return this.yieldReturn(e[f].value)}return this.yieldBreak()},l.Blank)})};u.prototype.groupBy=function(t,n,e,r){var i=this;return t=o.createLambda(t),n=o.createLambda(n),e!=null&&(e=o.createLambda(e)),r=o.createLambda(r),new u(function(){var a;return new s(function(){a=i.toLookup(t,n,r).toEnumerable().getEnumerator()},function(){for(;a.moveNext();)return e==null?this.yieldReturn(a.current()):this.yieldReturn(e(a.current().key(),a.current()));return!1},function(){o.dispose(a)})})};u.prototype.partitionBy=function(t,n,e,r){var i=this;t=o.createLambda(t),n=o.createLambda(n),r=o.createLambda(r);var a;return e==null?(a=!1,e=function(f,c){return new R(f,c)}):(a=!0,e=o.createLambda(e)),new u(function(){var f,c,p,h=[];return new s(function(){f=i.getEnumerator(),f.moveNext()&&(c=t(f.current()),p=r(c),h.push(n(f.current())))},function(){for(var v;(v=f.moveNext())==!0&&p===r(t(f.current()));)h.push(n(f.current()));if(h.length>0){const y=a?e(c,u.from(h)):e(c,h);return v?(c=t(f.current()),p=r(c),h=[n(f.current())]):h=[],this.yieldReturn(y)}return!1},function(){o.dispose(f)})})};u.prototype.buffer=function(t){var n=this;return new u(function(){var e;return new s(function(){e=n.getEnumerator()},function(){for(var r=[],i=0;e.moveNext();)if(r.push(e.current()),++i>=t)return this.yieldReturn(r);return r.length>0?this.yieldReturn(r):!1},function(){o.dispose(e)})})};u.prototype.aggregate=function(t,n,e){return e=o.createLambda(e),e(this.scan(t,n,e).last())};u.prototype.average=function(t){t=o.createLambda(t);var n=0,e=0;return this.forEach(function(r){n+=t(r),++e}),n/e};u.prototype.count=function(t){t=t==null?l.True:o.createLambda(t);var n=0;return this.forEach(function(e,r){t(e,r)&&++n}),n};u.prototype.max=function(t){return t==null&&(t=l.Identity),this.select(t).aggregate(function(n,e){return n>e?n:e})};u.prototype.min=function(t){return t==null&&(t=l.Identity),this.select(t).aggregate(function(n,e){return nt(e)?n:e})};u.prototype.minBy=function(t){return t=o.createLambda(t),this.aggregate(function(n,e){return t(n)0:u.prototype.any.apply(this,arguments)};d.prototype.count=function(t){return t==null?this.getSource().length:u.prototype.count.apply(this,arguments)};d.prototype.elementAt=function(t){var n=this.getSource();return 0<=t&&t0?n[0]:u.prototype.first.apply(this,arguments)};d.prototype.firstOrDefault=function(t,n){if(t!==void 0)return u.prototype.firstOrDefault.apply(this,arguments);n=t;var e=this.getSource();return e.length>0?e[0]:n};d.prototype.last=function(t){var n=this.getSource();return t==null&&n.length>0?n[n.length-1]:u.prototype.last.apply(this,arguments)};d.prototype.lastOrDefault=function(t,n){if(t!==void 0)return u.prototype.lastOrDefault.apply(this,arguments);n=t;var e=this.getSource();return e.length>0?e[e.length-1]:n};d.prototype.skip=function(t){var n=this.getSource();return new u(function(){var e;return new s(function(){e=t<0?0:t},function(){return e0?this.yieldReturn(t[--n]):!1},l.Blank)})};d.prototype.sequenceEqual=function(t,n){return(t instanceof d||t instanceof Array)&&n==null&&u.from(t).count()!=this.count()?!1:u.prototype.sequenceEqual.apply(this,arguments)};d.prototype.toJoinedString=function(t,n){var e=this.getSource();return n!=null||!(e instanceof Array)?u.prototype.toJoinedString.apply(this,arguments):(t==null&&(t=""),e.join(t))};d.prototype.getEnumerator=function(){var t=this.getSource(),n=-1;return{current:function(){return t[n]},moveNext:function(){return++n