#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 3e5 + 50;
ll d[maxn],h[maxn];
struct node
{
    int l,r;
    ll sum,lmax,rmax,maxx;
}tree[maxn << 2];
void push_up(int i)
{
    int x1 = tree[i << 1].l,y1 = tree[i << 1].r;
    int x2 = tree[i << 1 | 1].l,y2 = tree[i << 1 | 1].r;
    ll dis = d[y1];

    /*ll maxx = max(tree[i << 1].lmax,tree[i << 1].sum + h[x1] + h[x2] + dis);

    now.lmax = max(maxx,tree[i << 1].sum + tree[i << 1 | 1].lmax - h[x2] + h[x1] + dis);

    maxx = max(tree[i << 1 | 1].rmax,tree[i << 1 | 1].sum + h[y2] + h[y1]);

    now.rmax = max(maxx,tree[i << 1 | 1].sum + tree[i << 1].rmax - h[y1] + h[y2]);

    now.sum = tree[i << 1].sum + tree[i << 1 | 1].sum;

    maxx = max(tree[i << 1].maxx,tree[i << 1 | 1].maxx);
    maxx = max(maxx,h[y1] + h[x2] + dis);
    maxx = max(now.lmax,now.rmax);

    now.maxx = max(maxx,tree[i << 1].rmax + tree[i << 1 | 1].lmax - h[y1] - h[x2]);*/

    ll mx = 0;

    ll maxx = max(tree[i << 1].lmax,h[x1] + tree[i << 1].sum + h[x2] + dis);

    tree[i].lmax = max(maxx,tree[i << 1].sum + tree[i << 1 | 1].lmax - h[x2] + h[x1] + dis);
    mx = max(mx,tree[i].lmax);

    maxx = max(tree[i << 1 | 1].rmax,h[y2] + tree[i << 1 | 1].sum + h[y1] + dis);

    tree[i].rmax = max(maxx,tree[i << 1 | 1].sum + tree[i << 1].rmax + h[y2] - h[y1] + dis);
    mx = max(mx,tree[i].rmax);

    mx = max(mx,max(tree[i << 1].maxx,tree[i << 1 | 1].maxx));
    mx = max(mx,h[x2] + h[y1] + dis);
    tree[i].maxx = max(mx,tree[i << 1].rmax + tree[i << 1 | 1].lmax - h[x2] - h[y1] + dis);

    tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum + dis;
}
void build(int i,int l,int r)
{
    tree[i].l = l;
    tree[i].r = r;
    int mid = (l + r) >> 1;
    if (l == r)
    {
        tree[i].lmax = tree[i].rmax = tree[i].maxx = h[l];
        tree[i].sum = 0;
        return;
    }
    build(i << 1,l,mid);
    build(i << 1 | 1,mid + 1,r);
    push_up(i);
}
node query(int i,int x,int y)
{
    if (tree[i].l >= x && tree[i].r <= y) return tree[i];
    int mid = (tree[i].l + tree[i].r) >> 1;
    if (y <= mid) return query(i << 1,x,y);
    else if (x > mid) return query(i << 1 | 1,x,y);
    else
    {
        node lt = query(i << 1,x,mid);
        node rt = query(i << 1 | 1,mid + 1,y);
        node now;
        int x1 = lt.l,y1 = lt.r;
        int x2 = rt.l,y2 = rt.r;
        ll mx = 0;
        ll dis = d[y1];

        ll maxx = max(lt.lmax,h[x1] + lt.sum + h[x2] + dis);

        now.lmax = max(maxx,lt.sum + rt.lmax - h[x2] + h[x1] + dis);
        mx = max(mx,now.lmax);

        maxx = max(rt.rmax,h[y2] + rt.sum + h[y1] + dis);

        now.rmax = max(maxx,rt.sum + lt.rmax + h[y2] - h[y1] + dis);
        mx = max(mx,now.rmax);

        mx = max(mx,max(lt.maxx,rt.maxx));
        mx = max(mx,h[x2] + h[y1] + dis);
        now.maxx = max(mx,lt.rmax + rt.lmax - h[x2] - h[y1] + dis);

        now.sum = lt.sum + rt.sum + dis;
        now.l = lt.l;
        now.r = rt.r;
        return now;
    }
}
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for (int i = 1 ; i <= n ; i ++)
    {
        scanf("%lld",&d[i]);
    }
    for (int i = 1 ; i <= n ; i ++)
    {
        scanf("%lld",&h[i]);
        h[i] *= 2;
    }
    for (int i = n + 1 ; i <= 2 * n ; i ++)
    {
        d[i] = d[i - n];
        h[i] = h[i - n];
    }
    int m = n;
    n *= 2;
    build(1,1,n);
    //cout<<"??  "<<tree[1].maxx<<endl;
    while(q --)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if (x > y)
        {
            int x1 = y + 1;
            int y1 = x - 1;
            x = x1;
            y = y1;
        }
        else
        {
            int x1 = y + 1;
            int y1 = x + m - 1;
            x = x1;
            y = y1;
        }
        //cout<<"? "<<x<<"  "<<y<<endl;
        node res = query(1,x,y);
        printf("%lld\n",res.maxx);
    }
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+5;
const int INF = 0x3f3f3f3f;
typedef long long LL;

#define root 1, n<<1, 1
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1

struct PP {
    int cr, cl;     //index
    LL val;
}st[N<<3];

int n, m;
LL d[N<<1], h[N<<1];
LL sum[N<<1];
//when query
LL ans;
int idx;

inline LL getdis(int x, int y) {
    if(x > y) swap(x, y);
    return sum[y-1]-sum[x-1];
}

void pushup(int l, int r, int rt) {
    int mid = (l+r)/2;
    //right
    LL tmp = 2*h[st[rt<<1].cr]+getdis(st[rt<<1].cr, r);
    if(tmp > 2*h[st[rt<<1|1].cr]+getdis(st[rt<<1|1].cr, r))
        st[rt].cr = st[rt<<1].cr;
    else
        st[rt].cr = st[rt<<1|1].cr;
    //left
    tmp = 2*h[st[rt<<1|1].cl]+getdis(st[rt<<1|1].cl, l);
    if(tmp > 2*h[st[rt<<1].cl]+getdis(st[rt<<1].cl, l))
        st[rt].cl = st[rt<<1|1].cl;
    else
        st[rt].cl = st[rt<<1].cl;
    //val
    st[rt].val = max(st[rt<<1].val, st[rt<<1|1].val);
    tmp = 2*(h[st[rt<<1].cr]+h[st[rt<<1|1].cl])+getdis(st[rt<<1].cr, st[rt<<1|1].cl);
    st[rt].val = max(st[rt].val, tmp);
}

void build(int l, int r, int rt) {
    if(l == r) {
        st[rt] = (PP){l, r, 2*h[l]};
        return ;
    }
    int mid = (l+r)/2;
    build(lson);
    build(rson);
    pushup(l, r, rt);
}

void query(int l, int r, int rt, int x, int y) {
    if(x <= l && y >= r) {
        ans = max(ans, st[rt].val);
        LL tmp;
        if(idx != -1) {
            ans = max(ans, 2*(h[idx]+h[st[rt].cl])+getdis(idx, st[rt].cl));
            tmp = 2*h[idx]+getdis(idx, r);
            if(tmp < 2*h[st[rt].cr]+getdis(st[rt].cr, r))
                idx = st[rt].cr;
        }
        else
            idx = st[rt].cr;

        return ;
    }
    int mid = (l+r)/2;
    if(x <= mid)
        query(lson, x, y);
    if(y > mid)
        query(rson, x, y);
}

