There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$.

Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable.

NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below:

function findPath (array){
	var x = 0, y = 0;
	var found = true;
	var path = "";
	for (var i in array){
		while (1){
			if (array[i][0] > x){
				path+='R';
				x+=1;
			}
			else if(array[i][1] > y){
				path +='U';
				y+=1;
			}
			else if(x == array[i][0] && y == array[i][1]){
				break;
			}
			else{
				found = false;
				break
			}
		}
		if (!found){
			break;
		}
	}
	if(found){
		print('YES');
		print(path);
	}
	else{
		print('NO');
	}
}

var queries = parseInt(readline())
for(var i = 0 ; i < queries; i++){
var array = [];
var elements = parseInt(readline());
for (var j = 0; j< elements; j++){
	var entries = readline().split(' ').map((val)=>{
		return parseInt(val);
	})
	array.push(entries)
}
array.sort((a,b)=>{
	if (a[0]!= b[0]){
		return a[0] - b[0];
	}
	else{
		return a[1] - b[1];
	}})
findPath(array)
}

'use strict'

const problem = (a) => {
    let r = '';
    let prevX = 0;
    let min = 0;
    let max = 0;
    const keys = Object.keys(a).sort((x, y) => x - y);
    for (let i = 0; i < keys.length; i++) {
        const x = keys[i];
        if (max > a[x].min) return 'NO';
        r += 'R'.repeat(x - prevX) + 'U'.repeat(a[x].max - max);
        max = a[x].max;
        min = a[x].min;
        prevX = x;
    }
    return `YES\n${r}`;
}

let t = +readline();
while(t--) {
    const n = +readline();
    const a = Array.from({ length: n }, () => readline().split(' ').map(Number))
        .reduce((r, t) => {
            const x = t[0];
            const y = t[1];
            if (!r[x]) {
                r[x] = { min: y, max: y };
            } else {
                r[x].min = Math.min(r[x].min, y);
                r[x].max = Math.max(r[x].max, y);
            }
            return r;
        }, {});
    print(problem(a));
}

Main();
function myout(t){print(t);}//standard output
//[no]param
//不明値:何もしない  1:数値へ変換  2:半角SPで分割  3:改行で分割  4:半角SPで分割し、数値配列へ
//5:改行で分割し、数値配列へ  6:1文字に分割  7:1文字に分割し、数値配列へ
//8:半角SPで結合　9:改行で結合　0:文字なしで結合
function myconv(i,no){switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 3:return i.split("\n");case 4:return i.split(" ").map((a)=>Number(a));case 5:return i.split("\n").map((a)=>Number(a));case 6:return i.split("");case 7:return i.split("").map((a)=>Number(a));case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}
function Main() {
  var t = myconv(readline(),1);
  var output = [];
  for(var i = 0; i < t; i++){
      var n = myconv(readline(),1);
      var list = new Array(n);
      var underX = 0;
      var underY = 0;
      for(var j = 0; j < n; j++){
          var tmp = myconv(readline(),4);
          tmp = [tmp[0],tmp[1]];
          list[j] = tmp;
      }
      list.sort(function(a,b){
          var tmpa = a[0] + a[1];
          var tmpb = b[0] + b[1];
          return tmpa - tmpb;
      });
      var out = "";
      var breakFlg = false;
      for(var j = 0; j < n; j++){
          var x = list[j][0];
          var y = list[j][1];
          if(underX <= x && underY <= y){
              for(var k = 0; k < Math.abs(x - underX); k++){
                  out += "R";
              }
              for(var k = 0; k < Math.abs(y - underY); k++){
                  out += "U";
              }
              underX = x;
              underY = y;
          }else{
              breakFlg = true;
              break;
          }
      }
      if(!breakFlg){
          output.push("YES");
          output.push(out);
      }else{
          output.push("NO");
      }
  }
  myout(myconv(output,9));
}

function main() {
  var testCasesNum = +readline();
  for (var i = 0; i < testCasesNum; i++) {
    findNext();
  }
};

function findNext() {
    var pointsNum = +readline();
    var points = {};
    for (var j = 0; j < pointsNum; j++) {
        var xy = readline().split(' ').map(Number);
        var x = xy[0];
        var y = xy[1];
        if (!points[x]) {
            points[x] = {
                min: 1000,
                max: 0
            }
        }
        if (y < points[x].min) {
            points[x].min = y;
        }
        if (y > points[x].max) {
            points[x].max = y;
        }
    }
    var keyss = Object.keys(points).map(Number).sort(byNum);
    var points2 = {};
    for (var k = 0; k < keyss.length; k++) {
        points2[keyss[k]] = points[keyss[k]]
    }
    var curr = {
        x: 0,
        y: 0
    };
    var res = '';
    for (var m = 0; m < keyss.length; m++) {
        var nextX = keyss[m];
        if (points2[nextX].min < curr.y) {
            print('NO');
            return;
        }

        for (var a = curr.x; a < nextX; a++) {
            res += 'R';
        }
        curr.x = nextX;

        for (var b = curr.y; b < points2[nextX].max; b++) {
            res += 'U';
        }
        curr.y = points2[nextX].max;
        // print(res, nextX, curr, keys);
    }
    print('YES');
    print(res);
};

function byNum(a, b) { return a - b };
main();

var t = parseInt(readline(), 10);

for(var i=1; i<=t; i++) {
    var n = parseInt(readline(), 10);
    var path = '', x = 0, y = 0, valid = true, xy = [];

    for(var j=1; j<=n; j++) {
        var inp = readline().split(' ').map(el => parseInt(el, 10));
        xy.push({x: inp[0], y: inp[1]});
    }

    xy.sort((a,b) => (a.x + a.y) - (b.x + b.y));

    for(var k=0; k<n; k++) {
        if(xy[k].x < x) {
            valid = false;
            break;
        } else {
            for(var m=1; m<=(xy[k].x - x); m++) {
                path += 'R';
            }

            x = xy[k].x;

        }

        if(xy[k].y < y) {
            valid = false;
            break;
        } else {
            for(var m=1; m<=(xy[k].y - y); m++) {
                path += 'U';
            }

            y = xy[k].y;

        }
    }

    if(valid) {
        print('YES');
        print(path);
    } else print('NO');
}

var tests = parseInt(readline());
while(tests--){
  var n =  parseInt(readline());
  var packs = [];
  var path='';
  var _x,_y;
  _x=_y=0;
  for(var i=0;i<n;i++){
      packs.push(readline().split(' ').map(function(a){
          return parseInt(a);
      }));
  }
  packs.sort(function(a,b){
      if(a[0]!=b[0]){
          return a[0]-b[0];
      }else if(a[1] != b[1]){
          return a[1]-b[1];
      }
  });
    for(var j=0;j<packs.length;j++){
        var x = packs[j][0];
        var y = packs[j][1];
        if(x >= _x && y >=_y){
            for(var k=0;k<(x-_x);k++){
                path +='R';
            }
            for(var k=0;k<(y-_y);k++){
                path +='U';
            }
            _x = x;
            _y = y;
        }else if(_y > y){
            path="";
            break;
        }
    }
    if(path!=''){
        print("YES");
        print(path);
    }else{
        print("NO");
    }
}

process.stdin.resume()
process.stdin.setEncoding('utf-8')

var arr = ''
process.stdin.on('data', (i) => {
  arr += i
})

process.stdin.on('end', () => {
  arr = arr.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
  main(arr)
})

function wr(...x) {
  console.log(...x)
}

function sort(a, inc = true) {
  if(inc) a.sort((a, b) => a - b)
  else a.sort((a, b) => b - a)
}

function readInt(a, i) {
  if(i === undefined) return parseInt(a.shift())
  else return parseInt(a[i])
}

function readInts(a, i) {
  if(i === undefined) return arr.shift().split(' ').map(a => parseInt(a))
  else return arr[i].split(' ').map(a => parseInt(a))
}

