{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":" \n \u003cp\u003eRainbow loves to play kinds of War Chess games. There are many famous War Chess games such as \"Biography of Cao Cao\", \"Anecdotes of Wagang Mountain\", etc. In this problem, let\u0027s consider a much simpler version of War Chess games. The game is based on the following features:\u003c/p\u003e \n \u003cp\u003e\u003cstrong\u003eMAP\u003c/strong\u003e\u003c/p\u003e \n \u003cp\u003eThe map is a rectangular area which consists of N rows and M columns. The cells in the rectangular area have various landforms like plain, woods, hill, river, snowfield, etc. Specifically, each cell (i,j) is assigned an integer of moving property G\u003csub\u003ei,j\u003c/sub\u003e, which makes influence on going across it.\u003c/p\u003e \n \u003cp\u003e\u003cstrong\u003eCHARACTER\u003c/strong\u003e\u003c/p\u003e \n \u003cp\u003eA character can be regarded as a person standing in a cell of the map. Each character has life HP, attacking ability AT, moving ability MV and attacking distance range [AD\u003csub\u003e1\u003c/sub\u003e,AD\u003csub\u003e2\u003c/sub\u003e]. Characters are divided into two groups. Group 0 is controlled by the player and group 1 is controlled by the system.\u003c/p\u003e \n \u003cp\u003eA character can attack a character from the \u003cstrong\u003eopposite\u003c/strong\u003e group if the Manhattan distance between them is in the attacker\u0027s attacking distance range (\u003cstrong\u003einclusive\u003c/strong\u003e). \u0026nbsp;When a character p is attacked by enemy q, its life HP\u003csub\u003ep\u003c/sub\u003e will be minus by AT\u003csub\u003eq\u003c/sub\u003e. A character is driven out of the map \u003cstrong\u003eimmediately\u003c/strong\u003e when its HP becomes 0 or negative.\u003c/p\u003e \n \u003cp\u003eEach character can occupy a cell or move on the map. Several rules specify the position and moving limits of them:\u003c/p\u003e \n \u003cp\u003e\u003cstrong\u003eOCCUPATION\u003c/strong\u003e\u003c/p\u003e \n \u003cp\u003eInitially, characters are generated in different cells. They occupy their own cell separately. A character cannot move when another one is moving. \u003c/p\u003e \n \u003cp\u003eOnce a character starts to move, it will lose occupation of its current standing cell. When the character stops moving, it will gain the occupation of the cell it is standing in.\u003c/p\u003e \n \u003cp\u003e\u003cstrong\u003eSTEP\u003c/strong\u003e\u003c/p\u003e \n \u003cp\u003eA \u003cstrong\u003emove\u003c/strong\u003e consists of several \u003cstrong\u003esteps\u003c/strong\u003e. Each step, one can walk from a cell to an adjacent cell sharing an edge (up, down, left or right). A step walking to a cell occupied by \u003cstrong\u003ethe other character\u003c/strong\u003e is invalid.\u003c/p\u003e \n \u003cp\u003eWhen it steps from a cell (i,j) to a cell (x,y), its moving ability MV will be minus by G\u003csub\u003ex,y\u003c/sub\u003e. Moving steps which make MV become negative are invalid. After a valid step, if the current cell is \u003cstrong\u003ebeside\u003c/strong\u003e (sharing an edge with) someone of the \u003cstrong\u003eopposite\u003c/strong\u003e group, the MV of a character will become 0.\u003c/p\u003e \n \u003cp\u003eA character\u0027s MV recovers to the original value immediately when it gains occupation of a cell.\u003c/p\u003e \n \u003cp\u003eThe set of cells, able to be reached by valid steps and stopped at in a move, is called a character’s moving range. Staying at the original cell is also valid and should be counted into the moving range.\u003c/p\u003e \n \u003cp\u003e\u003cimg width\u003d\"629\" height\u003d\"464\" src\u003d\"CDN_BASE_URL/3c6f6d0c6f3fdba40e7644c0ef49d5d7?v\u003d1659750678\"\u003e\u003c/p\u003e \n \u003cp\u003eThe process of the game contains several rounds. In each round, the player\u0027s group acts first and the system\u0027s group acts next. For each group, the characters of the group act by turns. In each action, a character first moves to a cell in its moving range, then attacks a character of the opposite group in its attacking distance range or do nothing. When all the characters of a group are driven out of the map, the group loses the game and the opposite group wins.\u003c/p\u003e \n \u003cp\u003eRainbow has just got some logs of a game and wants to watch the game. Help him to recover the game process.\u003cbr\u003e\u003c/p\u003e \n "}},{"title":"Input","value":{"format":"HTML","content":" \n \u003cp\u003eThere are multiple test cases (no more than 20). For each test case:\u003c/p\u003e \n \u003cp\u003eThe first line contains three integers N,M,C,E (1≤N,M≤100,2≤C≤min(N*M,100),1≤E≤3000), representing the number of rows and columns, the number of characters and the number of events in the log.\u003c/p\u003e \n \u003cp\u003eEach of the next N lines contains M integers, representing the moving property G\u003csub\u003ei,j\u003c/sub\u003e (1≤G\u003csub\u003ei,j\u003c/sub\u003e≤10\u003csup\u003e2\u003c/sup\u003e ).\u003c/p\u003e \n \u003cp\u003eEach of the next C lines contains eight integers HP, AT, MV, AD\u003csub\u003e1\u003c/sub\u003e, AD\u003csub\u003e2\u003c/sub\u003e, ST\u003csub\u003ex\u003c/sub\u003e, ST\u003csub\u003ey\u003c/sub\u003e, GR,(1≤HP,AT,MV≤10\u003csup\u003e3\u003c/sup\u003e,1≤AD\u003csub\u003e1\u003c/sub\u003e≤AD\u003csub\u003e2\u003c/sub\u003e≤20,1\u0026lt;ST\u003csub\u003ex\u003c/sub\u003e≤N,1≤ST\u003csub\u003ey\u003c/sub\u003e≤M,0≤GR≤1), representing the five properties, the initial position and the group of a character.\u003c/p\u003e \n \u003cp\u003eThe next E lines describe events in the log. Each line is in one of the following formats:\u003c/p\u003e \n \u003cp\u003e\u003cem\u003eRound of GR\u003c/em\u003e – Characters in group GR take the round.\u003c/p\u003e \n \u003cp\u003e\u003cem\u003eAction of character ID\u003c/em\u003e – Character ID takes the action.\u003c/p\u003e \n \u003cp\u003e\u003cem\u003eMove to (x,y)\u003c/em\u003e – Current acting character moves to cell (x,y) by some steps.\u003c/p\u003e \n \u003cp\u003e\u003cem\u003eAttack ID\u003c/em\u003e – Current acting character attacks character ID but ID isn’t driven out.\u003c/p\u003e \n \u003cp\u003e\u003cem\u003eDrive out ID\u003c/em\u003e – Current acting character attacks character ID and ID is driven out.\u003c/p\u003e \n \u003cp\u003eIn the events, 1≤x≤N,1≤y≤M,1≤ID≤C,0≤GR≤1. The row number of cells is marked 1 to N from top to bottom, the column number is marked 1 to M from left to right. Initially, each group has at least one character.\u003cbr\u003e\u003c/p\u003e \n "}},{"title":"Output","value":{"format":"HTML","content":" \n \u003cp\u003eFor each event of Move to, output \"INVALID\" if breaking the moving rules. Otherwise output the maximum rest moving ability when it stepped into the destination.\u003c/p\u003e \n \u003cp\u003eFor each event of Attack or Drive out, output \"INVALID\" if cannot attack or being wrong at driving out. Otherwise, output the remaining HP of the one being attacked.\u003c/p\u003e \n \u003cp\u003eInvalid events should be \u003cstrong\u003eignored\u003c/strong\u003e while dealing with the next events. The data ensures that the game has just finished after processing all the valid events. Events of \u003cem\u003eRound of\u003c/em\u003e and \u003cem\u003eAction of\u003c/em\u003e are ensured to be well recorded in the log without missing or breaking.\u003cbr\u003e\u003c/p\u003e \n \u003c/div\u003e \n "}},{"title":"Sample Input","value":{"format":"HTML","content":" \n \u003cpre\u003e5 5 4 12\r\n1 9 1 4 4\r\n1 9 1 2 4\r\n1 9 1 1 2\r\n2 9 2 7 3\r\n2 3 2 6 1\r\n10 5 8 1 3 1 1 0\r\n20 10 5 1 2 2 1 0\r\n19 10 5 1 2 5 2 1\r\n25 25 3 1 1 5 5 0\r\nRound of 0\r\nAction of character 2\r\nMove to (5,1)\r\nAttack 3\r\nAction of character 1\r\nMove to (3,1)\r\nRound of 1\r\nAction of character 3\r\nDrive out 1\r\nRound of 0\r\nAction of character 2\r\nDrive out 3\u003c/pre\u003e \n "}},{"title":"Sample Output","value":{"format":"HTML","content":" \n \u003cpre\u003e0\r\n9\r\n6\r\nINVALID\r\n-1\u003c/pre\u003e \n "}}]}