{"trustable":false,"sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"ptx\" lang\u003d\"en-US\"\u003e\r\n\tFarmer John\u0026#39;s pastoral neighborhood has N farms (2 \u0026lt;\u003d N \u0026lt;\u003d 40,000), usually numbered/labeled 1..N. A series of M (1 \u0026lt;\u003d M \u0026lt; 40,000) vertical and horizontal roads each of varying lengths (1 \u0026lt;\u003d length \u0026lt;\u003d 1000) connect the farms. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n):\u003cbr /\u003e\r\n\t\u003cpre\u003e\r\n\t\u003cspan data-scayt_word\u003d\"F1\" data-scaytid\u003d\"1\"\u003eF1\u003c/span\u003e --- (13) ---- \u003cspan data-scayt_word\u003d\"F6\" data-scaytid\u003d\"15\"\u003eF6\u003c/span\u003e --- (9) ----- \u003cspan data-scayt_word\u003d\"F3\" data-scaytid\u003d\"16\"\u003eF3\u003c/span\u003e\r\n\r\n | |\r\n\r\n (3) |\r\n\r\n | (7)\r\n\r\n \u003cspan data-scayt_word\u003d\"F4\" data-scaytid\u003d\"17\"\u003eF4\u003c/span\u003e --- (20) -------- \u003cspan data-scayt_word\u003d\"F2\" data-scaytid\u003d\"2\"\u003eF2\u003c/span\u003e |\r\n\r\n | |\r\n\r\n (2) \u003cspan data-scayt_word\u003d\"F5\" data-scaytid\u003d\"19\"\u003eF5\u003c/span\u003e\r\n\r\n | \r\n\r\n \u003cspan data-scayt_word\u003d\"F7\" data-scaytid\u003d\"20\"\u003eF7\u003c/span\u003e \u003c/pre\u003e\r\n\t\u003cbr /\u003e\r\n\tBeing an ASCII diagram, it is not precisely to scale, of course.\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\tEach farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path\u003cbr /\u003e\r\n\t(sequence of roads) links every pair of farms.\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\t\u003cspan data-scayt_word\u003d\"FJ\" data-scaytid\u003d\"28\"\u003eFJ\u003c/span\u003e lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road:\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\tThere is a road of length 10 running north from Farm #23 to Farm #17\u003cbr /\u003e\r\n\tThere is a road of length 7 running east from Farm #1 to Farm #17\u003cbr /\u003e\r\n\t...\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\tAs \u003cspan data-scayt_word\u003d\"FJ\" data-scaytid\u003d\"37\"\u003eFJ\u003c/span\u003e is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\tWhat is the Manhattan distance between farms #1 and #23?\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\tFJ answers Bob, when he can (sometimes he doesn\u0026#39;t yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the \u0026quot;Manhattan\u0026quot; distance between the pair of farms.\u003cbr /\u003e\r\n\tThe Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points).\u003cbr /\u003e\r\n\t\u003cbr /\u003e\r\n\tWhen Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with \u0026quot;-1\u0026quot;.\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cdiv class\u003d\"ptx\" lang\u003d\"en-US\"\u003e\r\n\t\u003cpre\u003e\r\n\t* Line 1: Two space-separated integers: N and M\r\n\r\n\r\n\r\n* Lines 2..M+1: Each line contains four space-separated entities, \u003cspan data-scayt_word\u003d\"F1\" data-scaytid\u003d\"3\"\u003eF1\u003c/span\u003e,\r\n\r\n \u003cspan data-scayt_word\u003d\"F2\" data-scaytid\u003d\"8\"\u003eF2\u003c/span\u003e, L, and D that describe a road. \u003cspan data-scayt_word\u003d\"F1\" data-scaytid\u003d\"4\"\u003eF1\u003c/span\u003e and \u003cspan data-scayt_word\u003d\"F2\" data-scaytid\u003d\"9\"\u003eF2\u003c/span\u003e are numbers of\r\n\r\n two farms connected by a road, L is its length, and D is a\r\n\r\n character that is either \u0026#39;N\u0026#39;, \u0026#39;E\u0026#39;, \u0026#39;S\u0026#39;, or \u0026#39;W\u0026#39; giving the\r\n\r\n direction of the road from \u003cspan data-scayt_word\u003d\"F1\" data-scaytid\u003d\"5\"\u003eF1\u003c/span\u003e to \u003cspan data-scayt_word\u003d\"F2\" data-scaytid\u003d\"10\"\u003eF2\u003c/span\u003e.\r\n\r\n\r\n\r\n* Line M+2: A single integer, K (1 \u0026lt;\u003d K \u0026lt;\u003d 10,000), the number of FB\u0026#39;s\r\n\r\n queries\r\n\r\n\r\n\r\n* Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob\r\n\r\n and contains three space-separated integers: \u003cspan data-scayt_word\u003d\"F1\" data-scaytid\u003d\"6\"\u003eF1\u003c/span\u003e, \u003cspan data-scayt_word\u003d\"F2\" data-scaytid\u003d\"11\"\u003eF2\u003c/span\u003e, and I. \u003cspan data-scayt_word\u003d\"F1\" data-scaytid\u003d\"7\"\u003eF1\u003c/span\u003e\r\n\r\n and \u003cspan data-scayt_word\u003d\"F2\" data-scaytid\u003d\"12\"\u003eF2\u003c/span\u003e are numbers of the two farms in the query and I is the\r\n\r\n index (1 \u0026lt;\u003d I \u0026lt;\u003d M) in the data after which Bob asks the\r\n\r\n query. Data index 1 is on line 2 of the input data, and so on.\u003c/pre\u003e\r\n\u003c/div\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cdiv class\u003d\"ptx\" lang\u003d\"en-US\"\u003e\r\n\t\u003cpre\u003e\r\n\t* Lines 1..K: One integer per line, the response to each of Bob\u0026#39;s\r\n\r\n queries. Each line should contain either a distance\r\n\r\n measurement or -1, if it is impossible to determine the\r\n\r\n appropriate distance.\u003c/pre\u003e\r\n\u003c/div\u003e"}},{"title":"Sample Input","value":{"format":"HTML","content":"\u003cpre class\u003d\"sio\"\u003e\r\n7 6\r\n1 6 13 E\r\n6 3 9 E\r\n3 5 7 S\r\n4 1 3 N\r\n2 4 20 W\r\n4 7 2 S\r\n3\r\n1 6 1\r\n1 4 3\r\n2 6 6\r\n\u003c/pre\u003e"}},{"title":"Sample Output","value":{"format":"HTML","content":"\u003cpre class\u003d\"sio\"\u003e\r\n13\r\n-1\r\n10\r\n\u003c/pre\u003e"}},{"title":"Hint","value":{"format":"HTML","content":"\u003cdiv class\u003d\"ptx\" lang\u003d\"en-US\"\u003e\r\n\tAt time 1, \u003cspan data-scayt_word\u003d\"FJ\" data-scaytid\u003d\"13\"\u003eFJ\u003c/span\u003e knows the distance between 1 and 6 is 13.\u003cbr /\u003e\r\n\tAt time 3, the distance between 1 and 4 is still unknown.\u003cbr /\u003e\r\n\tAt the end, location 6 is 3 units west and 7 north of 2, so the distance is 10.\u003c/div\u003e"}}]}