{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"Farmer John\u0027s 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):\r\u003cbr\u003e\u003cpre\u003e F1 --- (13) ---- F6 --- (9) ----- F3\r\u003cbr\u003e | |\r\u003cbr\u003e (3) |\r\u003cbr\u003e | (7)\r\u003cbr\u003e F4 --- (20) -------- F2 |\r\u003cbr\u003e | |\r\u003cbr\u003e (2) F5\r\u003cbr\u003e | \r\u003cbr\u003e F7 \u003c/pre\u003e\r\u003cbr\u003eBeing an ASCII diagram, it is not precisely to scale, of course.\r\u003cbr\u003e\r\u003cbr\u003eEach 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 \r\u003cbr\u003e(sequence of roads) links every pair of farms.\r\u003cbr\u003e\r\u003cbr\u003eFJ 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:\r\u003cbr\u003e\r\u003cbr\u003e There is a road of length 10 running north from Farm #23 to Farm #17\r\u003cbr\u003e There is a road of length 7 running east from Farm #1 to Farm #17\r\u003cbr\u003e ...\r\u003cbr\u003e\r\u003cbr\u003eAs FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:\r\u003cbr\u003e\r\u003cbr\u003e What is the Manhattan distance between farms #1 and #23?\r\u003cbr\u003e\r\u003cbr\u003eFJ answers Bob, when he can (sometimes he doesn\u0027t yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the \"Manhattan\" distance between the pair of farms.\r\u003cbr\u003eThe 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).\r\u003cbr\u003e\r\u003cbr\u003eWhen 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 \"-1\".\r\u003cbr\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cpre\u003e* Line 1: Two space-separated integers: N and M\r\u003cbr\u003e\r\u003cbr\u003e* Lines 2..M+1: Each line contains four space-separated entities, F1,\r\u003cbr\u003e F2, L, and D that describe a road. F1 and F2 are numbers of\r\u003cbr\u003e two farms connected by a road, L is its length, and D is a\r\u003cbr\u003e character that is either \u0027N\u0027, \u0027E\u0027, \u0027S\u0027, or \u0027W\u0027 giving the\r\u003cbr\u003e direction of the road from F1 to F2.\r\u003cbr\u003e\r\u003cbr\u003e* Line M+2: A single integer, K (1 \u0026lt;\u003d K \u0026lt;\u003d 10,000), the number of FB\u0027s\r\u003cbr\u003e queries\r\u003cbr\u003e\r\u003cbr\u003e* Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob\r\u003cbr\u003e and contains three space-separated integers: F1, F2, and I. F1\r\u003cbr\u003e and F2 are numbers of the two farms in the query and I is the\r\u003cbr\u003e index (1 \u0026lt;\u003d I \u0026lt;\u003d M) in the data after which Bob asks the\r\u003cbr\u003e query. Data index 1 is on line 2 of the input data, and so on.\u003c/pre\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cpre\u003e* Lines 1..K: One integer per line, the response to each of Bob\u0027s\r\u003cbr\u003e queries. Each line should contain either a distance\r\u003cbr\u003e measurement or -1, if it is impossible to determine the\r\u003cbr\u003e appropriate distance.\u003c/pre\u003e"}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e7 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\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e13\r\n-1\r\n10\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Hint","value":{"format":"HTML","content":"At time 1, FJ knows the distance between 1 and 6 is 13.\r\u003cbr\u003eAt time 3, the distance between 1 and 4 is still unknown.\r\u003cbr\u003eAt the end, location 6 is 3 units west and 7 north of 2, so the distance is 10.\r\u003cbr\u003e"}}]}