function main(input) {
  let i = 0
  const t = readInt(input, i++)

  for(let j = 0; j < t; j++) {
    const n = readInt(input, i++)
    const as = new Array(n)
    for(let k = 0; k < n; k++) {
      as[k] = readInts(input, i++)
    }

    as.sort((a, b) => {
      if(a[0] !== b[0]) return a[0] - b[0]
      else return a[1] - b[1]
    })
    // wr(as)
    let x = 0
    let y = 0
    let f = true
    let s = ""
    for(let k = 0; k < n && f; k++) {
      const [x1, y1] = as[k]
      if(x1 < x || y1 < y || (x1 === x && y1 === y)) f = false
      else if(x1 === x) {
        let t = y1 - y
        while(t--) s += "U"
        y = y1
      }
      else if(y1 === y) {
        let t = x1 - x
        while(t--) s += "R"
        x = x1
      }
      else {
        let t = x1 - x
        while(t--) s += "R"
        t = y1 - y
        while(t--) s += "U"
        x = x1
        y = y1
      }
    }
    if(!f) wr('NO')
    else {
      wr('YES')
      wr(s)
    }
  }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function readint() {
    let line = readline().split(' ').map(function(num) {
        return parseInt(num);
    });
    return line;
}

function readfloat() {
    let line = readline().split(' ').map(function(num) {
        return parseFloat(num);
    });
    return line;
}

function print(x) {
    console.log(x);
}

function write(x) {
    process.stdout.write(x);
}
function main() {
    let t = readline();
    //go go bro!!!
    while(t--) {
        const n = readline();
        let i = n;
        let points = [];
        while(i--) {
            points.push(readint());
        }
        points.sort(function(a,b) {
            if(a[0] > b[0]) {
                return 1;
            } else if(a[0] < b[0]) {
                return -1;
            } else {
                if(a[1] > b[1]) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        let printable = true;
        let ans = [];
        let prev = [0,0];
        for(let i = 0; i < n; ++i) {
            let curr = points[i];
            if(prev[1] > curr[1]) {
                print("NO");
                printable = false;
                break;
            } else {
                for(let j = 0; j < curr[0] - prev[0]; j++) {
                    ans.push('R');
                }
                for(let j = 0; j < curr[1] - prev[1]; j++) {
                    ans.push('U');
                }
            }
            prev = curr;
        }
        if(printable) {
            print('YES');
            print(ans.join(''));
        }
    }
}

function findPath (array){
	var x = 0, y = 0;
	var found = true;
	var path = "";
	for (var i in array){
		while (1){
			if (array[i][0] > x){
				path+='R';
				x+=1;
			}
			else if(array[i][1] > y){
				path +='U';
				y+=1;
			}
			else if(x == array[i][0] && y == array[i][1]){
				break;
			}
			else{
				found = false;
				break
			}
		}
		if (!found){
			break;
		}
	}
	if(found){
		print('YES');
		print(path);
	}
	else{
		print('NO');
	}
}

var queries = parseInt(readline())
for(var i = 0 ; i < queries; i++){
var array = [];
var elements = parseInt(readline());
for (var i = 0; i< elements; i++){
	var entries = readline().split(' ').map((val)=>{
		return parseInt(val);
	})
	array.push(entries)
}
array.sort((a,b)=>{
	if (a[0]!= b[0]){
		return a[0] - b[0];
	}
	else{
		return a[1] - b[1];
	}})
findPath(array)
}

'use strict'

const problem = (a) => {
    let r = '';
    let prevX = 0;
    let min = 0;
    let max = 0;
    for (let x in a) {
        if (max > a[x].min) return 'NO';
        r += 'R'.repeat(x - prevX) + 'U'.repeat(a[x].max - max);
        max = a[x].max;
        min = a[x].min;
        prevX = x;
    }
    return `YES\n${r}`;
}

let t = +readline();
while(t--) {
    const n = +readline();
    const a = Array.from({ length: n }, () => readline().split(' ').map(Number))
        .reduce((r, t) => {
            const x = t[0];
            const y = t[1];
            if (!r[x]) {
                r[x] = { min: y, max: y };
            } else {
                r[x].min = Math.min(r[x].min, x);
                r[x].max = Math.max(r[x].max, y);
            }
            return r;
        }, {});
    print(problem(a));
}

'use strict'

const problem = (a) => {
    let r = '';
    let prevX = 0;
    let prevY = 0;
    for (let x in a) {
        if (a[x] < prevY) return 'NO';
        r += 'R'.repeat(x - prevX) + 'U'.repeat(a[x] - prevY)
        prevY = a[prevX = x];
    }
    return `YES\n${r}`;

}

let t = +readline();
while(t--) {
    const n = +readline();
    const a = Array.from({ length: n }, () => readline().split(' ').map(Number))
        .reduce((r, t) => { r[t[0]] = Math.max(r[t[0]] || 0, t[1]); return r; }, {});
    print(problem(a));
}

'use strict'

const problem = (a) => {
    let r = '';
    let prevX = 0;
    let prevY = 0;
    const keys = Object.keys(a).sort((x, y) => x - y);
    for (let i = 0; i < keys.length; i++) {
        const x = keys[i];
        if (a[x] < prevY) return 'NO';
        r += 'R'.repeat(x - prevX) + 'U'.repeat(a[x] - prevY);
        prevY = a[prevX = x];
    }
    return `YES\n${r}`;
}

let t = +readline();
while(t--) {
    const n = +readline();
    const a = Array.from({ length: n }, () => readline().split(' ').map(Number))
        .reduce((r, t) => { r[t[0]] = Math.max(r[t[0]] || 0, t[1]); return r; }, {});
    print(problem(a));
}

'use strict'

const problem = (a) => {
    let r = '';
    let prevX = 0;
    let min = 0;
    let max = 0;
    const keys = Object.keys(a).sort((x, y) => x - y);
    for (let i = 0; i < keys.length; i++) {
        const x = keys[i];
        if (max > a[x].min) return 'NO';
        r += 'R'.repeat(x - prevX) + 'U'.repeat(a[x].max - max);
        max = a[x].max;
        min = a[x].min;
        prevX = x;
    }
    return `YES\n${r}`;
}

let t = +readline();
while(t--) {
    const n = +readline();
    const a = Array.from({ length: n }, () => readline().split(' ').map(Number))
        .reduce((r, t) => {
            const x = t[0];
            const y = t[1];
            if (!r[x]) {
                r[x] = { min: y, max: y };
            } else {
                r[x].min = Math.min(r[x].min, x);
                r[x].max = Math.max(r[x].max, y);
            }
            return r;
        }, {});
    print(problem(a));
}

function main() {
  var testCasesNum = +readline();
  for (var i = 0; i < testCasesNum; i++) {
    findNext();
  }
}

function findNext() {
    var pointsNum = +readline();
    var points = {};
    for (var j = 0; j < pointsNum; j++) {
        var xy = readline().split(' ').map(Number);
        var x = xy[0];
        var y = xy[1];
        if (!points[x]) {
            points[x] = {
                min: 1000,
                max: 0
            }
        }
        if (y < points[x].min) {
            points[x].min = y;
        }
        if (y > points[x].max) {
            points[x].max = y;
        }
    }
    var keyss = Object.keys(points).map(Number).sort(byNum);
    var points2 = {};
    for (var k = 0; k < keyss.length; k++) {
        points2[keyss[k]] = points[keyss[k]]
    }
    print(points2);
    var curr = {
        x: 0,
        y: 0
    };
    var res = '';
    for (var m = 0; m < keyss.length; m++) {
        var nextX = keyss[m];
        if (points2[nextX].min < curr.y) {
            print('NO');
            return;
        }

        for (var a = curr.x; a < nextX; a++) {
            res += 'R';
        }
        curr.x = nextX;

        for (var b = curr.y; b < points2[nextX].max; b++) {
            res += 'U';
        }
        curr.y = points2[nextX].max;
        // print(res, nextX, curr, keys);
    }
    print('YES');
    print(res);
};

function byNum(a, b) { return a - b }

main();

function main() {
  var testCasesNum = +readline();
  for (var i = 0; i < testCasesNum; i++) {
    findNext();
  }
}

function findNext() {
    var pointsNum = +readline();
    var points = {};
    for (var j = 0; j < pointsNum; j++) {
        var xy = readline().split(' ').map(Number);
        var x = xy[0];
        var y = xy[1];
        if (!points[x]) {
            points[x] = {
                min: 1000,
                max: 0
            }
        }
        if (y < points[x].min) {
            points[x].min = y;
        }
        if (y > points[x].max) {
            points[x].max = y;
        }
    }
    var keyss = Object.keys(points).sort(byNum);
    var points2 = {};
    for (var k = 0; k < keyss.length; k++) {
        points2[keyss[k]] = points[keyss[k]]
    }
    // print(points2);
    var curr = {
        x: 0,
        y: 0
    };
    var res = '';
    for (var m = 0; m < keyss.length; m++) {
        var nextX = keyss[m];
        for (var a = curr.x; a < nextX; a++) {
            res += 'R';
        }
        curr.x = nextX;
        if (points2[nextX].min < curr.y) {
            print('NO');
            return;
        }
        for (var b = curr.y; b < points2[nextX].max; b++) {
            res += 'U';
        }
        curr.y = points2[nextX].max;
        // print(res, nextX, curr, keys);
    }
    print('YES');
    print(res);
};

function byNum(a, b) { return a - b };

main()

var t = parseInt(readline(), 10);

for(var i=1; i<=t; i++) {
    var n = parseInt(readline(), 10);
    var path = '', x = 0, y = 0, valid = true, xy = [];

    for(var j=1; j<=n; j++) {
        var inp = readline().split(' ').map(el => parseInt(el, 10));
        xy.push({x: inp[0], y: inp[1]});
    }

    xy.sort((a,b) => a.y - b.y);
    xy.sort((a,b) => a.x - b.x);

    for(var k=0; k<n; k++) {
        if(xy[k].x < x) {
            valid = false;
            break;
        } else {
            for(var m=1; m<=(xy[k].x - x); m++) {
                path += 'R';
            }

            x = xy[k].x;

        }

        if(xy[k].y < y) {
            valid = false;
            break;
        } else {
            for(var m=1; m<=(xy[k].y - y); m++) {
                path += 'U';
            }

            y = xy[k].y;

        }
    }

    if(valid) {
        print('YES');
        print(path);
    } else print('NO');
}

var t = parseInt(readline(), 10);

for(var i=1; i<=t; i++) {
    var n = parseInt(readline(), 10);
    var path = '', x = 0, y = 0, valid = true, xy = [];

    for(var j=1; j<=n; j++) {
        var inp = readline().split(' ').map(el => parseInt(el, 10));
        xy.push({x: inp[0], y: inp[1]});
    }

    xy.sort((a,b) => a.x - b.x);
    xy.sort((a,b) => a.y - b.y);

    for(var k=0; k<n; k++) {
        if(xy[k].x < x) {
            valid = false;
            break;
        } else {
            for(var m=1; m<=(xy[k].x - x); m++) {
                path += 'R';
            }

            x = xy[k].x;

        }

        if(xy[k].y < y) {
            valid = false;
            break;
        } else {
            for(var m=1; m<=(xy[k].y - y); m++) {
                path += 'U';
            }

            y = xy[k].y;

        }
    }

    if(valid) {
        print('YES');
        print(path);
    } else print('NO');
}

function increment(npacks,packs){
    packs = packs.sort(function(a,b){
        return (a[0] - b[0]);
    });
  var curX = 0;
  var curY = 0;
  var flag = false;
  for (var i = 0; i < npacks; i++) {
    nextX = packs[i][0];
    nextY = packs[i][1];
    if (nextX < curX && nextY > curY) {
      flag = true;
    } else if (nextY < curY && nextX > curX) {
      flag = true;
    }
    curX = nextX;
    curY = nextY;
  }
  if (flag) {
    print("NO");
    return false;
  } else {
    print("YES");
    curX = 0;
    curY = 0;
    var remains = packs;
    var path = "";
    var cur = [];
    while (remains.length) {
      nextX = remains[0][0];
      nextY = remains[0][1];
      cur.push(remains[0]);
      remains.splice(0, 1);
      for (var j = 1; j < remains.length; j++) {
        if (remains[j][0] == nextX) {
          cur.push(remains[j]);
          remains.splice(j, 1);
        }
      }
      cur.sort();
      var rMoves = nextX - curX;
      if (nextX > curX) {
        for (var a = 0; a < rMoves; a++) {
          path += 'R';
          curX++;
        }
      }
      for (var b in cur) {
        var uMoves = nextY - curY;
        for (var r = 0; r < uMoves; r++) {
          path += 'U';
          curY++;
        }
      }
      cur = [];
    }
    if(path.length>0){
    		print(path);
        return true;
    }else{
    	return false;
    }
  }
}

var t = parseInt(readline());
while(t--){
    var npacks = parseInt(readline());
    var packs = [];
    for (var l = 0; l < npacks; l++) {
        packs.push(readline().split(' ').map((val)=>{return parseInt(val)}));

    }
    increment(npacks,packs);
}

function increment(npacks,packs){
    packs = packs.sort(function(a,b){
        if(a[0]!=b[0]){
            return (a[0] - b[0]);
        }else{
            return (a[1] - b[1]);
        }
    });
  var curX = 0;
  var curY = 0;
  var flag = false;
  for (var i = 0; i < npacks; i++) {
    nextX = packs[i][0];
    nextY = packs[i][1];
    if (nextX < curX && nextY > curY) {
      flag = true;
    } else if (nextY < curY && nextX > curX) {
      flag = true;
    }
    curX = nextX;
    curY = nextY;
  }
  if (flag) {
    print("NO");
    return false;
  } else {
    print("YES");
    curX = 0;
    curY = 0;
    var remains = packs;
    var path = "";
    var cur = [];
    while (remains.length) {
      nextX = remains[0][0];
      nextY = remains[0][1];
      cur.push(remains[0]);
      remains.splice(0, 1);
      for (var j = 1; j < remains.length; j++) {
        if (remains[j][0] == nextX) {
          cur.push(remains[j]);
          remains.splice(j, 1);
        }
      }
      cur.sort();
      var rMoves = nextX - curX;
      if (nextX > curX) {
        for (var a = 0; a < rMoves; a++) {
          path += 'R';
          curX++;
        }
      }
      for (var b in cur) {
        var uMoves = nextY - curY;
        for (var r = 0; r < uMoves; r++) {
          path += 'U';
          curY++;
        }
      }
      cur = [];
    }
    if(path.length>0){
    		print(path);
        return true;
    }else{
    	return false;
    }
  }
}

var t = parseInt(readline());
while(t--){
    var npacks = parseInt(readline());
    var packs = [];
    for (var l = 0; l < npacks; l++) {
        packs.push(readline().split(' ').map((val)=>{return parseInt(val)}));

    }
    increment(npacks,packs);
}

function increment(npacks,packs){
    packs = packs.sort(function(a,b){
        return (a[0] - b[0]);
    });
  var curX = 0;
  var curY = 0;
  var flag = false;
  for (var i = 0; i < npacks; i++) {
    nextX = packs[i][0];
    nextY = packs[i][1];
    if (nextX < curX && nextY > curY) {
      flag = true;
    } else if (nextY < curY && nextX > curX) {
      flag = true;
    }
    curX = nextX;
    curY = nextY;
  }
  if (flag) {
    print("NO");
    return false;
  } else {
    print("YES");
    curX = 0;
    curY = 0;
    var remains = packs;
    var path = "";
    var cur = [];
    while (remains.length) {
      nextX = remains[0][0];
      nextY = remains[0][1];
      cur.push(remains[0]);
      remains.splice(0, 1);
      for (var j = 1; j < remains.length; j++) {
        if (remains[j][0] == nextX) {
          cur.push(remains[j]);
          remains.splice(j, 1);
        }
      }
      cur.sort(function(a,b){
        return (a[0] - b[0]);
        });
      var rMoves = nextX - curX;
      if (nextX > curX) {
        for (var a = 0; a < rMoves; a++) {
          path += 'R';
          curX++;
        }
      }
      for (var b in cur) {
        var uMoves = nextY - curY;
        for (var r = 0; r < uMoves; r++) {
          path += 'U';
          curY++;
        }
      }
      cur = [];
    }
    if(path.length>0){
    		print(path);
        return true;
    }else{
    	return false;
    }
  }
}

var t = parseInt(readline());
while(t--){
    var npacks = parseInt(readline());
    var packs = [];
    for (var l = 0; l < npacks; l++) {
        packs.push(readline().split(' ').map((val)=>{return parseInt(val)}));

    }
    increment(npacks,packs);
}

function increment(npacks,packs){
  var curX = 0;
  var curY = 0;
  var flag = false;
  for (var i = 0; i < npacks; i++) {
    nextX = packs[i][0];
    nextY = packs[i][1];
    if (nextX < curX && nextY > curY) {
      flag = true;
    } else if (nextY < curY && nextX > curX) {
      flag = true;
    }
    curX = nextX;
    curY = nextY;
  }
  if (flag) {
    print("NO");
    return false;
  } else {
    print("YES");
    curX = 0;
    curY = 0;
    var remains = packs;
    var path = "";
    var cur = [];
    while (remains.length) {
      nextX = remains[0][0];
      nextY = remains[0][1];
      cur.push(remains[0]);
      remains.splice(0, 1);
      for (var j = 1; j < remains.length; j++) {
        if (remains[j][0] == nextX) {
          cur.push(remains[j]);
          remains.splice(j, 1);
        }
      }
      cur.sort();
      var rMoves = nextX - curX;
      if (nextX > curX) {
        for (var a = 0; a < rMoves; a++) {
          path += 'R';
          curX++;
        }
      }
      for (var b in cur) {
        var uMoves = nextY - curY;
        for (var r = 0; r < uMoves; r++) {
          path += 'U';
          curY++;
        }
      }
      cur = [];
    }
    if(path.length>0){
    		print(path);
        return true;
    }else{
    	return false;
    }
  }
}

var t = +readline();
while(t--){
    var npacks = +readline();
    var packs = [];
    for (var l = 0; l < npacks; l++) {
        packs.push(readline().split(' '));
    }
    increment(npacks,packs);
}

function increment(npacks,packs){
  var curX = 0;
  var curY = 0;
  var flag = false;
  for (var i = 0; i < npacks; i++) {
    nextX = packs[i][0];
    nextY = packs[i][1];
    if (nextX < curX && nextY > curY) {
      flag = true;
    } else if (nextY < curY && nextX > curX) {
      flag = true;
    }
    curX = nextX;
    curY = nextY;
  }
  if (flag) {
    print("NO");
    return false;
  } else {
    print("YES");
    curX = 0;
    curY = 0;
    var remains = packs;
    var path = "";
    var cur = [];
    while (remains.length) {
      nextX = remains[0][0];
      nextY = remains[0][1];
      cur.push(remains[0]);
      remains.splice(0, 1);
      for (var j = 1; j < remains.length; j++) {
        if (remains[j][0] == nextX) {
          cur.push(remains[j]);
          remains.splice(j, 1);
        }
      }
      cur.sort();
      var rMoves = nextX - curX;
      if (nextX > curX) {
        for (var a = 0; a < rMoves; a++) {
          path += 'R';
          curX++;
        }
      }
      for (var b in cur) {
        var uMoves = cur[b][1] - curY;
        for (var r = 0; r < uMoves; r++) {
          path += 'U';
          curY++;
        }
      }
      cur = [];
    }
    if(path.length>0){
    		print(path);
        return true;
    }else{
    	return false;
    }
  }
}

var t = +readline();
while(t--){
    var npacks = +readline();
    var packs = [];
    for (var l = 0; l < npacks; l++) {
        packs.push(readline().split(' '));
    }
    increment(npacks,packs);
}

function increment(npacks,packs){
    packs = packs.sort(function(a,b){
       return (a[0] - b[0]);
    });
  var curX = 0;
  var curY = 0;
  var flag = false;
  for (var i = 0; i < npacks; i++) {
    nextX = packs[i][0];
    nextY = packs[i][1];
    if (nextX < curX && nextY > curY) {
      flag = true;
    } else if (nextY < curY && nextX > curX) {
      flag = true;
    }
    curX = nextX;
    curY = nextY;
  }
  if (flag) {
    print("NO");
    return false;
  } else {
    print("YES");
    curX = 0;
    curY = 0;
    var remains = packs;
    var path = "";
    var cur = [];
    while (remains.length) {
      nextX = remains[0][0];
      nextY = remains[0][1];
      cur.push(remains[0]);
      remains.splice(0, 1);
      for (var j = 1; j < remains.length; j++) {
        if (remains[j][0] == nextX) {
          cur.push(remains[j]);
          remains.splice(j, 1);
        }
      }
      cur.sort();
      var rMoves = nextX - curX;
      if (nextX > curX) {
        for (var a = 0; a < rMoves; a++) {
          path += 'R';
          curX++;
        }
      }
      for (var b in cur) {
        var uMoves = nextY - curY;
        for (var r = 0; r < uMoves; r++) {
          path += 'U';
          curY++;
        }
      }
      cur = [];
    }
    if(path.length>0){
    		print(path);
        return true;
    }else{
    	return false;
    }
  }
}

var t = +readline();
while(t--){
    var npacks = +readline();
    var packs = [];
    for (var l = 0; l < npacks; l++) {
        packs.push(readline().split(' '));
    }
    increment(npacks,packs);
}


You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.For example, if $$$s$$$ is "acbc" you can get the following strings in one operation:   "aabc" (if you perform $$$s_2 = s_1$$$);  "ccbc" (if you perform $$$s_1 = s_2$$$);  "accc" (if you perform $$$s_3 = s_2$$$ or $$$s_3 = s_4$$$);  "abbc" (if you perform $$$s_2 = s_3$$$);  "acbb" (if you perform $$$s_4 = s_3$$$); Note that you can also apply this operation to the string $$$t$$$.Please determine whether it is possible to transform $$$s$$$ into $$$t$$$, applying the operation above any number of times.Note that you have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is represented by two consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \le |t| \leq 100$$$, $$$|t| = |s|$$$) consisting of lowercase Latin letters.

For each query, print "YES" if it is possible to make $$$s$$$ equal to $$$t$$$, and "NO" otherwise. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).

NoteIn the first query, you can perform two operations $$$s_1 = s_2$$$ (after it $$$s$$$ turns into "aabb") and $$$t_4 = t_3$$$ (after it $$$t$$$ turns into "aabb"). In the second query, the strings are equal initially, so the answer is "YES".In the third query, you can not make strings $$$s$$$ and $$$t$$$ equal. Therefore, the answer is "NO".

'use strict'

readline();
let s,t;
const fn = (s, t) => {
    for (let i of s) { if (t.has(i)) return 'YES'; }
    return 'NO';
}
while((s=readline()) && (t=readline())) {
    write(fn(new Set(s.split('')), new Set(t.split(''))) + '\n');
}

n = +readline();
for (var i = 0; i < n; i++) {
    s1 = readline().split('');
    s2 = readline().split('');
    ans = 'NO';
    for (var k = 0; k < s1.length; k++) {
        if ((s2.includes(s1[k]))||(s1.includes(s2[k]))) {
            ans = 'YES';
            break;
        }
    }
    print(ans)
}

var q = readline();
for(var i = 0; i<q; i++) {
    var s = readline();
    var t = readline();
    var l = 0;

    for(var i1 = 0; i1<s.length; i1++) {
        if(t.indexOf(s.slice(i1, i1+1)) != -1) l++;
    }

    if(l===0) print('NO');
    else print('YES');
}

const readline = require('readline');
//const lineReader = require('fs');
//const scanner = lineReader.createReadStream('main.in');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let n, lp = 0;
let x, y; //String
let frqx = new Array(27);
let frqy = new Array(27);

rl.on('line', (input) => {
    if (lp === 0){
        lp = 1;
        n = parseInt(input) * 2;
    } else if (lp === 1){
        n--;
        x = input;
        lp = 2;
    } else if (lp === 2){
        n--;
        frqx.fill(0);
        frqy.fill(0);
        lp = 1;
        y = input;
        let flg = false;
        for (let i = 0; i < x.length; i++)frqx[x.charCodeAt(i) - "a".charCodeAt(0)] += 1;
        for (let i = 0; i < y.length; i++)frqy[y.charCodeAt(i) - "a".charCodeAt(0)] += 1;
        for (let i = 0; i < 26; i++)if (frqx[i] && frqy[i]){ flg = true; break; }
        if (flg === true)console.log("YES"); else console.log("NO");
    }
    if (n === 0)rl.close();
});

const readline = require("readline").createInterface(process.stdin, process.stdout);
var linesLeft,
    last = '',
    result = '';
readline.on("line", line => {
  if (linesLeft === undefined)
    linesLeft = parseInt(line) * 2;
  else{
    if (linesLeft % 2 === 0)
        last = line;
    else
        solution(last, line);
    if (!(--linesLeft > 0))
        readline.close();
  }
}).on("close",() => {
  console.log(result);
  process.exit(0);
});

function solution (first, second){
    first = [...first];
    second = [...second];
    result += (first.some(ch => second.includes(ch)) ? 'yes' : 'no') + '\n';
}

const readline = require("readline").createInterface(process.stdin, process.stdout);
var linesLeft,
    last = '',
    result = '';
readline.on("line", line => {
  if (linesLeft === undefined)
    linesLeft = parseInt(line) * 2;
  else{
    if (linesLeft % 2 === 0)
        last = line;
    else
        solution(last, line);
    if (!(--linesLeft > 0))
        readline.close();
  }
}).on("close",() => {
  console.log(result)
  process.exit(0);
});

function solution (first, second){
    first = [...first];
    second = [...second];
    result += (first.some(ch => second.includes(ch)) ? 'yes' : 'no') + '\n';
}

'use strict'

readline();
let s,t;
const fn = (s, t) => {
    for (let i of s) { if (t.has(i)) return 'YES'; }
    return 'NO';
}
while((s=readline()) && (t=readline())) {
    write(fn(new Set(s.split('')), new Set(s.split(''))));
}

'use strict'

readline();
let s,t;
const fn = (s, t) => {
    for (let i of s) { if (t.has(i)) return 'YES'; }
    return 'NO';
}
while((s=readline()) && (t=readline())) {
    write(fn(new Set(s.split('')), new Set(s.split(''))) + '\n');
}

n = +readline()
var q;
for (var i = 0; i < n;i ++){
    q = +readline();
    if (q === 1) { write(2)}
    else if (q === 2) { write(2)}
    else {
        if (q % 2 === 0){
            write(0)
        }
        else {write(1)}
    }
}

var q = readline();
var s = readline();
var t = readline();
var l = 0;
for(var i = 0; i<q; i++) {
    for(var i1 = 0; i1<q.length; i1++) {
        if(s.indexOf(q.slice(i1, i1+1) != -1)) l++
    }
   for(var i1 = 0; i1<q.length; i1++) {
        if(q.indexOf(s.slice(i1, i1+1) != -1)) l++
    }

    if(l>0) print('YES')
    else print('NO')
}


You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.The square still has a rectangular shape of $$$n \times m$$$ meters. However, the picture is about to get more complicated now. Let $$$a_{i,j}$$$ be the $$$j$$$-th square in the $$$i$$$-th row of the pavement.You are given the picture of the squares:  if $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black;  if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles:  $$$1 \times 1$$$ tiles — each tile costs $$$x$$$ burles and covers exactly $$$1$$$ square;  $$$1 \times 2$$$ tiles — each tile costs $$$y$$$ burles and covers exactly $$$2$$$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $$$1 \times 1$$$ tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.What is the smallest total price of the tiles needed to cover all the white squares?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of testcases. Then the description of $$$t$$$ testcases follow. The first line of each testcase contains four integers $$$n$$$, $$$m$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 100$$$; $$$1 \le m \le 1000$$$; $$$1 \le x, y \le 1000$$$) — the size of the Theatre square, the price of the $$$1 \times 1$$$ tile and the price of the $$$1 \times 2$$$ tile. Each of the next $$$n$$$ lines contains $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line is $$$a_{i,j}$$$. If $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black, and if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white. It's guaranteed that the sum of $$$n \times m$$$ over all testcases doesn't exceed $$$10^5$$$.

For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.

NoteIn the first testcase you are required to use a single $$$1 \times 1$$$ tile, even though $$$1 \times 2$$$ tile is cheaper. So the total price is $$$10$$$ burles.In the second testcase you can either use two $$$1 \times 1$$$ tiles and spend $$$20$$$ burles or use a single $$$1 \times 2$$$ tile and spend $$$1$$$ burle. The second option is cheaper, thus the answer is $$$1$$$.The third testcase shows that you can't rotate $$$1 \times 2$$$ tiles. You still have to use two $$$1 \times 1$$$ tiles for the total price of $$$20$$$.In the fourth testcase the cheapest way is to use $$$1 \times 1$$$ tiles everywhere. The total cost is $$$6 \cdot 3 = 18$$$.

var t = parseInt(readline());
for (var tc = 0; tc < t; tc++) {
  var input = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var n = input[0],
    m = input[1],
    x = input[2],
    y = input[3];
  var cost = 0;
  var min = Math.min(2 * x, y);

  for (var i = 0; i < n; i++) {
    var squares = readline().split("");
    for (var j = 0; j < squares.length; j++) {
      if (squares[j] == ".") {
        if (squares[j + 1] == ".") {
          if (2 * x > y) {
            cost += y;
            j++;
            continue;
          } else cost += x;
        } else cost += x;
      }
    }
  }
  print(cost);
}

var testCases = readline();
for(var i =0; i< testCases; i++) {
    var info =  readline().split(" ");
    var oneCost = info[2];
    var twoCost = info[3];
    var cost = 0;
    for(var j =0; j< info[0]; j++) {
        var row = readline();
        var dotGroups =  row.match(/\.+/g) || [];
          for(var k =0; k< dotGroups.length; k++) {
              var groupLength = dotGroups[k].length;
              var currCost = groupLength*oneCost;
              if(twoCost < (oneCost * 2)) {
                  var t = Math.floor(groupLength/2);
                  currCost = t*twoCost + (groupLength - t*2)*oneCost;
              }
              cost+= currCost;
           }
    }
    print(cost);
}

//↓見なくていいよ！ここから--------------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
var obj;
var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  myerr("-----start-----");
  var start = new Date();
  Main();
  var end = new Date() - start;
  myerr("----- end -----");
  myerr("time : " + (end) + "ms");
  myerr("memory : " + Math.round(process.memoryUsage().heapTotal / 1024) + "KB");
});
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  var returnObj = {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
  };
  return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割  7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}

