{"raw_statement":[{"iden":"statement","content":"In the year of $30XX$ participants of some world programming championship live in a single large hotel. The hotel has $n$ floors. Each floor has $m$ sections with a single corridor connecting all of them. The sections are enumerated from $1$ to $m$ along the corridor, and all sections with equal numbers on different floors are located exactly one above the other. Thus, the hotel can be represented as a rectangle of height $n$ and width $m$. We can denote sections with pairs of integers $(i, j)$, where $i$ is the floor, and $j$ is the section number on the floor.\n\nThe guests can walk along the corridor on each floor, use stairs and elevators. Each stairs or elevator occupies all sections $(1, x)$, $(2, x)$, $\\ldots$, $(n, x)$ for some $x$ between $1$ and $m$. All sections not occupied with stairs or elevators contain guest rooms. It takes one time unit to move between neighboring sections on the same floor or to move one floor up or down using stairs. It takes one time unit to move up to $v$ floors in any direction using an elevator. You can assume you don't have to wait for an elevator, and the time needed to enter or exit an elevator is negligible.\n\nYou are to process $q$ queries. Each query is a question \"what is the minimum time needed to go from a room in section $(x_1, y_1)$ to a room in section $(x_2, y_2)$?\""},{"iden":"input","content":"The first line contains five integers $n, m, c_l, c_e, v$ ($2 \\leq n, m \\leq 10^8$, $0 \\leq c_l, c_e \\leq 10^5$, $1 \\leq c_l + c_e \\leq m - 1$, $1 \\leq v \\leq n - 1$) — the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectively.\n\nThe second line contains $c_l$ integers $l_1, \\ldots, l_{c_l}$ in increasing order ($1 \\leq l_i \\leq m$), denoting the positions of the stairs. If $c_l = 0$, the second line is empty.\n\nThe third line contains $c_e$ integers $e_1, \\ldots, e_{c_e}$ in increasing order, denoting the elevators positions in the same format. It is guaranteed that all integers $l_i$ and $e_i$ are distinct.\n\nThe fourth line contains a single integer $q$ ($1 \\leq q \\leq 10^5$) — the number of queries.\n\nThe next $q$ lines describe queries. Each of these lines contains four integers $x_1, y_1, x_2, y_2$ ($1 \\leq x_1, x_2 \\leq n$, $1 \\leq y_1, y_2 \\leq m$) — the coordinates of starting and finishing sections for the query. It is guaranteed that the starting and finishing sections are distinct. It is also guaranteed that these sections contain guest rooms, i. e. $y_1$ and $y_2$ are not among $l_i$ and $e_i$."},{"iden":"output","content":"Print $q$ integers, one per line — the answers for the queries."},{"iden":"example","content":"Input\n\n5 6 1 1 3\n2\n5\n3\n1 1 5 6\n1 3 5 4\n3 3 5 3\n\nOutput\n\n7\n5\n4"},{"iden":"note","content":"In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit.\n\nIn the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the section 2."}],"translated_statement":[{"iden":"statement","content":"在公元 $30 X X$ 年，某世界编程锦标赛的参赛者们居住在一个大型酒店中。该酒店有 $n$ 层，每层有 $m$ 个区域，由一条走廊连接所有区域。区域沿走廊从 $1$ 到 $m$ 编号，不同楼层上编号相同的区域恰好上下对齐。因此，酒店可表示为一个高度为 $n$、宽度为 $m$ 的矩形。我们可以用整数对 $(i, j)$ 表示区域，其中 $i$ 是楼层，$j$ 是该楼层的区域编号。\n\n客人可以在每层的走廊上行走，使用楼梯或电梯。每个楼梯或电梯占据所有形如 $(1, x)$、$(2, x)$、$dots.h$、$(n, x)$ 的区域，其中 $x$ 在 $1$ 到 $m$ 之间。所有未被楼梯或电梯占据的区域均为客房。在同层移动到相邻区域需要一单位时间，使用楼梯上下一层也需要一单位时间。使用电梯可以在一单位时间内上下最多 $v$ 层。你可以假设无需等待电梯，且进出电梯的时间可忽略不计。\n\n你需要处理 $q$ 个查询。每个查询询问：“从区域 $(x_1, y_1)$ 的房间到区域 $(x_2, y_2)$ 的房间所需的最少时间是多少？”\n\n第一行包含五个整数 $n, m, c_l, c_e, v$（$2 lt.eq n, m lt.eq 10^8$，$0 lt.eq c_l, c_e lt.eq 10^5$，$1 lt.eq c_l + c_e lt.eq m -1$，$1 lt.eq v lt.eq n -1$）——分别表示楼层数、每层区域数、楼梯数量、电梯数量和电梯最大速度。\n\n第二行包含 $c_l$ 个按递增顺序排列的整数 $l_1, dots.h, l_(c_l)$（$1 lt.eq l_i lt.eq m$），表示楼梯的位置。若 $c_l = 0$，则第二行为空行。\n\n第三行包含 $c_e$ 个按递增顺序排列的整数 $e_1, dots.h, e_(c_e)$，表示电梯的位置，格式相同。保证所有 $l_i$ 和 $e_i$ 互不相同。\n\n第四行包含一个整数 $q$（$1 lt.eq q lt.eq 10^5$）——查询数量。\n\n接下来的 $q$ 行描述查询。每行包含四个整数 $x_1, y_1, x_2, y_2$（$1 lt.eq x_1, x_2 lt.eq n$，$1 lt.eq y_1, y_2 lt.eq m$）——表示查询的起始和终止区域坐标。保证起始和终止区域不同，且均为客房，即 $y_1$ 和 $y_2$ 不在 $l_i$ 或 $e_i$ 中。\n\n请输出 $q$ 个整数，每行一个——对应每个查询的答案。\n\n在第一个查询中，最优方案是用四单位时间走到第 5 个区域的电梯，用两单位时间乘电梯到第五层，再用一单位时间走到目标位置。\n\n在第二个查询中，仍最优使用电梯；但在第三个查询中，使用第 2 个区域的楼梯更优。"},{"iden":"input","content":"第一行包含五个整数 $n, m, c_l, c_e, v$（$2 lt.eq n, m lt.eq 10^8$，$0 lt.eq c_l, c_e lt.eq 10^5$，$1 lt.eq c_l + c_e lt.eq m -1$，$1 lt.eq v lt.eq n -1$）——分别表示楼层数、每层区域数、楼梯数量、电梯数量和电梯最大速度。第二行包含 $c_l$ 个按递增顺序排列的整数 $l_1, dots.h, l_(c_l)$（$1 lt.eq l_i lt.eq m$），表示楼梯的位置。若 $c_l = 0$，则第二行为空行。第三行包含 $c_e$ 个按递增顺序排列的整数 $e_1, dots.h, e_(c_e)$，表示电梯的位置，格式相同。保证所有 $l_i$ 和 $e_i$ 互不相同。第四行包含一个整数 $q$（$1 lt.eq q lt.eq 10^5$）——查询数量。接下来的 $q$ 行描述查询。每行包含四个整数 $x_1, y_1, x_2, y_2$（$1 lt.eq x_1, x_2 lt.eq n$，$1 lt.eq y_1, y_2 lt.eq m$）——表示查询的起始和终止区域坐标。保证起始和终止区域不同，且均为客房，即 $y_1$ 和 $y_2$ 不在 $l_i$ 或 $e_i$ 中。"},{"iden":"output","content":"请输出 $q$ 个整数，每行一个——对应每个查询的答案。"},{"iden":"note","content":"在第一个查询中，最优方案是用四单位时间走到第 5 个区域的电梯，用两单位时间乘电梯到第五层，再用一单位时间走到目标位置。在第二个查询中，仍最优使用电梯；但在第三个查询中，使用第 2 个区域的楼梯更优。"}],"sample_group":[],"show_order":[],"formal_statement":"**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ be the number of floors and sections per floor, respectively.  \nLet $ c_l, c_e \\in \\mathbb{Z}_{\\geq 0} $ be the number of stairs and elevators, respectively.  \nLet $ v \\in \\mathbb{Z}^+ $ be the elevator speed (maximum floors traversed per time unit).  \n\nLet $ L = \\{ l_1, l_2, \\dots, l_{c_l} \\} \\subseteq \\{1, \\dots, m\\} $ be the set of stair positions, sorted increasingly.  \nLet $ E = \\{ e_1, e_2, \\dots, e_{c_e} \\} \\subseteq \\{1, \\dots, m\\} $ be the set of elevator positions, sorted increasingly, with $ L \\cap E = \\emptyset $.  \n\nLet $ Q $ be a set of $ q $ queries. Each query is a tuple $ (x_1, y_1, x_2, y_2) \\in \\{1,\\dots,n\\}^2 \\times \\{1,\\dots,m\\}^2 $, where $ y_1, y_2 \\notin L \\cup E $, and $ (x_1, y_1) \\neq (x_2, y_2) $.\n\n**Constraints**  \n1. $ 2 \\leq n, m \\leq 10^8 $  \n2. $ 0 \\leq c_l, c_e \\leq 10^5 $, $ c_l + c_e \\leq m - 1 $  \n3. $ 1 \\leq v \\leq n - 1 $  \n4. All $ l_i, e_i $ are distinct and in $ [1, m] $  \n5. For each query: $ 1 \\leq x_1, x_2 \\leq n $, $ 1 \\leq y_1, y_2 \\leq m $, $ y_1, y_2 \\notin L \\cup E $, $ (x_1, y_1) \\neq (x_2, y_2) $  \n6. $ 1 \\leq q \\leq 10^5 $\n\n**Objective**  \nFor each query $ (x_1, y_1, x_2, y_2) $, compute the minimum time to travel from $ (x_1, y_1) $ to $ (x_2, y_2) $, using:  \n- Horizontal movement on the same floor: cost 1 per unit distance.  \n- Vertical movement via stairs: cost 1 per floor.  \n- Vertical movement via elevators: cost $ \\left\\lceil \\frac{|x_1 - x_2|}{v} \\right\\rceil $ per vertical transition.  \n\nMovement must proceed via a vertical transit point (stair or elevator) located at some column $ z \\in L \\cup E $.  \n\nThe travel time from $ (x_1, y_1) $ to $ (x_2, y_2) $ via a transit at column $ z $ is:  \n$$\n|y_1 - z| + \\text{vert}(x_1, x_2, z) + |z - y_2|\n$$  \nwhere  \n$$\n\\text{vert}(x_1, x_2, z) = \n\\begin{cases}\n|x_1 - x_2| & \\text{if } z \\in L \\\\\n\\left\\lceil \\frac{|x_1 - x_2|}{v} \\right\\rceil & \\text{if } z \\in E\n\\end{cases}\n$$\n\nThe minimal time is:  \n$$\n\\min_{z \\in L \\cup E} \\left( |y_1 - z| + |z - y_2| + \\text{vert}(x_1, x_2, z) \\right)\n$$\n\nAdditionally, if direct horizontal movement is possible (i.e., same floor), then:  \n$$\n|x_1 - x_2| + |y_1 - y_2|\n$$  \nis a candidate (but since $ y_1, y_2 $ are guest rooms and vertical transit is required unless $ x_1 = x_2 $, this reduces to $ |y_1 - y_2| $ when $ x_1 = x_2 $).\n\nThus, the full objective is:  \n$$\n\\min \\left( \n\\begin{cases}\n|y_1 - y_2| & \\text{if } x_1 = x_2 \\\\\n\\min_{z \\in L \\cup E} \\left( |y_1 - z| + |z - y_2| + \\text{vert}(x_1, x_2, z) \\right) & \\text{otherwise}\n\\end{cases}\n\\right)\n$$","simple_statement":null,"has_page_source":false}