int main() {
    scanf("%d%d", &n, &m);
    int tmp;
    for(int i = 1;i <= n; i++) {
        scanf("%I64d", &d[i]);
        d[i+n] = d[i];
    }
    for(int i = 1;i <= 2*n; i++) {
        sum[i] = sum[i-1]+d[i];
    }
    for(int i = 1;i <= n; i++) {
        scanf("%I64d", &h[i]);
        h[i+n] = h[i];
    }
    build(root);
    //
    int l, r;
    for(int i = 1;i <= m; i++) {
        scanf("%d%d", &l, &r);
        ans = 0, idx = -1;
        l <= r? query(root, r+1, l+n-1):query(root, r+1, l-1);
        printf("%I64d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

const int SIZE = 2e5 + 5;
const int inf = 1e18;

int n, m;
int d[SIZE], h[SIZE], sumMin[SIZE], sumMax[SIZE];

struct node {
	int l, r, maxx, minn;
} tree[SIZE << 2];

namespace ae86 {
	const int bufl = 1 << 15;
	char buf[bufl], *s = buf, *t = buf;
	inline int fetch() {
		if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; }
		return *s++;
	}
	inline int read() {
		int a = 0, b = 1, c = fetch();
		while (!isdigit(c))b ^= c == '-', c = fetch();
		while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
		return b ? a : -a;
	}
}
using ae86::read;

inline int getMax(int x, int y)
{
	return sumMax[x] > sumMax[y] ? x : y;
}

inline int getMin(int x, int y)
{
	return sumMin[x] < sumMin[y] ? x : y;
}

#define lson(p) p << 1
#define rson(p) p << 1 | 1

inline void pushUp(int p)
{
	tree[p].maxx = getMax(tree[lson(p)].maxx, tree[rson(p)].maxx);
	tree[p].minn = getMin(tree[lson(p)].minn, tree[rson(p)].minn);
}

inline void build(int p, int l, int r)
{
	tree[p].l = l, tree[p].r = r;
	if (l == r) {
		tree[p].maxx = tree[p].minn = l;
		return;
	}
	int mid = (l + r) >> 1;
	build(lson(p), l, mid); build(rson(p), mid + 1, r);
	pushUp(p);
}

inline int queryMax(int p, int l, int r)
{
	if (l <= tree[p].l && tree[p].r <= r) {
		return tree[p].maxx;
	}
	int mid = (tree[p].l + tree[p].r) >> 1, ans = 0;
	if (l <= mid) ans = queryMax(lson(p), l, r);
	if (r > mid) ans = getMax(queryMax(rson(p), l, r), ans);
	return ans;
}

inline int queryMin(int p, int l, int r)
{
	if (l <= tree[p].l && tree[p].r <= r) {
		return tree[p].minn;
	}
	int mid = (tree[p].l + tree[p].r) >> 1, ans = 0;
	if (l <= mid) ans = queryMin(lson(p), l, r);
	if (r > mid) ans = getMin(queryMin(rson(p), l, r), ans);
	return ans;
}

inline int calcMin(int l, int r)
{
	if (l > r) return 0;
	return queryMin(1, l, r);
}

inline int calcMax(int l, int r)
{
	if (l > r) return 0;
	return queryMax(1, l, r);
}

inline int solve(int l, int r)
{
	int minn = calcMin(l, r), maxx = calcMax(l, r);
	if (minn != maxx) return sumMax[maxx] - sumMin[minn];
	int minn2 = getMin(calcMin(l, minn - 1), calcMin(minn + 1, r));
	int maxx2 = getMax(calcMax(l, maxx - 1), calcMax(maxx + 1, r));
	return std::max(sumMax[maxx2] - sumMin[minn], sumMax[maxx] - sumMin[minn2]);
}

signed main()
{
	// freopen("code.in", "r", stdin);
	// freopen("code.out", "w", stdout);
	n = read(), m = read();
	for (register int i = 1; i <= n; ++ i) {
		d[i % n + 1] = d[i % n + 1 + n] = read();
	}
	for (register int i = 1; i <= n; ++ i) {
		h[i] = h[i + n] = read();
	}
	int sum = 0; sumMax[0] = -inf, sumMin[0] = inf;
	for (register int i = 1; i <= (n << 1); ++ i) {
		sum += d[i];
		sumMax[i] = sum + 2 * h[i];
		sumMin[i] = sum - 2 * h[i];
	}
	build(1, 1, 2 * n + 1);
	while (m --) {
		int l = read(), r = read();
		if (l <= r) printf("%lld\n", solve(r + 1, n + l - 1));
		else printf("%lld\n", solve(r + 1, l - 1));
	}
	return 0;
}


#include <bits/stdc++.h>
using int64 = long long;
const int N = 8000005;
const int64 INF = 1e13;
struct node {
    int64 lr, l_, r_, __;
} a[N];
int64 n, m, lra, l_a, _ra, __a;
int64 h[N], dis[N];
inline int64 ls(int64 p) { return p << 1; }
inline int64 rs(int64 p) { return p << 1 | 1; }
inline void update(int p) {
    a[p].lr =
        std::max(a[ls(p)].l_ + a[rs(p)].r_, std::max(a[ls(p)].lr, a[rs(p)].lr));
    a[p].l_ = std::max(a[ls(p)].l_ + a[rs(p)].__, a[rs(p)].l_);
    a[p].r_ = std::max(a[ls(p)].__ + a[rs(p)].r_, a[ls(p)].r_);
    a[p].__ = a[ls(p)].__ + a[rs(p)].__;
}
inline void build(int64 p, int64 l, int64 r) {
    if (l == r) {
        a[p].l_ = h[l] + dis[l];
        a[p].r_ = h[l] + dis[l];
        a[p].__ = dis[l];
        return;
    }
    int64 mid = l + r >> 1;
    build(ls(p), l, mid);
    build(rs(p), mid + 1, r);
    update(p);
}
inline void query(int64 p, int64 l, int64 r, int64 L, int64 R) {
    if (L <= l && r <= R) {
        lra = std::max(lra, std::max(a[p].lr, l_a + a[p].r_));
        l_a = std::max(l_a + a[p].__, std::max(a[p].l_, l_a));
        return;
    }
    int64 mid = l + r >> 1;
    if (mid >= L) query(ls(p), l, mid, L, R);
    if (mid < R) query(rs(p), mid + 1, r, L, R);
}
int main() {
    std::ios::sync_with_stdio(0);
    std::cin >> n >> m;
    for (int i = 2; i <= 2 * n; i += 2) std::cin >> dis[i], dis[i - 1] = 0;
    for (int i = 1; i <= 2 * n; i += 2) {
        std::cin >> h[i], h[i] *= 2;
        h[i + 1] = -INF;
    }
    for (int i = 1; i <= 2 * n; i++) {
        dis[2 * n + i] = dis[i];
        h[i + 2 * n] = h[i];
    }
    build(1, 1, 4 * n - 1);
    for (int i = 1; i <= m; i++) {
        int64 x, y;
        std::cin >> x >> y;
        lra = l_a = -INF;
        if (x == y) {
            query(1, 1, 4 * n - 1, 2 * x + 1, 2 * x - 3 + 2 * n);
        } else if (x < y) {
            query(1, 1, 4 * n - 1, 2 * y + 1, 2 * x - 3 + 2 * n);
        } else {
            query(1, 1, 4 * n - 1, 2 * y + 1, 2 * x - 3);
        }
        std::cout << lra << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
#define F first
#define S second
const ll N = 201111 ;
ll d[N], s[N], h[N], n, q, le[4 * N], ri[4 * N], A[4 * N], MAX;
void build(int l, int r, int node){
	if(l == r){ri[node]=le[node]=2*h[l];return;}
	int mid = (l+r)/2 ;
	build(l, mid, node*2), build(mid+1, r, node*2+1) ;
	A[node] = le[node*2] + ri[node*2+1] + d[mid] ;
	A[node] = max(A[node], max(A[node*2], A[node*2+1])) ;
	le[node] = max(le[node*2+1], le[node*2] + s[r] - s[mid]) ;
	ri[node] = max(ri[node*2], ri[node*2+1] + s[mid+1] - s[l]) ;
}
ll qu(int l, int r, int node, int a, int b){
	if(r < a or l > b) return 0 ;
	if(a <= l and r <= b){
		ll k = A[node];
		if(MAX) k = max(k, MAX+d[l-1]+ri[node]) ;
		if(!MAX) MAX = le[node] ;
		else MAX = max(MAX + s[r] - s[l-1], le[node]) ;
		return k ;
	}
	int mid = (l+r)/2 ;
	ll a1 = qu(l, mid, node*2, a, b), a2 = qu(mid+1, r, node*2+1, a, b) ;
	return max(a1, a2) ;
}
int main ()
{
	scanf("%lld%lld", &n, &q) ;
	for(int i = 1 ; i <= n ; i ++){
		scanf("%lld", &d[i]) ;
		s[i+1] = s[i] + d[i] ;
		d[i+n] = d[i] ;
	}
	for(int i = n+1 ; i < 2*n ; i ++)
		s[i+1] = s[i] + d[i] ;
	for(int i = 1 ; i <= n ; i ++){
		scanf("%lld", &h[i]) ;
		h[i+n] = h[i] ;
	}
	n *= 2 ;
	build(1, n, 1) ;
	while(q --){
		int a, b ; scanf("%d%d", &a, &b) ;
		MAX = 0 ;
		printf("%lld\n", qu(1, n, 1, b+1, a-1+(n/2))) ;
	}
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
#define F first
#define S second
const ll N = 201111 ;
ll d[N], s[N], h[N], n, q, le[4 * N], ri[4 * N], A[4 * N], MAX;
void build(int l, int r, int node){
	if(l == r){ri[node]=le[node]=2*h[l];return;}
	int mid = (l+r)/2 ;
	build(l, mid, node*2), build(mid+1, r, node*2+1) ;
	A[node] = le[node*2] + ri[node*2+1] + d[mid] ;
	A[node] = max(A[node], max(A[node*2], A[node*2+1])) ;
	le[node] = max(le[node*2+1], le[node*2] + s[r] - s[mid]) ;
	ri[node] = max(ri[node*2], ri[node*2+1] + s[mid+1] - s[l]) ;
}
ll qu(int l, int r, int node, int a, int b){
	if(r < a or l > b) return 0 ;
	if(a <= l and r <= b){
		ll k = A[node];
		if(MAX) k = max(k, MAX+d[l-1]+ri[node]) ;
		if(!MAX) MAX = le[node] ;
		else MAX = max(MAX + s[r] - s[l-1], le[node]) ;
		return k ;
	}
	int mid = (l+r)/2 ;
	ll a1 = qu(l, mid, node*2, a, b), a2 = qu(mid+1, r, node*2+1, a, b) ;
	return max(a1, a2) ;
}
int main ()
{
	scanf("%lld%lld", &n, &q) ;
	for(int i = 1 ; i <= n ; i ++){
		scanf("%lld", &d[i]) ;
		s[i+1] = s[i] + d[i] ;
		d[i+n] = d[i] ;
	}
	for(int i = n+1 ; i < 2*n ; i ++)
		s[i+1] = s[i] + d[i] ;
	for(int i = 1 ; i <= n ; i ++){
		scanf("%lld", &h[i]) ;
		h[i+n] = h[i] ;
	}
	n *= 2 ;
	build(1, n, 1) ;
	while(q --){
		int a, b ; scanf("%d%d", &a, &b) ;
		MAX = 0 ;
		if(a > b) b += (n/2) ;
		printf("%lld\n", qu(1, n, 1, b+1, a-1+(n/2))) ;
	}
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {

    vpii v;
    v.pb(mp(a.m, a.n));
    v.pb(mp(a.o, a.p));
    v.pb(mp(b.m, b.n));
    v.pb(mp(b.o, b.p));
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    Node ans = {v[0].f, v[0].s, v[1].f, v[1].s};
    return ans;
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { scanf("%d", &arr[i]); arr[i+a] = arr[i]; }
    F0R(i, a) { scanf("%d", &h[i]); h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%d\n", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {
    if (a.m >= b.m) {
        if (a.o >= b.m) {
            return {a.m, a.n, a.o, a.p};
        }
        return {a.m, a.n, b.m, b.n};
    }
    if (b.o >= a.m) { return {b.m, b.n, b.o, b.p}; }
    return {b.m, b.n, a.m, a.n};
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { scanf("%d", &arr[i]); arr[i+a] = arr[i]; }
    F0R(i, a) { scanf("%d", &h[i]); h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%d\n", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <ll, ll>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const ll INF = 10000000000000007;
int arr[2*MAX];
int h[2*MAX];
ll pref[2*MAX];
ll frnt[2*MAX];
ll bck[2*MAX];

struct Node {
    ll m, n, o, p;
};

Node combine(Node a, Node b) {
    if (a.m >= b.m) {
        if (a.o >= b.m) {
            return {a.m, a.n, a.o, a.p};
        }
        return {a.m, a.n, b.m, b.n};
    }
    if (b.o >= a.m) { return {b.m, b.n, b.o, b.p}; }
    return {b.m, b.n, a.m, a.n};
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { scanf("%d", &arr[i]); arr[i+a] = arr[i]; }
    F0R(i, a) { scanf("%d", &h[i]); h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        ll maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%I64d\n", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {
    vpii v;
    v.pb(mp(a.m, a.n));
    v.pb(mp(a.o, a.p));
    v.pb(mp(b.m, b.n));
    v.pb(mp(b.o, b.p));
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    Node ans = {v[0].f, v[0].s, v[1].f, v[1].s};
    return ans;
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { cin >> arr[i]; arr[i+a] = arr[i]; }
    F0R(i, a) { cin >> h[i]; h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%d", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {
    vpii v;
    v.pb(mp(a.m, a.n));
    v.pb(mp(a.o, a.p));
    v.pb(mp(b.m, b.n));
    v.pb(mp(b.o, b.p));
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    Node ans = {v[0].f, v[0].s, v[1].f, v[1].s};
    return ans;
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { cin >> arr[i]; arr[i+a] = arr[i]; }
    F0R(i, a) { cin >> h[i]; h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%d", &maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Segtree {
    int arr[8*MAX];
    int tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { tree[node] = arr[a]; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = max(tree[node*2], tree[node*2+1]);
    }

    int query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { return -INF; }
        if (i <= a && b <= j) { return tree[node]; }
        int x = query(2*node, a, (a+b)/2, i, j);
        int y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return max(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { cin >> arr[i]; arr[i+a] = arr[i]; }
    F0R(i, a) { cin >> h[i]; h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        FOR(i, nx, ny+1) { FOR(j, nx, ny+1) { if (i == j) { continue; } maxx = max(maxx, frnt[i] + bck[j]); } }
        /*
        cout << nx << " " << ny << endl;
        cout << s1.query(1, 0, 2*a-1, nx, ny) + s2.query(1, 0, 2*a-1, nx, ny) << endl;
        */
        cout << maxx << endl;
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {
    if (a.m >= b.m) {
        if (a.o >= b.m) {
            return {a.m, a.n, a.o, a.p};
        }
        return {a.m, a.n, b.m, b.n};
    }
    if (b.o >= a.m) { return {b.m, b.n, b.o, b.p}; }
    return {b.m, b.n, a.m, a.n};
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
main() {
    int a, b; scanf("%I64d %I64d", &a, &b);
    F0R(i, a) { scanf("%I64d", &arr[i]); arr[i+a] = arr[i]; }
    F0R(i, a) { scanf("%I64d", &h[i]); h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%I64d\n", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {
    vpii v;
    v.pb(mp(a.m, a.n));
    v.pb(mp(a.o, a.p));
    v.pb(mp(b.m, b.n));
    v.pb(mp(b.o, b.p));
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    Node ans = {v[0].f, v[0].s, v[1].f, v[1].s};
    return ans;
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { cin >> arr[i]; arr[i+a] = arr[i]; }
    F0R(i, a) { cin >> h[i]; h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        cout << maxx << endl;
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <ll, ll>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const ll INF = 10000000000000007;
int arr[2*MAX];
int h[2*MAX];
ll pref[2*MAX];
ll frnt[2*MAX];
ll bck[2*MAX];

struct Node {
    ll m, n, o, p;
};

Node combine(Node a, Node b) {
    if (a.m >= b.m) {
        if (a.o >= b.m) {
            return {a.m, a.n, a.o, a.p};
        }
        return {a.m, a.n, b.m, b.n};
    }
    if (b.o >= a.m) { return {b.m, b.n, b.o, b.p}; }
    return {b.m, b.n, a.m, a.n};
}
struct Segtree {
    ll arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { scanf("%d", &arr[i]); arr[i+a] = arr[i]; }
    F0R(i, a) { scanf("%d", &h[i]); h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        ll maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%I64d\n", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair <int, int>
#define piii pair<pii, int>
#define vi vector<ll>
#define vpii vector<pii>

#define read1(a) int a; scanf("%d", &a)
#define read2(a, b) int a, b; scanf("%d %d", &a, &b)
#define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)

#define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
#define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }

#define f first
#define s second

#define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);

#define println1(a) printf("%d\n", a);
#define println2(a, b) printf("%d %d\n", a, b);
#define println3(a, b, c) printf("%d %d %d\n", a, b, c);
#define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");

const int MOD = 2000000007;
const int MAX = 100005;
const int INF = 1000000007;
int arr[2*MAX];
int h[2*MAX];
int pref[2*MAX];
int frnt[2*MAX];
int bck[2*MAX];

struct Node {
    int m, n, o, p;
};

Node combine(Node a, Node b) {
    vpii v;
    v.pb(mp(a.m, a.n));
    v.pb(mp(a.o, a.p));
    v.pb(mp(b.m, b.n));
    v.pb(mp(b.o, b.p));
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    Node ans = {v[0].f, v[0].s, v[1].f, v[1].s};
    return ans;
}
struct Segtree {
    int arr[8*MAX];
    Node tree[8*MAX];
    void build(int node, int a, int b) {
        if (a == b) { Node i = {arr[a], a, -INF, -1}; tree[node] = i; return; }
        build(2*node, a, (a+b)/2);
        build(2*node+1, (a+b)/2 + 1, b);
        tree[node] = combine(tree[node*2], tree[node*2+1]);
    }

    Node query(int node, int a, int b, int i, int j) {
        if (a > j || b < i) { Node n = {-INF, -1, -INF, -1}; return n; }
        if (i <= a && b <= j) { return tree[node]; }
        Node x = query(2*node, a, (a+b)/2, i, j);
        Node y = query(2*node+1, (a+b)/2 + 1, b, i, j);
        return combine(x, y);
    }
};

Segtree s1, s2;
int main() {
    read2(a, b);
    F0R(i, a) { cin >> arr[i]; arr[i+a] = arr[i]; }
    F0R(i, a) { cin >> h[i]; h[i+a] = h[i]; }

    /*
    cout << "arr: "; F0R(i, 2*a) { cout << arr[i] << " "; } cout << endl;
    cout << "h: "; F0R(i, 2*a) { cout << h[i] << " "; } cout << endl;
    */

    FOR(i, 1, 2*a+1) { pref[i] = pref[i-1] + arr[i-1]; }

    /*
    cout << "pref: "; F0R(i, 2*a+1) { cout << pref[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) {
        frnt[i] = pref[i] + 2 * h[i];
        bck[i] = 2 * h[i] - pref[i];
    }

    /*
    cout << "frnt: "; F0R(i, 2*a) { cout << frnt[i] << " "; } cout << endl;
    cout << "bck: "; F0R(i, 2*a) { cout << bck[i] << " "; } cout << endl;
    */

    F0R(i, 2*a) { s1.arr[i] = frnt[i]; s2.arr[i] = bck[i]; }
    s1.build(1, 0, 2*a-1);
    s2.build(1, 0, 2*a-1);

    F0R(i, b) {
        read2(x, y);
        x--; y--;
        int nx, ny;
        if (x > y) { nx = y+1; ny = x-1; }
        else { nx = y+1; ny = x-1+a; }
        int maxx = -1;
        Node n1 = s1.query(1, 0, 2*a-1, nx, ny);
        Node n2 = s2.query(1, 0, 2*a-1, nx, ny);
        vpii v1 = { mp(n1.m, n1.n), mp(n1.o, n1.p) };
        vpii v2 = { mp(n2.m, n2.n), mp(n2.o, n2.p) };

        for (pii i : v1) {
            for (pii j : v2) {
                if (i.s != j.s) { maxx = max(maxx, i.f + j.f); }
            }
        }

        printf("%d\n", maxx);
    }
}
/*
5 100
1 2 3 4 5
3 5 2 1 4
*/


#include<bits/stdc++.h>
#define int long long
#define lch(p) p << 1
#define rch(p) p << 1 | 1
#define Inf 0x3f3f3f3f3f3f3f3f
#define ll long long
#define MaxN 1000010
using namespace std;

int n, q;
ll d[MaxN], h[MaxN], a[MaxN<<2], b[MaxN<<2], s[MaxN<<2];

struct node {
	ll a, b, s;
	node() {a = b = s = -Inf;}
};

void build(int p, int L, int R) {
	if (L == R) {
		a[p] = h[L] + d[L - 1];
		b[p] = h[L] - d[L - 1];
		s[p] = -Inf;
		return;
	}
	int mid = (L + R) / 2;
	build(lch(p), L, mid);
	build(rch(p), mid + 1, R);
	a[p] = max(a[lch(p)], a[rch(p)]);
	b[p] = max(b[lch(p)], b[rch(p)]);
	s[p] = max(max(s[lch(p)], s[rch(p)]), b[lch(p)] + a[rch(p)]);
}

node query(int p, int L, int R, int l, int r) {
	if (l <= L && R <= r) {
		node res; res.a = a[p]; res.b = b[p]; res.s = s[p];
		return res;
	}
	int mid = (L + R) / 2;
	node x, y, z;
	if (l <= mid) x = query(lch(p), L, mid, l, r);
	if (r > mid) y = query(rch(p), mid + 1, R, l, r);
	z.a = max(x.a, y.a); z.b = max(x.b, y.b);
	z.s = max(max(x.s, y.s), x.b + y.a);
	return z;
}

signed main() {
	scanf("%lld%lld", &n, &q);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &d[i]);
		d[n + i] = d[i];
	}
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &h[i]);
		h[i] *= 2; h[n + i] = h[i];
	}
	for (int i = 1; i <= 2 * n; i++)
		d[i] += d[i - 1];
	build(1, 1, n * 2);
	while (q--) {
		int l, r;
		scanf("%lld%lld", &l, &r);
		if (l <= r) printf("%lld\n", query(1, 1, n * 2, r + 1, l + n - 1).s);
		else printf("%lld\n", query(1, 1, n * 2, r + 1, l - 1).s);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll INF = 1e17;
const int N = 4e5+10;

ll d[N], ld[N], rd[N], h[N];
pll segpl[N], segml[N];
pll segpr[N], segmr[N];

void build(int n)
{
    for (int i = 0; i < n; ++i) {
        segpl[n+i] = {2LL*h[i]+ld[i], i};
        segml[n+i] = {2LL*h[i]-ld[i], i};
        segpr[n+i] = {2LL*h[i]+rd[i], i};
        segmr[n+i] = {2LL*h[i]-rd[i], i};
    }
    for (int i = n-1; i > 0; --i) {
        segpl[i] = max(segpl[i<<1], segpl[i<<1|1]);
        segml[i] = max(segml[i<<1], segml[i<<1|1]);
        segpr[i] = max(segpr[i<<1], segpr[i<<1|1]);
        segmr[i] = max(segmr[i<<1], segmr[i<<1|1]);
    }
}

pll query(pll seg[], int n, int l, int r)
{
    pll ans = {-INF, -INF};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) ans = max(ans, seg[l++]);
        if (r&1) ans = max(ans, seg[--r]);
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &d[i]);
        d[n+i] = d[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &h[i]);
        h[n+i] = h[i];
    }

    for (int i = 1; i < 2*n; ++i)
        ld[i] = ld[i-1] + d[i-1];
    for (int i = 2*n-2; i >= 0; --i)
        rd[i] = rd[i+1] + d[i];
    build(2*n);

    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        if (a <= b) {
            int na = b+1;
            int nb = n+a;
            a = na; b = nb;
        } else {
            int na = b+1;
            int nb = a;
            a = na; b = nb;
        }

        pll l = query(segml, 2*n, a, b);
        pll r = query(segpl, 2*n, l.second+1, b);
        ll ans = l.first + r.first;
        /*r = query(segmr, 2*n, a, b);
        l = query(segpr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);*/

        printf("%I64d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll INF = 1e17;
const int N = 2e5+10;

ll d[N], ld[N], rd[N], h[N];
pll segpl[N], segml[N];
pll segpr[N], segmr[N];

void build(int n)
{
    for (int i = 0; i < n; ++i) {
        segpl[n+i] = {2LL*h[i]+ld[i], i};
        segml[n+i] = {2LL*h[i]-ld[i], i};
        segpr[n+i] = {2LL*h[i]+rd[i], i};
        segmr[n+i] = {2LL*h[i]-rd[i], i};
    }
    for (int i = n-1; i > 0; --i) {
        segpl[i] = max(segpl[i<<1], segpl[i<<1|1]);
        segml[i] = max(segml[i<<1], segml[i<<1|1]);
        segpr[i] = max(segpr[i<<1], segpr[i<<1|1]);
        segmr[i] = max(segmr[i<<1], segmr[i<<1|1]);
    }
}

pll query(pll seg[], int n, int l, int r)
{
    pll ans = {-INF, -INF};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) ans = max(ans, seg[l++]);
        if (r&1) ans = max(ans, seg[--r]);
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &d[i]);
        d[n+i] = d[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%d", &h[i]);
        h[n+i] = h[i];
    }

    for (int i = 1; i < 2*n; ++i)
        ld[i] = ld[i-1] + d[i-1];
    for (int i = 2*n-2; i >= 0; --i)
        rd[i] = rd[i+1] + d[i];
    build(2*n);

    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        if (a <= b) {
            int na = b+1;
            int nb = n+a;
            a = na; b = nb;
        } else {
            int na = b+1;
            int nb = a;
            a = na; b = nb;
        }

        pll l = query(segml, 2*n, a, b);
        pll r = query(segpl, 2*n, l.second+1, b);
        ll ans = l.first + r.first;
        r = query(segmr, 2*n, a, b);
        l = query(segpr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);

        l = query(segpl, 2*n, a, b);
        r = query(segml, 2*n, l.second+1, b);
        ans = max(ans, l.first + r.first);
        r = query(segpr, 2*n, a, b);
        l = query(segmr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);

        printf("%I64d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll INF = 1e17;
const int N = 4e5+10;

ll d[N], ld[N], rd[N], h[N];
pll segpl[N], segml[N];
pll segpr[N], segmr[N];

void build(int n)
{
    for (int i = 0; i < n; ++i) {
        segpl[n+i] = {2LL*h[i]+ld[i], i};
        segml[n+i] = {2LL*h[i]-ld[i], i};
        segpr[n+i] = {2LL*h[i]+rd[i], i};
        segmr[n+i] = {2LL*h[i]-rd[i], i};
    }
    for (int i = n-1; i > 0; --i) {
        segpl[i] = max(segpl[i<<1], segpl[i<<1|1]);
        segml[i] = max(segml[i<<1], segml[i<<1|1]);
        segpr[i] = max(segpr[i<<1], segpr[i<<1|1]);
        segmr[i] = max(segmr[i<<1], segmr[i<<1|1]);
    }
}

pll query(pll seg[], int n, int l, int r)
{
    pll ans = {-INF, -INF};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) ans = max(ans, seg[l++]);
        if (r&1) ans = max(ans, seg[--r]);
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &d[i]);
        d[n+i] = d[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &h[i]);
        h[n+i] = h[i];
    }

    for (int i = 1; i < 2*n; ++i)
        ld[i] = ld[i-1] + d[i-1];
    for (int i = 2*n-2; i >= 0; --i)
        rd[i] = rd[i+1] + d[i];
    build(2*n);

    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        if (a <= b) {
            int na = b+1;
            int nb = n+a;
            a = na; b = nb;
        } else {
            int na = b+1;
            int nb = a;
            a = na; b = nb;
        }

        pll l = query(segml, 2*n, a, b);
        pll r = query(segpl, 2*n, l.second+1, b);
        ll ans = l.first + r.first;
        r = query(segmr, 2*n, a, b);
        l = query(segpr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);

        printf("%I64d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll INF = 1e17;
const int N = 2e5+10;

ll d[N], ld[N], rd[N], h[N];
pll segpl[N], segml[N];
pll segpr[N], segmr[N];

void build(int n)
{
    for (int i = 0; i < n; ++i) {
        segpl[n+i] = {2LL*h[i]+ld[i], i};
        segml[n+i] = {2LL*h[i]-ld[i], i};
        segpr[n+i] = {2LL*h[i]+rd[i], i};
        segmr[n+i] = {2LL*h[i]-rd[i], i};
    }
    for (int i = n-1; i > 0; --i) {
        segpl[i] = max(segpl[i<<1], segpl[i<<1|1]);
        segml[i] = max(segml[i<<1], segml[i<<1|1]);
        segpr[i] = max(segpr[i<<1], segpr[i<<1|1]);
        segmr[i] = max(segmr[i<<1], segmr[i<<1|1]);
    }
}

pll query(pll seg[], int n, int l, int r)
{
    pll ans = {-INF, -INF};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) ans = max(ans, seg[l++]);
        if (r&1) ans = max(ans, seg[--r]);
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &d[i]);
        d[n+i] = d[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &h[i]);
        h[n+i] = h[i];
    }

    for (int i = 1; i < 2*n; ++i)
        ld[i] = ld[i-1] + d[i-1];
    for (int i = 2*n-2; i >= 0; --i)
        rd[i] = rd[i+1] + d[i];
    build(2*n);

    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        if (a <= b) {
            int na = b+1;
            int nb = n+a;
            a = na; b = nb;
        } else {
            int na = b+1;
            int nb = a;
            a = na; b = nb;
        }

        pll l = query(segml, 2*n, a, b);
        pll r = query(segpl, 2*n, l.second+1, b);
        ll ans = l.first + r.first;
        r = query(segmr, 2*n, a, b);
        l = query(segpr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);

        printf("%I64d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll INF = 1e17;
const int N = 2e5+10;

ll d[N], ld[N], rd[N], h[N];
pll segpl[N], segml[N];
pll segpr[N], segmr[N];

void build(int n)
{
    for (int i = 0; i < n; ++i) {
        segpl[n+i] = {2LL*h[i]+ld[i], i};
        segml[n+i] = {2LL*h[i]-ld[i], i};
        segpr[n+i] = {2LL*h[i]+rd[i], i};
        segmr[n+i] = {2LL*h[i]-rd[i], i};
    }
    for (int i = n-1; i > 0; --i) {
        segpl[i] = max(segpl[i<<1], segpl[i<<1|1]);
        segml[i] = max(segml[i<<1], segml[i<<1|1]);
        segpr[i] = max(segpr[i<<1], segpr[i<<1|1]);
        segmr[i] = max(segmr[i<<1], segmr[i<<1|1]);
    }
}

pll query(pll seg[], int n, int l, int r)
{
    pll ans = {-INF, -INF};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) ans = max(ans, seg[l++]);
        if (r&1) ans = max(ans, seg[--r]);
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &d[i]);
        d[n+i] = d[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &h[i]);
        h[n+i] = h[i];
    }

    for (int i = 1; i < 2*n; ++i)
        ld[i] = ld[i-1] + d[i-1];
    for (int i = 2*n-2; i >= 0; --i)
        rd[i] = rd[i+1] + d[i];
    build(2*n);

    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        if (a <= b) {
            int na = b+1;
            int nb = n+a;
            a = na; b = nb;
        } else {
            int na = b+1;
            int nb = a;
            a = na; b = nb;
        }

        pll l = query(segml, 2*n, a, b);
        pll r = query(segpl, 2*n, l.second+1, b);
        ll ans = l.first + r.first;
        r = query(segmr, 2*n, a, b);
        l = query(segpr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);

        l = query(segpl, 2*n, a, b);
        r = query(segml, 2*n, l.second+1, b);
        ans = max(ans, l.first + r.first);
        r = query(segpr, 2*n, a, b);
        l = query(segmr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);

        printf("%I64d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll INF = 1e17;
const int N = 4e5+10;

ll d[N], ld[N], rd[N], h[N];
pll segpl[N], segml[N];
pll segpr[N], segmr[N];

void build(int n)
{
    for (int i = 0; i < n; ++i) {
        segpl[n+i] = {2LL*h[i]+ld[i], i};
        segml[n+i] = {2LL*h[i]-ld[i], i};
        segpr[n+i] = {2LL*h[i]+rd[i], i};
        segmr[n+i] = {2LL*h[i]-rd[i], i};
    }
    for (int i = n-1; i > 0; --i) {
        segpl[i] = max(segpl[i<<1], segpl[i<<1|1]);
        segml[i] = max(segml[i<<1], segml[i<<1|1]);
        segpr[i] = max(segpr[i<<1], segpr[i<<1|1]);
        segmr[i] = max(segmr[i<<1], segmr[i<<1|1]);
    }
}

pll query(pll seg[], int n, int l, int r)
{
    pll ans = {-INF, -INF};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) ans = max(ans, seg[l++]);
        if (r&1) ans = max(ans, seg[--r]);
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &d[i]);
        d[n+i] = d[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", &h[i]);
        h[n+i] = h[i];
    }

    for (int i = 1; i < 2*n; ++i)
        ld[i] = ld[i-1] + d[i-1];
    for (int i = 2*n-2; i >= 0; --i)
        rd[i] = rd[i+1] + d[i];
    build(2*n);

    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        if (a <= b) {
            int na = b+1;
            int nb = n+a;
            a = na; b = nb;
        } else {
            int na = b+1;
            int nb = a;
            a = na; b = nb;
        }

        pll l = query(segml, 2*n, a, b);
        pll r = query(segpl, 2*n, l.second+1, b);
        ll ans = l.first + r.first;
        r = query(segpl, 2*n, a, b);
        l = query(segml, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);
        /*r = query(segmr, 2*n, a, b);
        l = query(segpr, 2*n, a, r.second);
        ans = max(ans, l.first + r.first);*/

        printf("%I64d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long inf = 1e15;

typedef long long LL;

template <int N>
class SegmentTree {
  struct node {
    LL down, up, mx;
    void def(LL d, LL u) {
      down = d, up = u, mx = - inf;
    }
  } tree[N * 8];
  int M;
  void pushup(int x) {
    LL A = max(tree[x << 1].mx, tree[x << 1 | 1].mx);
    LL B = tree[x << 1].down + tree[x << 1 | 1].up;
    tree[x].mx = max(A, B);
    tree[x].down = max(tree[x << 1].down, tree[x << 1 | 1].down);
    tree[x].up = max(tree[x << 1].up, tree[x << 1 | 1].up);
  }
 public:
  void build(int n, LL d[], LL u[]) {
    for (M = 1; M < n + 2; M <<= 1);
    for (int i = 1; i <= n; i ++) {
      tree[i + M].def(d[i], u[i]);
    }
    for (int i = M; i; i --)
      pushup(i);
  }
  LL get(int s, int t) {
    LL d = -inf, u = -inf, mx = -inf;
    for (s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
      if (~ s & 1) {
        mx = max(mx, max(d + tree[s ^ 1].up, tree[s ^ 1].mx));
        d = max(d, tree[s ^ 1].down);
      }
      if (  t & 1) {
        mx = max(mx, max(tree[t ^ 1].down + u, tree[t ^ 1].mx));
        u = max(u, tree[t ^ 1].up);
      }
    }
    return max(mx, d + u);
  }
};

SegmentTree <200100> segtree;
int d[100100], h[100100];
LL down[200100], up[200100];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i ++) {
    scanf("%d", d + i);
  }
  for (int i = 1; i <= n; i ++) {
    scanf("%d", h + i);
  }
  LL sum = 0;
  for (int i = 1; i <= 2 * n; i ++) {
    int p = i <= n ? i : i - n;
    sum += d[p - 1];
    if (i - 1 == n) sum += d[n];
    down[i] = -sum + h[p] * 2;
    up[i] = sum + h[p] * 2;
  }
  segtree.build(2 * n, down, up);
  while (m --) {
    int a, b, l, r;
    scanf("%d %d", &a, &b);
    if (a <= b) {
      l = b + 1, r = a + n - 1;
    } else {
      l = b + 1, r = a - 1;
    }
    printf("%lld\n", segtree.get(l, r));
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long inf = 1e15;

typedef long long LL;

template <int N>
class SegmentTree {
  struct node {
    LL down, up, mx;
    void def(LL d, LL u) {
      down = d, up = u, mx = - inf;
    }
  } tree[N * 8];
  int M;
  void pushup(int x) {
    LL A = max(tree[x << 1].mx, tree[x << 1 | 1].mx);
    LL B = tree[x << 1].down + tree[x << 1 | 1].up;
    tree[x].mx = max(A, B);
    tree[x].down = max(tree[x << 1].down, tree[x << 1 | 1].down);
    tree[x].up = max(tree[x << 1].up, tree[x << 1 | 1].up);
  }
 public:
  void build(int n, LL d[], LL u[]) {
    for (M = 1; M < n + 2; M <<= 1);
    for (int i = 1; i <= n; i ++) {
      tree[i + M].def(d[i], u[i]);
    }
    for (int i = M; i; i --)
      pushup(i);
  }
  LL get(int s, int t) {
    LL d = -inf, u = -inf, mx = -inf;
    for (s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
      if (~ s & 1) {
        mx = max(mx, max(d + tree[s ^ 1].up, tree[s ^ 1].mx));
        d = max(d, tree[s ^ 1].down);
      }
      if (  t & 1) {
        mx = max(mx, max(tree[t ^ 1].down + u, tree[t ^ 1].mx));
        u = max(u, tree[t ^ 1].up);
      }
    }
    return max(mx, d + u);
  }
};

SegmentTree <200100> segtree;
int d[100100], h[100100];
LL down[200100], up[200100];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i ++) {
    scanf("%d", d + i);
  }
  for (int i = 1; i <= n; i ++) {
    scanf("%d", h + i);
  }
  LL sum = 0;
  for (int i = 1; i <= 2 * n; i ++) {
    int p = i <= n ? i : i - n;
    sum += d[p - 1];
    if (i - 1 == n) sum += d[n];
    down[i] = -sum + h[p] * 2;
    up[i] = sum + h[p] * 2;
  }
  segtree.build(2 * n, down, up);
  while (m --) {
    int a, b, l, r;
    scanf("%d %d", &a, &b);
    if (a <= b) {
      l = b + 1, r = a + n - 1;
    } else {
      l = a + 1, r = b - 1;
    }
    printf("%lld\n", segtree.get(l, r));
  }
  return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define N 100001
int h[N],d[N],d1[N],dn[N];
struct st
{
    int m1,m2,xx,yy,m3;
}tree[4*N];
int calc(int x,int y)
{

    return 2*h[x]+2*h[y]+d1[y]-d1[x];
}
int build(int node,int i,int j)
{
    if(i==j)
    {
        tree[node].m1=2*h[i]+d1[i];
        tree[node].m2=2*h[i]-d1[i];
        tree[node].m3=2*h[i]+dn[i];
        tree[node].yy=i;
        tree[node].xx=i;
    }
    else
    {
        int mid=(i+j)/2;
        build(2*node,i,mid);
        build(2*node+1,mid+1,j);
        tree[node].m1=max(tree[2*node].m1,tree[2*node+1].m1);
        tree[node].m2=max(tree[2*node].m2,tree[2*node+1].m2);
        tree[node].m3=max(tree[2*node].m3,tree[2*node+1].m3);

        int xx1=tree[2*node].xx,  xx2=tree[2*node+1].xx,  yy1=tree[2*node].yy,  yy2=tree[2*node+1].yy,xf,yf;
        int t1=calc(xx1,xx2);
        xf=xx1,yf=xx2;

        int t2=calc(xx1,yy2);
        if(t2>t1)
            xf=xx1,yf=yy2;

        int t3=calc(yy1,xx2);
        if(t3>t1 && t3>t2)
            xf=yy1,yf=xx2;

        int t4=calc(yy1,yy2);
        if(t4>t1 && t4>t2 && t4>t3)
            xf=yy1,yf=yy2;

        int t5=calc(xx1,yy1);
        if(t5>t1 && t5>t2 && t5>t3 && t5>t4 && xx1!=yy1)
            xf=xx1,yf=yy1;

        int t6=calc(xx2,yy2);
        if(t6>t1 && t6>t2 && t6>t3 && t6>t4 && t6>t5 && xx2!=yy2)
            xf=xx2,yf=yy2;

        tree[node].xx=xf,tree[node].yy=yf;

    }
}
struct st query(int node,int i,int j,int a,int b)
{
    if(a>b)
        return {0,0,0,0,0};
    if(i==a && j==b)
        return tree[node];
    int mid=(i+j)/2;
    if(b<=mid)
        return query(2*node,i,mid,a,b);
    if(a>mid)
        return query(2*node+1,mid+1,j,a,b);
    struct st z1=query(2*node,i,mid,a,mid);
    struct st z2=query(2*node+1,mid+1,j,mid+1,b);
    struct st z;
        z.m1=max(z1.m1,z2.m1);
        z.m2=max(z1.m2,z2.m2);
        z.m3=max(z1.m3,z2.m3);

        int xx1=z1.xx,  xx2=z2.xx,  yy1=z1.yy,  yy2=z2.yy,xf,yf;
        int t1=calc(xx1,xx2);
        xf=xx1,yf=xx2;

        int t2=calc(xx1,yy2);
        if(t2>t1)
            xf=xx1,yf=yy2;

        int t3=calc(yy1,xx2);
        if(t3>t1 && t3>t2)
            xf=yy1,yf=xx2;

        int t4=calc(yy1,yy2);
        if(t4>t1 && t4>t2 && t4>t3)
            xf=yy1,yf=yy2;

        int t5=calc(xx1,yy1);
        if(t5>t1 && t5>t2 && t5>t3 && t5>t4 && xx1!=yy1)
            xf=xx1,yf=yy1;

        int t6=calc(xx2,yy2);
        if(t6>t1 && t6>t2 && t6>t3 && t6>t4 && t6>t5 && xx2!=yy2)
            xf=xx2,yf=yy2;

        z.xx=xf,z.yy=yf;
        return z;
}

int main()
{
    int n,m,i,a,b,ans=0;
    struct st z,ff,gg;
    scanf("%d %d",&n,&m);
    for(i=1;i<=n;i++)
        scanf("%d",&d[i]);

    for(i=1;i<=n;i++)
        scanf("%d",&h[i]);
    h[0]=d1[0]=dn[0]=d[0]=0;

    d1[1]=0;
    for(i=2;i<=n;i++)
        d1[i]=d1[i-1]+d[i-1];

   // cout<<d1[5]<<endl;

    dn[n]=0;
    for(i=n-1;i>=1;i--)
        dn[i]=d[i]+dn[i+1];
        //cout<<h[4]<<" "<<h[5]<<" "<<d1[5]<<" "<<d1[4]<<endl;
    build(1,1,n);
    //cout<<tree[4].xx<<" "<<tree[4].yy<<" "<<tree[5].xx<<" "<<tree[5].yy<<endl;
    for(i=1;i<=m;i++)
    {
        scanf("%d %d",&a,&b);
        //cout<<calc(2,3)<<endl;

        if(a>b)
        {
           z=query(1,1,n,b+1,a-1);
           printf("%d",calc(z.xx,z.yy));
        }
        else
        {
           ff=query(1,1,n,1,a-1);
           gg=query(1,1,n,b+1,n);
           if(i==m)
            {
                //cout<<"*"<<ff.xx<<" "<<ff.yy<<" "<<calc(ff.xx,ff.yy)<<endl;
                //cout<<"#"<<gg.xx<<" "<<gg.yy<<" "<<calc(gg.xx,gg.yy)<<endl;
            }
           ans=0;
           if(a!=1 && b!=n)
            {//if(i==m)cout<<"*";
            ans=ff.m1+gg.m3+d[n];}
            //cout<<"^"<<ans<<endl;
           ans=max(ans,max(calc(ff.xx,ff.yy),calc(gg.xx,gg.yy)));
           printf("%d",ans);
        }
        printf("\n");

    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <stack>
#include <cstring>

#define SZ(x) (( (int) x.size() ))

using namespace std;

void calc()
{
	long double a,b;
	cin >> a >> b;

	long double s = 2*a*b;
	long double ans = a*b;

	long double h1 = a;
	long double w = min( a/4.0 , b ) ;
	long double h2 = a - w*4 ;

	ans += ( h1 + h2 ) * w / 2.0 ;

	cout << fixed  << setprecision(7) << ans / s << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>> t;
	for(int i  = 0 ; i < t; i++ )
		calc();
}


#include<iostream>
#include<algorithm>
#include<cassert>
#include<iomanip>
#include<cmath>

#define SZ(c) int((c).size())
#define foreach(it,c) for(__typeof (c).begin() it=(c).begin(); it!=(c).end(); it++)
#define WAIT cout<<flush, system("PAUSE")
using namespace std;
typedef double ld;
#define MAX 11

int T;
ld sol, a, b;
ld tn, sa, sb;

int main(){
   ios_base::sync_with_stdio(0);
   //cin.tie(0);

   cout<<setprecision(10)<<fixed;
   tn = tan(M_PI/8);

   cin>>T;
   while(T--){
      sol = 0;
      cin >> a >> b;

      if (b==0){
         cout<<1<<"\n";
         continue;
      }

      if (a==0){
         cout<<0.5<<"\n";
         continue;
      }

      sol += a*b;
      if (b>=a/4){
         sa = a;
         sb = a/4;

         //cout<<sa<<" "<<sb<<endl;

         sol += (sa*sb)/2;
      }else{

         sb = b;
         //sb/sa=tn
         sa = sb/tn;

         //cout<<sa<<" "<<sb<<endl;

         sol += (a*b)-(sa*sb)/2;
      }
      sol /= 2*(a*b);

      cout<<sol<<"\n";
   }
   // MUST comment the extra prints
   //WAIT;
}


#include<iostream>
#include<algorithm>
#include<cassert>
#include<iomanip>
#include<cmath>

#define SZ(c) int((c).size())
#define foreach(it,c) for(__typeof (c).begin() it=(c).begin(); it!=(c).end(); it++)
#define WAIT cout<<flush, system("PAUSE")
using namespace std;
#define MAX 11

int T;
double sol, a, b;
double tn, sa, sb;

int main(){
   ios_base::sync_with_stdio(0);
   cin.tie(0);

   cout<<setprecision(10)<<fixed;
   tn = tan(M_PI/8);

   cin>>T;
   while(T--){
      sol = 0;
      cin >> a >> b;
      sol += a*b;
      if (b>=a/4){
         sa = a;
         sb = a/4;

         //cout<<sa<<" "<<sb<<endl;

         sol += (sa*sb)/2;
      }else{

         sb = b;
         //sb/sa=tn
         sa = sb/tn;

         //cout<<sa<<" "<<sb<<endl;

         sol += (a*sb)-(sa*sb)/2;
      }
      sol /= 2*(a*b);

      cout<<sol<<"\n";
   }
   // MUST comment the extra prints
   //WAIT;
}


#include<iostream>
#include<algorithm>
#include<cassert>
#include<iomanip>
#include<cmath>

#define SZ(c) int((c).size())
#define foreach(it,c) for(__typeof (c).begin() it=(c).begin(); it!=(c).end(); it++)
#define WAIT cout<<flush, system("PAUSE")
using namespace std;
typedef double ld;
#define MAX 11

int T;
ld sol, a, b;
ld tn, sa, sb;

int main(){
   ios_base::sync_with_stdio(0);
   //cin.tie(0);

   cout<<setprecision(10)<<fixed;
   tn = tan(M_PI/8);

   cin>>T;
   while(T--){
      sol = 0;
      cin >> a >> b;
      if (b==0){
         cout<<1<<"\n";
         continue;
      }
      if (a==0){
         cout<<0<<"\n";
         continue;
      }

      sol += a*b;
      if (b>=a/4){
         sa = a;
         sb = a/4;

         //cout<<sa<<" "<<sb<<endl;

         sol += (sa*sb)/2;
      }else{

         sb = b;
         //sb/sa=tn
         sa = sb/tn;

         //cout<<sa<<" "<<sb<<endl;

         sol += (a*b)-(sa*sb)/2;
      }
      sol /= 2*(a*b);

      cout<<sol<<"\n";
   }
   // MUST comment the extra prints
   //WAIT;
}


#include<iostream>
#include<algorithm>
#include<cassert>
#include<iomanip>
#include<cmath>

#define SZ(c) int((c).size())
#define foreach(it,c) for(__typeof (c).begin() it=(c).begin(); it!=(c).end(); it++)
#define WAIT cout<<flush, system("PAUSE")
using namespace std;
typedef double ld;
#define MAX 11

int T;
ld sol, a, b;
ld tn, sa, sb;

int main(){
   ios_base::sync_with_stdio(0);
   //cin.tie(0);

   cout<<setprecision(10)<<fixed;
   //tn = tan(M_PI/8);
   tn = 0.25;
   //cout<<tn<<endl;

   cin>>T;
   while(T--){
      sol = 0;
      cin >> a >> b;

      if (b==0){
         cout<<1<<"\n";
         continue;
      }

      if (a==0){
         cout<<0.5<<"\n";
         continue;
      }

      sol += a*b;
      if (b>=a/4){
         sa = a;
         sb = a/4;

         //cout<<sa<<" "<<sb<<endl;

         sol += (sa*sb)/2;
      }else{

         sb = b;
         //sb/sa=tn
         sa = sb/tn;

         //cout<<sa<<" "<<sb<<endl;

         sol += (a*b)-(sa*sb)/2;
      }
      sol /= 2*(a*b);

      cout<<sol<<"\n";
   }
   // MUST comment the extra prints
   //WAIT;
}


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    int T;
    int i, j, k;
    double a, b;
    for(scanf("%d",&T); T; T--)
    {
        scanf("%lf%lf",&a,&b);
        if(fabs(b) < 1e-5) printf("1.000000000000\n");
        else if(fabs(a) < 1e-5) printf("0.500000000000\n");
        else if(a >= 4*b)
        {
            printf("%.9lf\n", (a*b+(a+(a-4*b))*b/2)/(2*a*b));
        }
        else
            printf("%.9lf\n", (b*a + 0.5*a*a/4)/(2*a*b));
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    int T;
    int i, j, k;
    double a, b;
    for(scanf("%d",&T); T; T--)
    {
        scanf("%lf%lf",&a,&b);
        if(fabs(b) < 1e-5) printf("1.000000000000\n");
        else if(fabs(a) < 1e-5)
        {
            if(b < -1e-7) printf("1.000000000000\n");
            else printf("0.0000000000\n");
        }
        else if(a >= 4*b)
        {
            printf("%.9lf\n", (a*b+(a+(a-4*b))*b/2)/(2*a*b));
        }
        else
            printf("%.9lf\n", (b*a + 0.5*a*a/4)/(2*a*b));
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    int T;
    int i, j, k;
    double a, b;
    for(scanf("%d",&T); T; T--)
    {
        scanf("%lf%lf",&a,&b);
        if(fabs(b) < 1e-5) printf("1.000000000000\n");
        else if(fabs(a) < 1e-5)
        {
            if(b < -1e-7) printf("1.000000000000\n");
            else printf("0.0000000000\n");
        }
        else if(a >= 4*b)
        {
            printf("%.9lf\n", (a+(a-4*b))*b/(2*a*b));
        }
        else
            printf("%.9lf\n", (b*a + 0.5*a*a/4)/(2*a*b));
    }
    return 0;
}


#include <cstdio>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		int a, b;
		scanf("%d%d", &a, &b);
		double p;
		if(a == 0 && b == 0) {
			p = 1;
		}
		else if(a == 0) {
			p = 0.5;
		}
		else if(b == 0) {
			p = 0;
		}
		else if(4 * b >= a) {
			p = (b + b + a / 4.0) / (4.0 * b);
		}
		else {
			p = 1 - b * 1.0 / a;
		}
		printf("%.8lf\n", p);
	}
	return 0;
}


#include <cstdio>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		int a, b;
		scanf("%d%d", &a, &b);
		double p;
		if(4 * b >= a) {
			p = (b + b + a / 4.0) / (4.0 * b);
		}
		else {
			p = 1 - b * 1.0 / a;
		}
		printf("%.8lf\n", p);
	}
	return 0;
}


#include <cstdio>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		int a, b;
		scanf("%d%d", &a, &b);
		double p;
		if(a == 0 && b == 0) {
			p = 1;
		}
		else if(a == 0) {
			p = 0.5;
		}
		else if(b == 0) {
			p = 1;
		}
		else if(4 * b >= a) {
			p = (b + b + a / 4.0) / (4.0 * b);
		}
		else {
			p = 1 - b * 1.0 / a;
		}
		printf("%.8lf\n", p);
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

#define For(i,a,b) for (int i = (int)(a); i <= (int)(b); ++i)
#define Ford(i,a,b) for (int i = (int)(a); i >= (int)(b); --i)
#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define Repd(i, n) for (int i = (int)(n); i >= 0; --i)

double go(double a, double b) {
       if (b == 0) return 1.0;
       if (a == 0) return 0.5;
       double m = min(b, a / 4.0);
       return (a * b + m * a - 2.0 * m * m) / (2.0 * a * b);
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    int t;
    cin >> t;

    double a, b;
    Rep (i, t) {
        cin >> a >> b;
        printf("%0.9lf\n", go(a, b));
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

#define For(i,a,b) for (int i = (int)(a); i <= (int)(b); ++i)
#define Ford(i,a,b) for (int i = (int)(a); i >= (int)(b); --i)
#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define Repd(i, n) for (int i = (int)(n); i >= 0; --i)

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    int t;
    cin >> t;

    double a, b;
    Rep (i, t) {
        cin >> a >> b;
        double m = min(b, a / 4.0);
        double res = (a == 0 || b == 0) ? 0.0 : (a * b + m * a - 2.0 * m * m) / (2.0 * a * b);
        printf("%0.9lf\n", res);
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

#define For(i,a,b) for (int i = (int)(a); i <= (int)(b); ++i)
#define Ford(i,a,b) for (int i = (int)(a); i >= (int)(b); --i)
#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define Repd(i, n) for (int i = (int)(n); i >= 0; --i)

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    int t;
    cin >> t;

    double a, b;
    Rep (i, t) {
        cin >> a >> b;
        double m = min(b, a / 4.0);
        double res = (a * b + m * a - 2.0 * m * m) / (2.0 * a * b);
        printf("%0.9lf\n", res);
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0 && B == 0){
          printf("%.10f\n", 1.0);
          return ;
     }
     if(B == 0) {
          printf("%.10f\n", 1.0);
          return ;
     }
     if(A == 0){
          printf("%.10f\n", 0.0);
          return ;
     }
     if(A <=  4 * B )
          printf("%.10f\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10f\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) / 2.0 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(B == 0) {
          printf("%.10f\n", 1.0);
          return ;
     }
     if(A == 0){
          printf("%.10f\n", 0.5);
          return ;
     }
     if(A < 4 * B )
          printf("%.10f\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10f\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) * 0.5 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0 && B == 0){
          printf("%.10f\n", 0.0);
          return ;
     }
     if(B == 0) {
          printf("%.10f\n", 0.0);
          return ;
     }
     if(A == 0){
          printf("%.10f\n", 0.0);
          return ;
     }
     if(A <=  4 * B )
          printf("%.10f\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10f\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) * 0.5 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T--){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0 && B == 0){
          printf("%.10f\n", 1.0);
          return ;
     }
     if(B == 0) {
          printf("%.10f\n", 0.0);
          return ;
     }
     if(A == 0){
          printf("%.10f\n", 0.0);
          return ;
     }
     if(A <=  4 * B )
          printf("%.10f\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10f\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) * 0.5 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0 && B == 0){
          printf("%.10f\n", 1.0);
          return ;
     }
     if(B == 0) {
          printf("%.10f\n", 1.0);
          return ;
     }
     if(A == 0){
          printf("%.10f\n", 0.0);
          return ;
     }
     if(A < 4 * B )
          printf("%.10f\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10f\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) * 0.5 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0 && B == 0){
          printf("%.10lf\n", 1.0);
          return ;
     }
     if(B == 0) {
          printf("%.10lf\n", 1.0);
          return ;
     }
     if(A == 0){
          printf("%.10lf\n", 0.0);
          return ;
     }
     if(a * 0.25 <=  b )
          printf("%.10lf\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10lf\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) / 2.0 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
   // while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0 && B == 0){
          printf("%.10lf\n", 1.0);
          return ;
     }
     if(B == 0) {
          printf("%.10lf\n", 1.0);
          return ;
     }
     if(A == 0){
          printf("%.10lf\n", 0.0);
          return ;
     }
     if(a * 0.25 <=  b )
          printf("%.10lf\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10lf\n", 0.5 + (((a - 4.0 * b) + a) * (a / 4.0 - b) / 2.0) / (2.0 * a * b) );
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            cin >> A >> B;
            a = A;
            b = B;
            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;

typedef double db;
db a, b;
int A, B;
void gao(){
     if(A == 0){
          printf("%.10f\n", 0.0);
          return ;
     }
     if(B == 0) {
          printf("%.10f\n", 1.0);
          return ;
     }
     if(A <=  4 * B )
          printf("%.10f\n", 0.5 + a / 16.0 / b);
     else {
          printf("%.10f\n", 0.5 + (((a - 4.0 * b) + a) * b / 2.0) / 2.0 / a / b);
     }
}
int main(){
    int T;
    cin >> T;
    while(T --){
            scanf("%d%d", &A, &B);
            a = A;
            b = B;

            //cin >> a >> b;
            gao();
    }
    //while( 1 );
    return 0;
}


/*
 * Author: iSea
 * Created Time:  2011/4/19 23:34:46
 * File Name: D.cpp
 */
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>

using namespace std;

#define PB push_back
#define PII pair<int, int>
#define SZ(x) ((int)((x).size()))
#define OUT(x) printf(#x" %d\n", x)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

const int maxn = 1000 + 10;
const int inf = (-1u) >> 1;

int main() {
    int t;
    double a, b;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%lf%lf", &a, &b);
        double all = 2 * a * b, ch;
        if (a/4 <= b) {
            ch = (b + b + a/4) * a/2;
        } else {
            ch = a*b;
            ch += (a + a-b*4) * b/2;
        }
        printf ("%.10lf\n", ch / all);
    }
    return 0;
}


/*
 * Author: iSea
 * Created Time:  2011/4/19 23:34:46
 * File Name: D.cpp
 */
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>

using namespace std;

#define PB push_back
#define PII pair<int, int>
#define SZ(x) ((int)((x).size()))
#define OUT(x) printf(#x" %d\n", x)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

const int maxn = 1000 + 10;
const int inf = (-1u) >> 1;

int main() {
    int t;
    double a, b;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%lf%lf", &a, &b);
        double all = 2 * a * b;
        double ch = (b + b + a/4) * a/2;
        printf ("%.10lf\n", ch / all);
    }
    return 0;
}


/*
 * Author: iSea
 * Created Time:  2011/4/19 23:34:46
 * File Name: D.cpp
 */
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>

using namespace std;

#define PB push_back
#define PII pair<int, int>
#define SZ(x) ((int)((x).size()))
#define OUT(x) printf(#x" %d\n", x)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

const int maxn = 1000 + 10;
const int inf = (-1u) >> 1;
const double eps = 1e-8;

int sgn(double x) {
    return (x > eps) - (x < -eps);
}

int main() {
    int t;
    double a, b;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%lf%lf", &a, &b);
        if (sgn(a) == 0) {
            double ans = 0;
            if (sgn(b) == 0) {
                ans = 1.0;
            } else {
                ans = 0.5;
            }
            printf ("%.8lf\n", ans);
            continue;
        }
        double all = 2 * a * b, ch;
        if (sgn(a - 4*b) <= 0) {
            ch = (b + b + a/4) * a/2;
        } else {
            ch = a*b;
            ch += (a + a - b*4) * b/2;
        }
        printf ("%.8lf\n", ch / all);
    }
    return 0;
}


/*
 * Author: iSea
 * Created Time:  2011/4/19 23:34:46
 * File Name: D.cpp
 */
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>

using namespace std;

#define PB push_back
#define PII pair<int, int>
#define SZ(x) ((int)((x).size()))
#define OUT(x) printf(#x" %d\n", x)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

const int maxn = 1000 + 10;
const int inf = (-1u) >> 1;
const double eps = 1e-8;

int sgn(double x) {
    return (x > eps) - (x < -eps);
}

int main() {
    int t;
    double a, b;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%lf%lf", &a, &b);
        if (sgn(a) == 0) {
            double ans = 0;
            if (sgn(b) == 0) {
                ans = 1.0;
            } else {
                ans = 0.5;
            }
            printf ("%.8lf\n", ans);
            continue;
        }
        double all = 2 * a * b, ch;
        if (sgn(b) == 0) {
            printf ("%.8lf\n", 1.0);
            continue;
        }
        if (sgn(a - 4*b) <= 0) {
            ch = (b + b + a/4) * a/2;
        } else {
            ch = a*b;
            ch += (a + a - b*4) * b/2;
        }
        printf ("%.8lf\n", ch / all);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long Int;

int main()
{
    Int t,a,b;
    Int i;
    double probability;
    double area,totalarea;

    scanf("%I64d",&t);

    for (i=1;i<=t;i++)
    {
        scanf("%I64d %I64d",&a,&b);

        probability=0.5;

        area=( (double)a/4.0 )*(double)a;
        area=area/2.0;
        totalarea=(double)a*(double)b;

        probability=probability+(area/totalarea)*0.5;

        printf("%4.9f\n",probability);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long Int;

int main()
{
    Int t,a,b;
    Int i;
    double probability;
    double area,totalarea;

    scanf("%I64d",&t);

    for (i=1;i<=t;i++)
    {
        scanf("%I64d %I64d",&a,&b);

        if (b==0)
        {
            printf("1.000000000\n");
            continue;
        }
        if (a==0)
        {
            printf("0.500000000\n");
            continue;
        }

        probability=0.5;

        if ( (double)a/4.0<=(double)b )
        area=( (double)a/4.0 )*(double)a;
        else
        area=( (double)a+( (double)a-4.0*(double)b ) )*(double)b;
        area=area/2.0;

        totalarea=(double)a*(double)b;

        probability=probability+(area/totalarea)*0.5;

        printf("%4.9f\n",probability);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long Int;

int main()
{
    Int t,a,b;
    Int i;
    double probability;
    double area,totalarea;

    scanf("%I64d",&t);

    for (i=1;i<=t;i++)
    {
        scanf("%I64d %I64d",&a,&b);

        if (b==0)
        {
            printf("1.000000000\n");
            continue;
        }
        if (a==0)
        {
            printf("0.500000000\n");
            continue;
        }

        probability=0.5 + (double)a/( (double)b*16.0 );

        printf("%4.9f\n",probability);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long Int;

int main()
{
    Int t,a,b;
    Int i;
    double probability;
    double area,totalarea;

    scanf("%I64d",&t);

    for (i=1;i<=t;i++)
    {
        scanf("%I64d %I64d",&a,&b);

        if (b==0)
        {
            printf("1.000000000\n");
            continue;
        }

        probability=0.5;

        area=( (double)a/4.0 )*(double)a;
        area=area/2.0;
        totalarea=(double)a*(double)b;

        probability=probability+(area/totalarea)*0.5;

        printf("%4.9f\n",probability);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long Int;

int main()
{
    Int t,a,b;
    Int i;
    double probability;
    double area,totalarea;

    scanf("%I64d",&t);

    for (i=1;i<=t;i++)
    {
        scanf("%I64d %I64d",&a,&b);

        if (b==0)
        {
            printf("1.000000000\n");
            continue;
        }
        if (a==0)
        {
            printf("0.500000000\n");
            continue;
        }

        probability=0.5;

        area=( (double)a/4.0 )*(double)a;
        area=area/2.0;
        totalarea=(double)a*(double)b;

        probability=probability+(area/totalarea)*0.5;

        printf("%4.9f\n",probability);
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;

int t;
double a, b;

int main()
{
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        cin >> a >> b;
        if (a / 4.0 <= b)
        {
            double s = 0.5 * a * a / 4.0;
            printf("%.7f\n", (s + a * b) / (2 * a * b));
        }
        else {
            double _a = 4 * b;
            double s = 0.5 * _a * b;
            printf("%.7f\n", ((a - _a) * b + a * b + s) / (2 * a * b));
        }
    }
}


#include <iostream>
#include <stdio.h>
using namespace std;

int t;
double a, b;

int main()
{
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        cin >> a >> b;
        if (b == 0) cout << 1;
            else if (a == 0) cout << 0.5;
             else
        if (a / 4.0 <= b)
        {
            double s = 0.5 * a * a / 4.0;
            printf("%.7f\n", (s + a * b) / (2 * a * b));
        }
        else {
            double _a = 4 * b;
            double s = 0.5 * _a * b;
            printf("%.7f\n", ((a - _a) * b + a * b + s) / (2 * a * b));
        }
    }
}


#include <iostream>
#include <stdio.h>
using namespace std;

int t;
double a, b;

int main()
{
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        cin >> a >> b;
        if (b == 0) cout << 1 << endl;
            else if (a == 0) cout << 0.5 << endl;
             else
        if (a / 4.0 <= b)
        {
            double s = 0.5 * a * a / 4.0;
            printf("%.7f\n", (s + a * b) / (2 * a * b));
        }
        else {
            double _a = 4 * b;
            double s = 0.5 * _a * b;
            printf("%.7f\n", ((a - _a) * b + a * b + s) / (2 * a * b));
        }
    }
}


#include <iostream>
using namespace std;

int t;
double a, b;

int main()
{
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        cin >> a >> b;
        if (a / 4.0 <= b)
        {
            double s = 0.5 * a * a / 4.0;
            printf("%.7f\n", (s + a * b) / (2 * a * b));
        }
        else {
            double _a = 4 * b;
            double s = 0.5 * _a * b;
            printf("%.7f\n", ((a - _a) * b + a * b + s) / (2 * a * b));
        }
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
double eps = 1e-7;
int main()
{
        int t;
        double a,b,ans;
        scanf("%d",&t);
        while(t--)
        {
                scanf("%lf%lf",&a,&b);
                ans = 0.50000000;
                if(b < eps)
                {
                        printf("%lf\n",1.0);
                        continue;
                }
                if(a < eps)
                {
                    printf("%lf\n",0.5);
                    continue;
                }
                if(a/b-4.0 < -eps)
                {
                        ans += 0.5*(a*a/8.0)/(a*b);
                }
                else
                {
                        ans += 0.5*(a*b-b*2.0*b)/(a*b);
                }
                printf("%lf\n",ans);
        }
        return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	double a,b,ans;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf%lf",&a,&b);
		ans = 0.50000000;
		if(a/b >= 4.0)
		{
			ans += 0.5*(a*b-b*2.0*b)/(a*b);
		}
		else
		{
			ans += 0.5*(a*a/8.0)/(a*b);
		}
		printf("%lf\n",ans);
	}
	return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
double eps = 1e-7;
int main()
{
	int t;
	double a,b,ans;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf%lf",&a,&b);
		ans = 0.50000000;
		if(b < eps)
		{
			printf("%lf\n",1.0);
			continue;
		}
		if(a/b-4.0 < -eps)
		{
			ans += 0.5*(a*a/8.0)/(a*b);
		}
		else
		{
			ans += 0.5*(a*b-b*2.0*b)/(a*b);
		}
		printf("%lf\n",ans);
	}
	return 0;
}


#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <numeric>
#include <limits.h>
#include <iterator>
#include <iomanip>
#define REP(x,n)  for(x=0;x<(int)(n);x++)

using namespace std;

#define REP(x,n)  for(x=0;x<(int)(n);x++)
#define EACH(itr, cont) for(typeof((cont).begin()) itr = (cont).begin(); itr != (cont).end(); ++itr)
#define ALL(X)    (X).begin(),(X).end()

typedef long long LL;

int main(int argc, char** argv) {
    cin.sync_with_stdio(false);
    cout << setprecision(16);
    int t, i;
    double a, b;
    cin >> t;

    REP(i, t) {
        // p->[0,a] , q->[-b,b]
        cin >> a >> b;
        double result = 0;
        if (b == 0) {
            result = 1;
        } else if (a == 0) {
            result = 0.5;
        } else {
            double c = 4 * b, total = 2.0 * c*a;
            result = 0.5;
            result += min(a, c) * min(a, c) / 2 / total;
            if (a > c) {
                result += (a - c) * c / total;
            }
        }

        cout << result << endl;
    }
}


#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <numeric>
#include <limits.h>
#include <iterator>
#include <iomanip>
#define REP(x,n)  for(x=0;x<(int)(n);x++)

using namespace std;

#define REP(x,n)  for(x=0;x<(int)(n);x++)
#define EACH(itr, cont) for(typeof((cont).begin()) itr = (cont).begin(); itr != (cont).end(); ++itr)
#define ALL(X)    (X).begin(),(X).end()

typedef long long LL;

int main(int argc, char** argv) {
    cin.sync_with_stdio(false);
    cout << setprecision(16);
    int t, i;
    double p, q;

    REP(i, t) {
        double maxQ = p / 4;
        maxQ = min(q, maxQ);
        cout << maxQ / (2 * q);
    }
}