//↑見なくていいよ！ここまで--------------------------------------------------
function Main(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var one = nextIntArray();
		var n = one[0];
		var m = one[1];
		var x = one[2];
		var y = one[3];
		var list = new Array(n);
		for(var j = 0; j < n; j++){
			list[j] = nextCharArray();
		}
		var cost = 0;
		if(x * 2 > y){
			for(var j = 0; j < n; j++){
				list[j].push("*");
				for(var k = 0; k < m; k++){
					if(list[j][k] == "." && list[j][k + 1] == "."){
						cost += y;
						k++;
					}else if(list[j][k] == "." && list[j][k + 1] == "*"){
						cost += x;
					}
				}
			}
		}else{
			for(var j = 0; j < n; j++){
				for(var k = 0; k < m; k++){
					if(list[j][k] == "."){
						cost += x;
					}
				}
			}
		}
		output[i] = cost;
	}
	myout(myconv(output,9));
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = [];
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString.push(inputStdin);
});

process.stdin.on('end', () => {
    inputString = inputString.join('').trim().split('\n').map(string => string.trim());

    //withTiming(main);
    main();
});

const readline = () => inputString[currentLine++];

const withTiming = callback => {
    const start = new Date();
    callback();
    console.log(`Took ${new Date() - start} ms`);
}

