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.

The 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.

You 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)$?"


-----Input-----

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.

The 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.

The 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.

The fourth line contains a single integer $q$ ($1 \leq q \leq 10^5$) — the number of queries.

The 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$.


-----Output-----

Print $q$ integers, one per line — the answers for the queries.


-----Example-----
Input
5 6 1 1 3
2
5
3
1 1 5 6
1 3 5 4
3 3 5 3

Output
7
5
4



-----Note-----

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.

In 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.

Difficulty: MEDIUM_HARD
Tags: Sorting
Skill types: Sorting

Starter code:

Solutions:
from __future__ import division, print_function
import bisect
import math
import itertools
import sys
from atexit import register
if sys.version_info[0] < 3:
	from io import BytesIO as stream
else:
	from io import StringIO as stream
if sys.version_info[0] < 3:

	class dict(dict):

		def items(self):
			return dict.iteritems(self)

		def keys(self):
			return dict.iterkeys(self)

		def values(self):
			return dict.itervalues(self)
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def sync_with_stdio(sync=True):
	global input, flush
	if sync:
		flush = sys.stdout.flush
	else:
		sys.stdin = stream(sys.stdin.read())
		input = lambda : sys.stdin.readline().rstrip('\r\n')
		sys.stdout = stream()
		register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))

def main():
	(n, m, c1, ce, v) = map(int, input().split())
	posstairs = list(map(int, input().split()))
	posele = list(map(int, input().split()))
	q = int(input())
	for i in range(0, q):
		(y1, x1, y2, x2) = map(int, input().split())
		if x2 < x1:
			(x1, x2) = (x2, x1)
		if ce != 0:
			ind1 = bisect.bisect_left(posele, x1)
			if ind1 == ce:
				ind1 -= 1
			t1 = abs(x1 - posele[ind1]) + abs(posele[ind1] - x2) + math.ceil(abs(y2 - y1) / v)
			ind1 -= 1
			if ind1 >= 0:
				t1 = min(t1, abs(x1 - posele[ind1]) + abs(posele[ind1] - x2) + math.ceil(abs(y2 - y1) / v))
		if c1 != 0:
			ind3 = bisect.bisect_left(posstairs, x1)
			if ind3 == c1:
				ind3 -= 1
			t2 = abs(x1 - posstairs[ind3]) + abs(posstairs[ind3] - x2) + abs(y2 - y1)
			ind3 -= 1
			if ind3 >= 0:
				t2 = min(t2, abs(x1 - posstairs[ind3]) + abs(posstairs[ind3] - x2) + abs(y2 - y1))
		if y1 == y2:
			print(abs(x2 - x1))
		elif ce != 0 and c1 != 0:
			print(min(t1, t2))
		elif ce != 0:
			print(t1)
		else:
			print(t2)
sync_with_stdio(False)
main()


import sys
input = sys.stdin.readline
import math
import bisect
(n, m, cl, ce, v) = list(map(int, input().split()))
l = [int(i) for i in input().split() if i != '\n']
e = [int(i) for i in input().split() if i != '\n']
q = int(input())
for i in range(q):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	(lft, neareststa, sta, nearestele) = (0, 0, 0, 0)
	if x2 == x1 and y1 == y2:
		output = 0
	elif x2 == x1:
		output = abs(y2 - y1)
	else:
		lft = math.ceil(abs(x2 - x1) / v)
		sta = abs(x2 - x1)
		if cl > 0:
			nearestele = bisect.bisect_left(l, y1)
			if len(l) > nearestele:
				first = abs(l[nearestele] - y1) + abs(y2 - l[nearestele])
			else:
				first = 1000000000001
			if nearestele != 0:
				second = abs(l[nearestele - 1] - y1) + abs(y2 - l[nearestele - 1])
			else:
				second = 1000000000001
			nearestele = min(first, second)
		else:
			nearestele = 1000000000001
		if ce > 0:
			neareststa = bisect.bisect_left(e, y1)
			if len(e) > neareststa:
				first = abs(e[neareststa] - y1) + abs(y2 - e[neareststa])
			else:
				first = 1000000000001
			if neareststa != 0:
				second = abs(e[neareststa - 1] - y1) + abs(y2 - e[neareststa - 1])
			else:
				second = 1000000000001
			neareststa = min(first, second)
		else:
			neareststa = 1000000000001
		output = min(lft + neareststa, sta + nearestele)
	sys.stdout.write(str(output) + '\n')


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
(n, m, cl, ce, v) = mints()
l = list(mints())
e = list(mints())

def dist(x1, y1, x2, y2, t, yv):
	if t == 0:
		return abs(y1 - yv) + abs(yv - y2) + abs(x1 - x2)
	if t == 1:
		return abs(y1 - yv) + abs(yv - y2) + (abs(x2 - x1) + v - 1) // v