const main = () => {
    const t = parseInt(readline());

    for (let k = 0; k < t; k++) {
        const [n, m, x, y] = readline().split(' ').map(x => parseInt(x));
        const tiles = [];
        let i = 0, j = 0, cost = 0;

        for (let j = 0; j < n; j++) {
            tiles.push(readline());
        }

        while (i < n) {
            while (j < m) {
                if (tiles[i][j] === '.') {
                    if (y < x * 2 && j !== m - 1 && tiles[i][j + 1] === '.') {
                        cost += y;
                        j += 2;
                    } else {
                        cost += x;
                        j++;
                    }
                } else {
                    j++;
                }
            }

            j = 0;
            i++;
        }

        console.log(cost);
    }
}

const processData = (lines) => {
  let acc = 0
  const n = lines[acc]
  acc++
  for (let i=0; i<n; i++) {
    const [n, m, o, t] = lines[acc].split(' ').map(x => +x)
    acc++
    let sum = 0
    for (let j=0; j<n; j++) {
      let tSum = 0
      let oSum = 0
      for (const s of lines[acc].split('*')) {
        oSum += s.length * o
        tSum += Math.floor(s.length / 2) * t + (s.length % 2) * o
      }
      sum += Math.min(tSum, oSum)
      acc++
    }
    console.log(sum)
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

const calculateLine = (line, oneCellCost, twoCellsCost) => {
  let ret = 0;
  for(let i = 0; i < line.length; i++) {
    if (line[i] == '.') {
      if (line[i+1] == '.') {
        if (twoCellsCost < oneCellCost * 2) {
          ret += twoCellsCost;
          i++;
        } else {
          ret += oneCellCost;
        }
      } else {
        ret += oneCellCost;
      }
    }
  }
  return ret;
}

let tests = true;
let index = 0, ret = 0;
let rows, cols, oneCellCost, twoCellsCost;
readline.on('line', line => {
  if (tests) { tests = false; return; }
  const l = line.split(' ');
  if (l.length > 1) {
    rows = parseInt(l[0]); cols = parseInt(l[1]);
    oneCellCost = parseInt(l[2]); twoCellsCost = parseInt(l[3]);
    index = 0; ret = 0;
  } else if (index++ < rows) {
    ret += calculateLine(line, oneCellCost, twoCellsCost);
    if (index === rows) {
      console.log(ret);
    }
  }
});

/**
4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
*..
.*.

*/

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = [];
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString.push(inputStdin);
});

process.stdin.on('end', () => {
    inputString = inputString.join('').trim().split('\n').map(string => string.trim());

    //withTiming(main);
    main();
});

const readline = () => inputString[currentLine++];

const withTiming = callback => {
    const start = new Date();
    callback();
    console.log(`Took ${new Date() - start} ms`);
}

const main = () => {
    const t = parseInt(readline());

    for (let k = 0; k < t; k++) {
        const [n, m, x, y] = readline().split(' ').map(x => parseInt(x));
        const tiles = [];
        let i = 0, j = 0, cost = 0;

        for (let j = 0; j < n; j++) {
            tiles.push(readline());
        }

        while (i < n) {
            while (j < m) {
                if (tiles[i][j] === '.') {
                    if (y < x * 2 && j !== m - 1) {
                        cost += y;
                        j += 2;
                    } else {
                        cost += x;
                        j++;
                    }
                } else {
                    j++;
                }
            }

            j = 0;
            i++;
        }

        console.log(cost);
    }
}

const processData = (lines) => {
  let acc = 0
  const n = lines[acc]
  acc++
  for (let i=0; i<n; i++) {
    const [n, m, o, t] = lines[acc].split(' ').map(x => +x)
    acc++
    let sum = 0
    for (let j=0; j<n; j++) {
      let tSum = 0
      let oSum = 0
      for (const s of lines[acc].split('*')) {
        oSum += s.length * o
        tSum += Math.ceil(s.length / 2) * t + (s.length % 2) * o
      }
      sum += Math.min(tSum, oSum)
      acc++
    }
    console.log(sum)
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})