q = mint()
bigg = 10 ** 100
for i in range(q):
	(x1, y1, x2, y2) = mints()
	best = bigg
	if len(l) != 0:
		L = 0
		R = len(l) - 1
		while R - L > 1:
			C = (L + R) // 2
			if l[C] <= y1:
				L = C
			else:
				R = C
		best = min(best, dist(x1, y1, x2, y2, 0, l[L]))
		best = min(best, dist(x1, y1, x2, y2, 0, l[R]))
		L = 0
		R = len(l) - 1
		while R - L > 1:
			C = (L + R) // 2
			if l[C] <= y2:
				L = C
			else:
				R = C
		best = min(best, dist(x1, y1, x2, y2, 0, l[L]))
		best = min(best, dist(x1, y1, x2, y2, 0, l[R]))
	if len(e) != 0:
		L = 0
		R = len(e) - 1
		while R - L > 1:
			C = (L + R) // 2
			if e[C] <= y1:
				L = C
			else:
				R = C
		best = min(best, dist(x1, y1, x2, y2, 1, e[L]))
		best = min(best, dist(x1, y1, x2, y2, 1, e[R]))
		L = 0
		R = len(e) - 1
		while R - L > 1:
			C = (L + R) // 2
			if e[C] <= y2:
				L = C
			else:
				R = C
		best = min(best, dist(x1, y1, x2, y2, 1, e[L]))
		best = min(best, dist(x1, y1, x2, y2, 1, e[R]))
	if x1 == x2:
		best = min(best, abs(y1 - y2))
	print(best)


from bisect import bisect_left
input = __import__('sys').stdin.readline
MIS = lambda : map(int, input().split())

def walk(x1, y1, x2, y2, L, v):
	if y1 == y2:
		return abs(x2 - x1)
	dy = abs(y1 - y2)
	vertical = dy // v
	if dy % v:
		vertical += 1
	i = bisect_left(L, x1)
	xs1 = L[i - 1] if 0 <= i - 1 < len(L) else float('inf')
	xs2 = L[i] if 0 <= i < len(L) else float('inf')
	xs3 = L[i + 1] if 0 <= i + 1 < len(L) else float('inf')
	d = min(abs(x1 - xs1) + abs(xs1 - x2), abs(x1 - xs2) + abs(xs2 - x2), abs(x1 - xs3) + abs(xs3 - x2))
	return d + vertical
(n, m, cs, ce, v) = MIS()
sta = list(MIS())
ele = list(MIS())
for TEST in range(int(input())):
	(y1, x1, y2, x2) = MIS()
	if x1 > x2:
		(x1, x2) = (x2, x1)
		(y1, y2) = (y2, y1)
	print(min(walk(x1, y1, x2, y2, sta, 1), walk(x1, y1, x2, y2, ele, v)))


import sys
from bisect import bisect_left
from math import ceil
input = sys.stdin.readline
(n, m, cl, ce, v) = map(int, input().split())
l = list(map(int, input().split()))
e = list(map(int, input().split()))
q = int(input())
for _ in range(q):
	(x1, y1, x2, y2) = map(int, input().split())
	if x1 == x2:
		print(abs(y1 - y2))
		continue
	ans = []
	idx = bisect_left(l, y1)
	for i in range(idx - 1, idx + 2):
		if 0 <= i < cl:
			t = abs(y1 - l[i]) + abs(x1 - x2) + abs(l[i] - y2)
			ans.append(t)
	idx = bisect_left(e, y1)
	for i in range(idx - 1, idx + 2):
		if 0 <= i < ce:
			t = abs(y1 - e[i]) + ceil(abs(x1 - x2) / v) + abs(y2 - e[i])
			ans.append(t)
	print(min(ans))


import sys
from bisect import bisect

def readline():
	return tuple(map(int, input().split()))

def readlines():
	return (tuple(map(int, line.split())) for line in sys.stdin.readlines())

def bisect_bounds(arr, val):
	idx = bisect(arr, val)
	if idx:
		yield (idx - 1)
	if idx < len(arr):
		yield idx

class Minimizator:

	def __init__(self, value=float('inf')):
		self.value = value

	def eat(self, value):
		self.value = min(self.value, value)