On a beach there are $$$n$$$ huts in a perfect line, hut $$$1$$$ being at the left and hut $$$i+1$$$ being $$$100$$$ meters to the right of hut $$$i$$$, for all $$$1 \le i \le n - 1$$$. In hut $$$i$$$ there are $$$p_i$$$ people.There are $$$m$$$ ice cream sellers, also aligned in a perfect line with all the huts. The $$$i$$$-th ice cream seller has their shop $$$x_i$$$ meters to the right of the first hut. All ice cream shops are at distinct locations, but they may be at the same location as a hut.You want to open a new ice cream shop and you wonder what the best location for your shop is. You can place your ice cream shop anywhere on the beach (not necessarily at an integer distance from the first hut) as long as it is aligned with the huts and the other ice cream shops, even if there is already another ice cream shop or a hut at that location. You know that people would come to your shop only if it is strictly closer to their hut than any other ice cream shop.If every person living in the huts wants to buy exactly one ice cream, what is the maximum number of ice creams that you can sell if you place the shop optimally?

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 200\,000$$$, $$$1 \le m \le 200\,000$$$) — the number of huts and the number of ice cream sellers. The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le 10^9$$$) — the number of people in each hut. The third line contains $$$m$$$ integers $$$x_1, x_2, \ldots, x_m$$$ ($$$0 \le x_i \le 10^9$$$, $$$x_i \ne x_j$$$ for $$$i \ne j$$$) — the location of each ice cream shop.

Print the maximum number of ice creams that can be sold by choosing optimally the location of the new shop.

NoteIn the first sample, you can place the shop (coloured orange in the picture below) $$$150$$$ meters to the right of the first hut (for example) so that it is the closest shop to the first two huts, which have $$$2$$$ and $$$5$$$ people, for a total of $$$7$$$ sold ice creams.  In the second sample, you can place the shop $$$170$$$ meters to the right of the first hut (for example) so that it is the closest shop to the last two huts, which have $$$7$$$ and $$$8$$$ people, for a total of $$$15$$$ sold ice creams.

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (j >= m) {
      var _sum2;
      res = Math.max(res, sum[n - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    }
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    while (j < m && i * 100 > b[j]) j++;
    if (j >= m) {
      var _sum3;
      res = Math.max(res, sum[n - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
      break;
    }
    if (j === 0) {
      res = Math.max(res, sum[i]);
      continue;
    } else {
      var _sum$r, _sum5;
      const x = b[j] - 0.5;
      const l = 2 * i * 100 - (b[j - 1] + 0.5);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      if (r >= n) {
        var _sum4;
        res = Math.max(res, sum[n - 1] - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0));
        break;
      }
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[n - 1]) - ((_sum5 = sum[i - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0));
    }
  }
  console.log(res);
  return res;
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0n) + BigInt(v);
  }
  let res = 0n;
  function max(a, b) {
    return a > b ? a : b;
  }
  for (let i = 0, j = 0; i < n; i++) {
    if (j >= m) {
      var _sum2;
      res = max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0n));
      break;
    }
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j >= m) {
      var _sum3;
      res = max(res, sum[sum.length - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0n));
      break;
    }
    if (j === 0) {
      res = max(res, sum[i]);
      continue;
    } else {
      var _sum$r, _sum5;
      const x = b[j] - 1;
      const l = 2 * i * 100 - (b[j - 1] + 1);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      if (r >= n) {
        var _sum4;
        res = max(res, sum[sum.length - 1] - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0n));
        break;
      }
      res = max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum5 = sum[i - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0n));
    }
  }
  console.log(res.toString());
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0n) + BigInt(v);
  }
  let res = 0n;
  function max(a, b) {
    return a > b ? a : b;
  }
  for (let i = 0, j = 0; i < n; i++) {
    if (j >= m) {
      var _sum2;
      res = max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0n));
      break;
    }
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j >= m) {
      var _sum3;
      res = max(res, sum[sum.length - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0n));
      break;
    }
    if (j === 0) {
      res = max(res, sum[i]);
      continue;
    } else {
      var _sum$r, _sum5;
      const x = b[j] - 0.5;
      const l = 2 * i * 100 - (b[j - 1] + 0.5);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      if (r >= n) {
        var _sum4;
        res = max(res, sum[sum.length - 1] - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0n));
        break;
      }
      res = max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum5 = sum[i - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0n));
    }
  }
  console.log(res.toString());
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0n) + BigInt(v);
  }
  let res = 0n;
  function max(a, b) {
    return a > b ? a : b;
  }
  for (let i = 0, j = 0; i < n; i++) {
    if (j >= m) {
      var _sum2;
      res = max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0n));
      break;
    }
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j >= m) {
      var _sum3;
      res = max(res, sum[sum.length - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0n));
      break;
    }
    if (j === 0) {
      res = max(res, sum[i]);
      continue;
    } else {
      var _sum$r, _sum5;
      const x = b[j] - 0.1;
      const l = 2 * i * 100 - (b[j - 1] + 0.1);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      if (r >= n) {
        var _sum4;
        res = max(res, sum[sum.length - 1] - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0n));
        break;
      }
      res = max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum5 = sum[i - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0n));
    }
  }
  console.log(res.toString());
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (j >= m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    }
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j >= m) {
      var _sum3;
      res = Math.max(res, sum[sum.length - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
      break;
    }
    if (j === 0) {
      res = Math.max(res, sum[i]);
      continue;
    } else {
      var _sum$r, _sum5;
      const x = b[j] - 0.1;
      const l = 2 * i * 100 - (b[j - 1] + 0.1);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      if (r >= n) {
        var _sum4;
        res = Math.max(res, sum[sum.length - 1] - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0));
        break;
      }
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum5 = sum[i - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === 0) {
      res = Math.max(res, sum[i]);
      continue;
    } else if (j >= m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    } else {
      var _sum$r, _sum3;
      const x = b[j] - 0.5;
      const l = 2 * i * 100 - (b[j - 1] + 0.5);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === 0) {
      res = Math.max(res, sum[i]);
      continue;
    } else if (j >= m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    } else {
      var _sum$r, _sum3;
      const x = b[j] - 1;
      const l = 2 * i * 100 - (b[j - 1] + 1);
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0; i < n; i++) {
    if (i * 100 >= b[0]) break;
    res = sum[i];
  }
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === 0) {
      continue;
    } else if (j >= m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    } else {
      var _sum$r, _sum3;
      const x = b[j] - 0.5;
      const l = j === 0 ? x : 2 * i * 100 - b[j - 1];
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j >= m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    } else {
      var _sum$r, _sum3;
      const x = b[j] - 0.5;
      const l = j === 0 ? x : 2 * i * 100 - b[j - 1];
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    } else {
      var _sum$r, _sum3;
      const x = b[j] - 0.5;
      const l = j === 0 ? x : 2 * i * 100 - b[j - 1];
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 === b[j]) {
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      break;
    } else {
      var _sum$r, _sum3;
      const x = b[j] - 1;
      const l = j === 0 ? x : 2 * i * 100 - b[j - 1];
      const r = Math.min(Math.floor((x + l) / (2 * 100)), Math.floor(x / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (b[j] === i * 100) {
      var _b, _sum$r, _sum2;
      const l = j === 0 ? 0 : Math.ceil((i * 100 + b[j - 1]) / (2 * 100));
      const r = Math.min(Math.floor((((_b = b[j + 1]) !== null && _b !== void 0 ? _b : Infinity) + i * 100) / (2 * 100)), Math.floor(b[j + 1] / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum2 = sum[l - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === m) {
      var _sum3;
      res = Math.max(res, sum[sum.length - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
      break;
    } else {
      var _sum$r2, _sum4;
      const l = j === 0 ? b[j] : 2 * i * 100 - b[j - 1];
      const r = Math.min(Math.floor((b[j] + l) / (2 * 100)), Math.floor(b[j] / 100));
      res = Math.max(res, ((_sum$r2 = sum[r]) !== null && _sum$r2 !== void 0 ? _sum$r2 : sum[sum.length - 1]) - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (b[j] === i * 100) {
      var _b, _sum$r, _sum2;
      const l = j === 0 ? 0 : Math.ceil((i * 100 + b[j - 1]) / (2 * 100));
      const r = Math.min(Math.floor((((_b = b[j + 1]) !== null && _b !== void 0 ? _b : Infinity) + i * 100) / (2 * 100)), Math.floor(b[j + 1] / 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum2 = sum[l - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
      j++;
      continue;
    }
    if (i * 100 > b[j]) j++;
    if (j === m) {
      var _sum3;
      res = Math.max(res, sum[sum.length - 1] - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
      break;
    } else {
      var _b2, _sum$r2, _sum4;
      const l = 2 * i * 100 - ((_b2 = b[j - 1]) !== null && _b2 !== void 0 ? _b2 : 0);
      const r = Math.min(Math.floor((b[j] + l) / (2 * 100)), Math.floor(b[j] / 100));
      res = Math.max(res, ((_sum$r2 = sum[r]) !== null && _sum$r2 !== void 0 ? _sum$r2 : sum[sum.length - 1]) - ((_sum4 = sum[i - 1]) !== null && _sum4 !== void 0 ? _sum4 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 > b[j]) j++;
    if (j === 0) {
      res = Math.max(res, sum[i]);
    } else if (j === m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
    } else {
      var _sum$r, _sum3;
      const l = 2 * i * 100 - b[j - 1];
      const r = Math.floor((b[j] + l) / (2 * 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, a, b) {
  console.log(n, m, a, b);
  b.sort((a, b) => a - b);
  const sum = [];
  for (let [i, v] of a.entries()) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + v;
  }
  let res = 0;
  for (let i = 0, j = 0; i < n; i++) {
    if (i * 100 > b[j]) j++;
    if (j === 0) {
      res = Math.max(res, sum[i]);
    } else if (j === m) {
      var _sum2;
      res = Math.max(res, sum[sum.length - 1] - ((_sum2 = sum[i - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
    } else {
      var _sum$r, _sum3;
      const l = 2 * i * 100 - b[j - 1];
      const r = Math.floor((b[j] + l) / (2 * 100));
      res = Math.max(res, ((_sum$r = sum[r]) !== null && _sum$r !== void 0 ? _sum$r : sum[sum.length - 1]) - ((_sum3 = sum[i - 1]) !== null && _sum3 !== void 0 ? _sum3 : 0));
    }
  }
  console.log(res);
}

async function main(read) {
  const [n, m] = (await read()).split(' ').map(Number);
  const a = (await read()).split(' ').map(Number);
  const b = (await read()).split(' ').map(Number);
  solve(n, m, a, b);
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();


You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k > 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.

If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.

NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.

function solve() {
   var nm = read().split(' ').map(Number);
   var n = nm[0];
   var m = nm[1];
   var a =[];
   for (var i = 0; i < n; i++) {
      a.push(read().split(' ').map(Number));
   }
   for (var i = 0; i < n; i++) {
      for (var j = 0; j < m; j++) {
         var expect = 4;
         if (i === 0 || i === n - 1) {
            expect--;
         }
         if (j === 0 || j === m - 1) {
            expect--;
         }
         if (expect < a[i][j]) {
            write('NO');
            return;
         }
         a[i][j] = expect;
      }
   }
   write('YES');
   for (var i = 0; i < n; i++) {
      write(a[i].join(' '));
   }
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}


There are $$$n$$$ houses numbered from $$$1$$$ to $$$n$$$ on a circle. For each $$$1 \leq i \leq n - 1$$$, house $$$i$$$ and house $$$i + 1$$$ are neighbours; additionally, house $$$n$$$ and house $$$1$$$ are also neighbours.Initially, $$$m$$$ of these $$$n$$$ houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently.Every day, the following things happen in order:  Cirno chooses an uninfected house, and protect it permanently.  All uninfected, unprotected houses which have at least one infected neighbor become infected. Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect.Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of test cases follows. The first line of each test case consists of two positive integers $$$n, m$$$ ($$$5 \leq n \leq 10^9$$$, $$$1 \leq m \leq \min(n, 10^5)$$$) — the number of houses on the circle, and the number of houses that are initially infected.  The second line of each test case consists of $$$m$$$ distinct positive integers $$$a_1, a_2, \cdots , a_m$$$ ($$$1 \leq a_i \leq n$$$) — the indices of the houses infected initially. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output an integer on a separate line, which is the minimum number of infected houses in the end.

NoteIn the first test case:At the start of the first day, house $$$3$$$, $$$6$$$, $$$8$$$ are infected. Choose house $$$2$$$ to protect.At the start of the second day, house $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ are infected. Choose house $$$10$$$ to protect.At the start of the third day, no more houses are infected.In the second test case:At the start of the first day, house $$$2$$$, $$$5$$$ are infected. Choose house $$$1$$$ to protect.At the start of the second day, house $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$ are infected. No more available houses can be protected.

var tests = parseInt(readline());
    var n, m;
    var a;
    for (var t=0; t < tests; t++) {
        var nm = readline().split(' ').map(x=>parseInt(x));
        n = nm[0];
        m = nm[1];
        a = readline().split(' ').map(x=>parseInt(x));
        a.sort((x, y) => x-y);
        var diff = [];
        for (var i = 0; i < a.length-1; i++) {
            diff.push(a[i+1] - a[i] - 1);
        }
        diff.push(a[0] + n - a[m-1] - 1);
        diff.sort((x, y) => y-x);
        var saved = 0;
        for (var i = 0; i < m; i++) {
            var si = diff[i] - (4 * i);
            var s;
            if (si == 1) {
                s = 1;
            } else {
                s = si - 1;
            }
            if (s < 1) {
                break;
            }
            saved += s;
        }
        print(n - saved);
    }


Ayush and Ashish play a game on an unrooted tree consisting of $$$n$$$ nodes numbered $$$1$$$ to $$$n$$$. Players make the following move in turns:   Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $$$1$$$. A tree is a connected undirected graph without cycles.There is a special node numbered $$$x$$$. The player who removes this node wins the game. Ayush moves first. Determine the winner of the game if each player plays optimally.

The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of testcases. The description of the test cases follows. The first line of each testcase contains two integers $$$n$$$ and $$$x$$$ $$$(1\leq n \leq 1000, 1 \leq x \leq n)$$$ — the number of nodes in the tree and the special node respectively. Each of the next $$$n-1$$$ lines contain two integers $$$u$$$, $$$v$$$ $$$(1 \leq u, v \leq n, \text{ } u \ne v)$$$, meaning that there is an edge between nodes $$$u$$$ and $$$v$$$ in the tree.

For every test case, if Ayush wins the game, print "Ayush", otherwise print "Ashish" (without quotes).

NoteFor the $$$1$$$st test case, Ayush can only remove node $$$2$$$ or $$$3$$$, after which node $$$1$$$ becomes a leaf node and Ashish can remove it in his turn.For the $$$2$$$nd test case, Ayush can remove node $$$2$$$ in the first move itself.

function mlt() { return readline().split(' ').map(x => parseInt(x)) }

function solv() {

    var inp = mlt()
    var x = inp[0]
    var y = inp[1]
    var vis = 0;
    for (var n = 0; n < x - 1; n++) {
        var px = mlt();

        if (px[0] == y || px[1] == y) vis++;
    }
    if (vis == 1 || x == 1) {
        print('Ayush')
        return;
    }
    else {
        x--;
        var st = ['Ashish', 'Ayush']
        print(st[x % 2])
    }
}

var tc = 1;
tc = parseInt(readline());
while (tc--) solv()

"use strict";
var T = parseInt(readline())
for(let t = 0 ; t < T ; t++){
    let n_x = readline().split(' ')
    let n = parseInt(n_x[0])
    let x = parseInt(n_x[1])
    let cnt = 0;
    for(let i = 0 ; i < n -1 ; i++){
        let u_v = readline().split(' ')
        let u = parseInt(u_v[0]);
        let v = parseInt(u_v[1]);
        if (u == x || v == x) cnt++
    }
    if (cnt <= 1)
        print('Ayush')
    else if (n % 2 == 0)
        print('Ayush')
    else
        print('Ashish')
}

function mlt() { return readline().split(' ').map(x => parseInt(x)) }

function solv() {

    var inp = mlt()
    var x = inp[0]
    var y = inp[1]
    var vis = 0;
    for (var n = 0; n < x - 1; n++) {
        var px = mlt();

        if (px[0] == y || px[1] == y) vis++;
    }
    if (vis == 1) {
        print('Ayush')
        return;
    }
    else {
        x--;
        var st = ['Ashish', 'Ayush']
        print(st[x % 2])
    }
}

var tc = 1;
tc = parseInt(readline());
while (tc--) solv()


Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group.

The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.

Print the smallest possible absolute difference in the first line. In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.

NoteIn the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.

//var print = require('./print');
//var readline = require('./readline');

var n = parseInt(readline());

var t1 = [];
var t2 = [];
var s1 = 0;
var s2 = 0;
var i=n;
for(; i>1; i-=2){
  if(s1>s2){
    s2+=i;
    t2.push(i);
    s1+=i-1;
    t1.push(i-1);
  }
  else{
      s2+=i-1;
      t2.push(i-1);
      s1+=i;
      t1.push(i);
  }
}
if(i){
  if(s1>s2){
    s2+=i;
    t2.push(i);
  }
  else{
      s1+=i;
      t1.push(i);
  }
}
print(Math.abs(s1-s2));
var res = '';
for(var i=0;i<t1.length;i++){
  res+=" "+t1[i];
}
print(t1.length+res);


In this task you need to process a set of stock exchange orders and use them to create order book.An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order.All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.You are given n stock exhange orders. Your task is to print order book of depth s for these orders.

The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order.

Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.

NoteDenote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.

/// <reference path="typings/d8/d8.d.ts" />
function words() {
    return readline().split(' ');
}
function ints() {
    return words().map(function (x) { return parseInt(x); });
}
var _a = ints(), n = _a[0], s = _a[1];
var bid = [];
var ask = [];
for (var i = 0; i < n; i++) {
    var _b = words(), d = _b[0], ps = _b[1], qs = _b[2];
    var p = parseInt(ps);
    var q = parseInt(qs);
    if (d == "B") {
        bid[p] = bid[p] || 0;
        bid[p] += q;
    }
    else {
        ask[p] = ask[p] || 0;
        ask[p] += q;
    }
}
function report(s, kind, order) {
    var output = [];
    order.forEach(function (x, i) { return output.push(i); });
    if (kind == "B")
        output.reverse();
    output = output.slice(0, s);
    if (kind == "S")
        output.reverse();
    for (var i = 0; i < output.length; i++) {
        print(kind + " " + output[i] + " " + order[output[i]]);
    }
}
report(s, 'S', ask);
report(s, 'B', bid);

/// <reference path="typings/d8/d8.d.ts" />
function words() {
    return readline().split(' ');
}
function ints() {
    return words().map(function (x) { return parseInt(x); });
}
var _a = ints(), n = _a[0], s = _a[1];
var bid = [];
var ask = [];
for (var i = 0; i < n; i++) {
    var _b = words(), d = _b[0], ps = _b[1], qs = _b[2];
    var p = parseInt(ps);
    var q = parseInt(qs);
    if (d == "B") {
        bid[p] = bid[p] || 0;
        bid[p] += q;
    }
    else {
        ask[p] = ask[p] || 0;
        ask[p] += q;
    }
}
function report(s, kind, order) {
    var output = [];
    order.forEach(function (x, i) { return output.push(i); });
    output.reverse();
    for (var i = 0; i < s && i < output.length; i++) {
        print(kind + " " + output[i] + " " + order[output[i]]);
    }
}
report(s, 'S', ask);
report(s, 'B', bid);