def main():
	(n, m, cl, ce, v) = readline()
	l = readline()
	e = readline()
	assert len(l) == cl
	assert len(e) == ce
	(q,) = readline()
	answers = list()
	for (x1, y1, x2, y2) in readlines():
		if x1 == x2:
			answers.append(abs(y1 - y2))
		else:
			ans = Minimizator(n + 2 * m)
			for idx in bisect_bounds(l, y1):
				ladder = l[idx]
				ans.eat(abs(x1 - x2) + abs(ladder - y1) + abs(ladder - y2))
			for idx in bisect_bounds(e, y1):
				elevator = e[idx]
				ans.eat((abs(x1 - x2) - 1) // v + 1 + abs(elevator - y1) + abs(elevator - y2))
			answers.append(ans.value)
	print('\n'.join(map(str, answers)))
main()


def takeClosest(myList, myNumber):
	if len(myList) == 0:
		return 90000000000.0
	pos = bisect_left(myList, myNumber)
	if pos == 0:
		return myList[0]
	if pos == len(myList):
		return myList[-1]
	before = myList[pos - 1]
	after = myList[pos]
	if after - myNumber < myNumber - before:
		return after
	else:
		return before
from bisect import bisect_left
from math import ceil
(n, m, n_stairs, n_elevators, v) = map(int, input().split(' '))
if n_stairs > 0:
	stairs = list(map(int, input().split(' ')))
else:
	stairs = []
	input()
if n_elevators > 0:
	elevators = list(map(int, input().split(' ')))
else:
	elevators = []
	input()
queries = int(input())
res = []
for i in range(queries):
	(x1, y1, x2, y2) = map(int, input().split(' '))
	next_elevator = takeClosest(elevators, (y1 + y2) / 2)
	next_stairs = takeClosest(stairs, (y1 + y2) / 2)
	time_elevator = abs(x1 - x2) / v
	time_stairs = abs(x1 - x2)
	mi = min(y1, y2)
	ma = max(y1, y2)
	if next_elevator < mi:
		time_elevator += (mi - next_elevator) * 2
	elif next_elevator > ma:
		time_elevator += (next_elevator - ma) * 2
	if next_stairs < mi:
		time_stairs += (mi - next_stairs) * 2
	elif next_stairs > ma:
		time_stairs += (next_stairs - ma) * 2
	dis = abs(y1 - y2)
	if time_elevator < time_stairs:
		dis += time_elevator
	else:
		dis += time_stairs
	if x1 == x2:
		res.append(abs(y1 - y2))
	else:
		res.append(ceil(dis))
print(*res, sep='\n')


import bisect as bs
import sys
inp = sys.stdin.readlines()
(n, m, ladders, elevators, v) = [int(x) for x in inp[0].strip().split()]
ladders = [int(x) for x in inp[1].strip().split()]
elevators = [int(x) for x in inp[2].strip().split()]
q = int(inp[3].strip())
qs = []
for i in range(q):
	qs.append([int(x) for x in inp[4 + i].strip().split()])
output = []
for query in qs:
	(x1, y1, x2, y2) = query
	if x1 == x2:
		output.append(abs(y1 - y2))
		continue
	cur_ld = []
	if ladders and y1 > ladders[0]:
		cur_ld.append(ladders[bs.bisect_left(ladders, y1) - 1])
	if ladders and y1 < ladders[-1]:
		cur_ld.append(ladders[bs.bisect_right(ladders, y1)])
	cur_elev = []
	if elevators and y1 > elevators[0]:
		cur_elev.append(elevators[bs.bisect_left(elevators, y1) - 1])
	if elevators and y1 < elevators[-1]:
		cur_elev.append(elevators[bs.bisect_right(elevators, y1)])
	ans = []
	for lad in cur_ld:
		ans.append(abs(y1 - lad) + abs(y2 - lad) + abs(x1 - x2))
	for elev in cur_elev:
		height = abs(x1 - x2)
		elspeed = height // v
		if height % v != 0:
			elspeed += 1
		ans.append(abs(y1 - elev) + abs(y2 - elev) + elspeed)
	ans = min(ans)
	output.append(ans)
output = '\n'.join(map(str, output))
print(output)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(10)]
pp = [0] * 10

def SieveOfEratosthenes(n=10):
	p = 2
	c = 0
	while p * p <= n:
		if prime[p] == True:
			c += 1
			for i in range(p, n + 1, p):
				pp[i] += 1
				prime[i] = False
		p += 1

def binarySearch(arr, n, key):
	left = 0
	right = n - 1
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] >= key:
			res = arr[mid]
			right = mid - 1
		else:
			left = mid + 1
	return res

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] >= key:
			right = mid - 1
		else:
			res = arr[mid]
			left = mid + 1
	return res
(n, m, cl, ce, v) = map(int, input().split())
l = list(map(int, input().split()))
e = list(map(int, input().split()))
l.sort()
e.sort()
q = int(input())
for i in range(q):
	res = 10 ** 10
	(x1, y1, x2, y2) = map(int, input().split())
	if x1 == x2:
		print(abs(y1 - y2))
		continue
	lx1 = binarySearch(l, cl, y1)
	lx2 = binarySearch1(l, cl, y1)
	ex1 = binarySearch(e, ce, y1)
	ex2 = binarySearch1(e, ce, y1)
	if lx1 != -1:
		res = min(res, abs(lx1 - y1) + abs(lx1 - y2) + abs(x1 - x2))
	if lx2 != -1:
		res = min(res, abs(lx2 - y1) + abs(lx2 - y2) + abs(x1 - x2))
	if ex1 != -1:
		res = min(res, abs(ex1 - y1) + abs(ex1 - y2) + math.ceil(abs(x1 - x2) / v))
	if ex2 != -1:
		res = min(res, abs(ex2 - y1) + abs(ex2 - y2) + math.ceil(abs(x1 - x2) / v))
	print(res)