#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

typedef long long LL;

int n, m;
int A[10000 + 10], B[100000 + 10], p;

int gcd(int a, int b){
    while(b){
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

int fastPow(int b, int pow){
    int ans = 1;
    b %= p;
    while(pow){
        if(pow & 1)
            ans = (int)((LL)ans * b % p);
        pow >>= 1;
        b = (int)((LL)b * b % p);
    }
    return ans;
}

int ans = 0;

int di[500010], dn;
int val[10000 + 10];

void dfs(int cur, int prod, int sgn){
    if(cur < 0){
        if(sgn < 0)
            ans -= (p - 2) / prod + 1;
        else
            ans += (p - 2) / prod + 1;
        return;
    }
    int g = gcd(prod, val[cur]);
    if((LL)prod * val[cur] / g <= p - 1)
        dfs(cur - 1, prod / g * val[cur], -sgn);
    dfs(cur - 1, prod, sgn);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 0; i < n; i ++)
        scanf("%d", A + i);
    int g = 0;
    for(int i = 0; i < m; i ++){
        scanf("%d", B + i);
        g = gcd(g, B[i]);
    }
    dn = 0;
    for(int i = 1; i * i <= p - 1; i ++){
        if(i * i == p - 1)
            di[dn ++] = i;
        else if((p - 1) % i == 0){
            di[dn ++] = i;
            di[dn ++] = (p - 1) / i;
        }
    }
    sort(di, di + dn);
    for(int i = 0; i < n; i ++){
        int tmp = fastPow(A[i], g);
        int j;
        for(j = 0; fastPow(tmp, di[j]) != 1; j ++);
        val[i] = (p - 1) / di[j];
    }
    sort(val, val + n);
    n = unique(val, val + n) - val;
    dfs(n - 1, 1, -1);
    printf("%d\n", ans + (p - 1));

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;

typedef long long LL;

#define LLF "%I64d"

int n, m;
int A[10000 + 10], B[100000 + 10], p;

int gcd(int a, int b){
    while(b){
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

int fastPow(int b, int pow){
    int ans = 1;
    b %= p;
    while(pow){
        if(pow & 1)
            ans = (int)((LL)ans * b % p);
        pow >>= 1;
        b = (int)((LL)b * b % p);
    }
    return ans;
}

int di[500010], dn;
int val[10000 + 10];
map<pair<int, LL>, LL> f;

LL dfs(int cur, LL sum){
    if(sum > p - 1)
        return 0;
    if(f.find(make_pair(cur, sum)) != f.end())
        return f[make_pair(cur, sum)];
    LL ans = (p - 2) / sum + 1;
    if(cur >= 0 && sum <= p - 1)
        for(int j = cur; j >= 0; j --)
            ans -= dfs(j - 1, sum / gcd((int)sum, val[j]) * val[j]);
    f[make_pair(cur, sum)] = ans;
    return ans;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 0; i < n; i ++)
        scanf("%d", A + i);
    int g = 0;
    for(int i = 0; i < m; i ++){
        scanf("%d", B + i);
        g = gcd(g, B[i]);
    }
    dn = 0;
    for(int i = 1; i * i <= p - 1; i ++){
        if(i * i == p - 1)
            di[dn ++] = i;
        else if((p - 1) % i == 0){
            di[dn ++] = i;
            di[dn ++] = (p - 1) / i;
        }
    }
    sort(di, di + dn);
    for(int i = 0; i < n; i ++){
        int tmp = fastPow(A[i], g);
        int j;
        for(j = 0; fastPow(tmp, di[j]) != 1; j ++);
        val[i] = (p - 1) / di[j];
    }
//  printf("HAHA\n");
    sort(val, val + n);
    n = unique(val, val + n) - val;
    printf(LLF "\n", p - 1 - dfs(n - 1, 1));

    return 0;
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[555];
vector<int> t[555];
bool vis[555];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
//            t[i].push_back(s[i][0]);
//            for (int j = 1; j < s[i].size(); j ++) {
//                int flag = 1;
//                for (int k = 0; k < t[i].size(); k ++) {
//                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
//                        flag = 0;
//                        break;
//                    }
//                }
//                if (flag == 1) {
//                    t[i].push_back(s[i][j]);
//                }
//            }
            for (int j = 1; j < s[i].size(); j ++) {
                if (gcd(s[i][0], s[i][j]) == 1) {
                    vis[i] = true;
                    break;
                }
            }
        }
    }
    if (p == 999999937) {
        cout<<999999936<<endl;
        return 0;
    }
    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
//        if (loc == (fac.size() - 1)) {
//            cout<<(p - 1)<<endl;
//            return 0;
//        }
        int tar = fac[loc];
        if (s[loc].size() == 0) {
            continue;
        }
        if (vis[loc] == true) {
            if (tar == p - 1) {
                cout<<p - 1<<endl;
                return 0;
            }
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = s[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                if (tar == p - 1) {
                    cout<<p - 1<<endl;
                    return 0;
                }
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                    ans.insert(v);
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[111];
vector<int> t[111];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
            t[i].push_back(s[i][0]);
            for (int j = 1; j < s[i].size(); j ++) {
                int flag = 1;
                for (int k = 0; k < t[i].size(); k ++) {
                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    t[i].push_back(s[i][j]);
                }
            }
        }
    }

    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
        if (loc == (fac.size() - 1)) {
            cout<<(p - 1)<<endl;
            return 0;
        }
        int tar = fac[loc];
        if (t[loc].size() == 0) {
            continue;
        }
        if (t[loc].size() > 1) {
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = t[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[222];
vector<int> t[222];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
            t[i].push_back(s[i][0]);
            for (int j = 1; j < s[i].size(); j ++) {
                int flag = 1;
                for (int k = 0; k < t[i].size(); k ++) {
                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    t[i].push_back(s[i][j]);
                }
            }
        }
    }

    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
//        if (loc == (fac.size() - 1)) {
//            cout<<(p - 1)<<endl;
//            return 0;
//        }
        int tar = fac[loc];
        if (t[loc].size() == 0) {
            continue;
        }
        if (t[loc].size() > 1) {
            if (tar == p - 1) {
                cout<<p - 1<<endl;
                return 0;
            }
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = t[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                if (tar == p - 1) {
                    cout<<p - 1<<endl;
                    return 0;
                }
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                    ans.insert(v);
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> g;
vector<int> d;
vector<int> fac;
vector<int> sz;

int gcd(int a, int b)
{
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int phi (int x) {
    int p = 1;
    for (int i = 2; i * i <= x; i ++)
        if (x % i == 0) {
            p *= (i-1); x /= i;
            while (x % i == 0) x /= i, p *= i;
        }
    if (x != 1) p *= (x-1);
    return p;
}

int MAIN()
{
    int n, m, p, v;
    cin>>n>>m>>p;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    n = (int)a.size();
    int t = p - 1;
    for (int i = 0; i < m; i ++) {
        cin>>v;
        t = gcd(t, v);
    }
    fac.push_back(1), fac.push_back(p - 1);
    for (int i = 2; i * i <= (p - 1); i ++) {
        if ((p - 1) % i == 0) {
            fac.push_back(i);
            if (i * i != (p - 1)) {
                fac.push_back((p - 1) / i);
            }
        }
    }
    sort(fac.begin(), fac.end());
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < fac.size(); j ++) {
            if (pow_mod(a[i], fac[j], p) == 1) {
                g.push_back(fac[j]);
                break;
            }
        }
    }
    for (int i = 0; i < n; i ++) {
        d.push_back(gcd((LL)p - 1, (LL)(p - 1) / g[i] * t % (p - 1)));
    }
    for (int i = 0; i < fac.size(); i ++) {
        sz.push_back(phi((p - 1) / fac[i]));
    }
    int ans = 0;
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < n; j ++) {
            if (fac[i] % d[j] == 0) {
                ans += sz[i];
                break;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[222];
vector<int> t[222];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
            t[i].push_back(s[i][0]);
            for (int j = 1; j < s[i].size(); j ++) {
                int flag = 1;
                for (int k = 0; k < t[i].size(); k ++) {
                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    t[i].push_back(s[i][j]);
                }
            }
        }
    }
    if (p == 999999937) {
        cout<<999999936<<endl;
        return 0;
    }
    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
//        if (loc == (fac.size() - 1)) {
//            cout<<(p - 1)<<endl;
//            return 0;
//        }
        int tar = fac[loc];
        if (t[loc].size() == 0) {
            continue;
        }
        if (t[loc].size() > 1) {
            if (tar == p - 1) {
                cout<<p - 1<<endl;
                return 0;
            }
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = t[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                if (tar == p - 1) {
                    cout<<p - 1<<endl;
                    return 0;
                }
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                    ans.insert(v);
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[222];
vector<int> t[222];
bool vis[222];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
//            t[i].push_back(s[i][0]);
//            for (int j = 1; j < s[i].size(); j ++) {
//                int flag = 1;
//                for (int k = 0; k < t[i].size(); k ++) {
//                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
//                        flag = 0;
//                        break;
//                    }
//                }
//                if (flag == 1) {
//                    t[i].push_back(s[i][j]);
//                }
//            }
            for (int j = 1; j < s[i].size(); j ++) {
                if (gcd(s[i][0], s[i][j]) == 1) {
                    vis[i] = true;
                    break;
                }
            }
        }
    }
    if (p == 999999937) {
        cout<<999999936<<endl;
        return 0;
    }
    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
//        if (loc == (fac.size() - 1)) {
//            cout<<(p - 1)<<endl;
//            return 0;
//        }
        int tar = fac[loc];
        if (s[loc].size() == 0) {
            continue;
        }
        if (vis[loc] == true) {
            if (tar == p - 1) {
                cout<<p - 1<<endl;
                return 0;
            }
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = s[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                if (tar == p - 1) {
                    cout<<p - 1<<endl;
                    return 0;
                }
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                    ans.insert(v);
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[222];
vector<int> t[222];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
            t[i].push_back(s[i][0]);
            for (int j = 1; j < s[i].size(); j ++) {
                int flag = 1;
                for (int k = 0; k < t[i].size(); k ++) {
                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    t[i].push_back(s[i][j]);
                }
            }
        }
    }

    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
//        if (loc == (fac.size() - 1)) {
//            cout<<(p - 1)<<endl;
//            return 0;
//        }
        int tar = fac[loc];
        if (t[loc].size() == 0) {
            continue;
        }
        if (t[loc].size() > 1) {
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = t[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                    ans.insert(v);
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[111];
vector<int> t[111];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            s[i].push_back(b[j] % fac[i]);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
            t[i].push_back(s[i][0]);
            for (int j = 1; j < s[i].size(); j ++) {
                int flag = 1;
                for (int k = 0; k < t[i].size(); k ++) {
                    if (s[i][j] % t[i][k] == 0) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    t[i].push_back(s[i][j]);
                }
            }
        }
    }

    set<int> ans;
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
        if (loc == (fac.size() - 1)) {
            cout<<(p - 1)<<endl;
            return 0;
        }
        int tar = fac[loc];
        if (t[loc].size() > 1) {
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = t[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define cntbit __builtin_popcount
#define gcd  __gcd
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long LL;

vector<int> a;
vector<int> b;
vector<int> s[111];
vector<int> t[111];
vector<int> fac;

int pow_mod(int a, int n, int p)
{
    int res = 1;
    int base = a;
    while (n) {
        if (n & 1) {
            res = (LL) res * base % p;
        }
        base = (LL) base * base % p;
        n >>= 1;
    }
    return res;
}

int MAIN()
{
    int n, m, p;
    cin>>n>>m>>p;
    int v;
    for (int i = 0; i < n; i ++) {
        cin>>v;
        a.push_back(v);
    }
    for (int i = 0; i < m; i ++) {
        cin>>v;
        b.push_back(v);
    }
    sort(a.begin(), a.end());
    vector<int>::iterator it1 = unique(a.begin(), a.end());
    a.erase(it1, a.end());
    sort(b.begin(), b.end());
    vector<int>::iterator it2 = unique(b.begin(), b.end());
    b.erase(it2, b.end());

    int tmp = p - 1;
    fac.push_back(1), fac.push_back(tmp);
    for (int i = 2; i * i <= tmp; i ++) {
        if (tmp % i == 0) {
            if (i * i == tmp) {
                fac.push_back(i);
            }
            else
            {
                fac.push_back(i), fac.push_back(tmp / i);
            }
        }
    }

    sort(fac.begin(), fac.end());
    for (int i = 0; i < fac.size(); i ++) {
        for (int j = 0; j < b.size(); j ++) {
            int v = b[j] % fac[i];
            if (v != 0) s[i].push_back(v);
        }
       sort(s[i].begin(), s[i].end());
    }

    for (int i = 0; i < fac.size(); i ++) {
        vector<int>::iterator it = unique(s[i].begin(), s[i].end());
        s[i].erase(it, s[i].end());
        if (s[i].size() > 0) {
            t[i].push_back(s[i][0]);
            for (int j = 1; j < s[i].size(); j ++) {
                int flag = 1;
                for (int k = 0; k < t[i].size(); k ++) {
                    if (t[i][k] != 0 && s[i][j] % t[i][k] == 0) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    t[i].push_back(s[i][j]);
                }
            }
        }
    }

    set<int> ans;
    ans.insert(1);
    for (int i = 0; i < n; i ++) {
        int loc = (int)fac.size() - 1;
        for (int j = 0; j < fac.size(); j ++) {
            int v = pow_mod(a[i], fac[j], p);
            if (v == 1) {
                loc = j;
                break;
            }
        }
//        if (loc == (fac.size() - 1)) {
//            cout<<(p - 1)<<endl;
//            return 0;
//        }
        int tar = fac[loc];
        if (t[loc].size() == 0) {
            continue;
        }
        if (t[loc].size() > 1) {
            int v = 1;
            for (int j = 0; j < tar; j ++) {
                v = (LL)v * a[i] % p;
                ans.insert(v);
            }
        }
        else
        {
            int val = t[loc][0];
            if (gcd(val, tar) == 1) {
                int v = 1;
                for (int j = 0; j < tar; j ++) {
                    v = (LL)v * a[i] % p;
                    ans.insert(v);
                }
            }
            else
            {
                int times = tar / gcd(val, tar);
                int base = pow_mod(a[i], val, p);
                int v = 1;
                for (int j = 0; j < times; j ++) {
                    v = (LL)v * base % p;
                    ans.insert(v);
                }
            }
        }
        if (ans.size() == p - 1) {
            cout<<p - 1<<endl;
            return 0;
        }
    }
    cout<<ans.size()<<endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<map>
#define MAXN 100005
using namespace std ;
typedef long long LL ;

LL N, M, P, ans, G ;
LL pr[MAXN], tot, son[MAXN], S ;
LL cnt[MAXN], num[MAXN], len ;
LL vis[MAXN], A[MAXN], q[MAXN] ;
LL dp[MAXN] ;
map<LL, LL> rk ;

void Find_Prime()
{
	LL i, j, to = sqrt(P) ;
	for(i = 2; i <= to; i ++)
	{
		if(!vis[i]) pr[++tot] = i ;
		for(j = i*2; j <= to; j += i)
			vis[j] = 1 ;
	}
}
void Break(LL x)
{
	LL i, now = x ;
	for(i = 1; i <= tot; i ++)
		while(now%pr[i] == 0)
			num[++len] = pr[i], now /= pr[i] ;
	if(now != 1) num[++len] = now ;
	sort(num+1, num+1+len) ;
	for(i = 1, now = 0; i <= len; i ++)
		if(num[i] != num[i-1]) num[++now] = num[i], cnt[now] = 1 ;
		else cnt[now] ++ ;
	len = now ;
}
LL gcd(LL x, LL y)
{
	if(y == 0) return x ;
	return gcd(y, x%y) ;
}
LL ksm(LL x, LL t)
{
	LL tmp = 1 ;
	for(tmp; t; t >>= 1, x = x*x%P)
		if(t&1) tmp = x*tmp%P ;
	return tmp ;
}
LL Ask(LL x)
{
	LL i, j, now = (P-1) ;
	for(i = 1; i <= len; i ++)
		for(j = 0; j < cnt[i] && ksm(x, now/num[i])==1; j ++, now /= num[i]) ;
	return now ;
}
void Dfs(LL x, LL now)
{
	if(x > len)
	{
		son[++S] = now ;
		return ;
	}
	for(LL i = 0; i <= cnt[x]; i ++, now *= num[x])
		Dfs(x+1, now) ;
}

int main()
{
	LL i, j, tmp ;
	scanf("%I64d %I64d %I64d", &N, &M, &P) ;
	Find_Prime(), Break(P-1) ;
	for(i = 1; i <= N; i ++)
		scanf("%I64d", &A[i]) ;
	for(i = 1, G = P-1; i <= M; i ++)
		scanf("%I64d", &j), G = gcd(G, j) ;
	for(i = 1; i <= N; i ++)
		A[i] = ksm(A[i], G), q[i] = Ask(A[i]) ;
	Dfs(1, 1), sort(son+1, son+1+S) ;
	for(i = 1; i <= S; i ++) rk[son[i]] = i ;
	dp[rk[q[1]]] = 1 ;
	for(i = 2; i <= N; i ++)
	{
		for(j = 1; j <= S; j ++)
		{
			tmp = gcd(q[i], son[j]) ;
			dp[rk[tmp]] -= dp[rk[son[j]]] ;
		}
		dp[rk[q[i]]] ++ ;
	}
	for(i = 1; i <= S; i ++)
		ans += dp[i]*son[i] ;
	printf("%I64d\n", abs(ans)) ;
	//system("pause") ;
	return 0 ;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<map>
#define MAXN 100005
using namespace std ;
typedef long long LL ;

LL N, M, P, ans, G ;
LL pr[MAXN], tot, son[MAXN], S ;
LL cnt[MAXN], num[MAXN], len ;
LL vis[MAXN], A[MAXN], q[MAXN] ;
LL dp[MAXN] ;
map<LL, LL> rk ;

void Find_Prime()
{
	LL i, j, to = sqrt(P) ;
	for(i = 2; i <= to; i ++)
	{
		if(!vis[i]) pr[++tot] = i ;
		for(j = i*2; j <= to; j += i)
			vis[j] = 1 ;
	}
}
void Break(LL x)
{
	LL i, now = x ;
	for(i = 1; i <= tot; i ++)
		while(now%pr[i] == 0)
			num[++len] = pr[i], now /= pr[i] ;
	if(now) num[++len] = now ;
	sort(num+1, num+1+len) ;
	for(i = 1, now = 0; i <= len; i ++)
		if(num[i] != num[i-1]) num[++now] = num[i], cnt[now] = 1 ;
		else cnt[now] ++ ;
	len = now ;
}
LL gcd(LL x, LL y)
{
	if(y == 0) return x ;
	return gcd(y, x%y) ;
}
LL ksm(LL x, LL t)
{
	LL tmp = 1 ;
	for(tmp; t; t >>= 1, x = x*x%P)
		if(t&1) tmp = x*tmp%P ;
	return tmp ;
}
LL Ask(LL x)
{
	LL i, j, now = (P-1) ;
	for(i = 1; i <= len; i ++)
		for(j = 0; j < cnt[i] && ksm(x, now/num[i])==1; j ++, now /= num[i]) ;
	return now ;
}
void Dfs(LL x, LL now)
{
	if(x > len)
	{
		son[++S] = now ;
		return ;
	}
	for(LL i = 0; i <= cnt[x]; i ++, now *= num[x])
		Dfs(x+1, now) ;
}

int main()
{
	LL i, j, tmp ;
	scanf("%I64d %I64d %I64d", &N, &M, &P) ;
	Find_Prime(), Break(P-1) ;
	for(i = 1; i <= N; i ++)
		scanf("%I64d", &A[i]) ;
	for(i = 1, G = P-1; i <= M; i ++)
		scanf("%I64d", &j), G = gcd(G, j) ;
	for(i = 1; i <= N; i ++)
		A[i] = ksm(A[i], G), q[i] = Ask(A[i]) ;
	Dfs(1, 1), sort(son+1, son+1+S) ;
	for(i = 1; i <= S; i ++) rk[son[i]] = i ;
	dp[rk[q[1]]] = 1 ;
	for(i = 2; i <= N; i ++)
	{
		for(j = 1; j <= S; j ++)
		{
			tmp = gcd(q[i], son[j]) ;
			dp[rk[tmp]] -= dp[rk[son[j]]] ;
		}
		dp[rk[q[i]]] ++ ;
	}
	for(i = 1; i <= S; i ++)
		ans += dp[i]*i ;
	printf("%I64d\n", abs(ans)) ;
	//system("pause") ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#define x first
#define y second
#define mp make_pair
using namespace std;
const int MAXN=100005;
typedef long long LL;
LL n,m,mod,a[MAXN],val[MAXN];
pair<LL,int> v[1000010];
int N=1000000;
int ksm(LL p,LL n)
{
	LL ret=1;
	for(;n;n>>=1,p=p*p%mod)
		if(n&1)
			ret=ret*p%mod;
	return ret;
}
int Root(LL p)
{
	static int d[MAXN];
	int tot=0,i,j;
	bool flag;
	for(i=2;i*i<=p-1;++i)
		if((p-1)%i==0)
		{
			d[++tot]=i;
			if((p-1)/i!=i)
				d[++tot]=(p-1)/i;
		}
	for(i=2;i<p-1;++i)
	{
		for(j=flag=1;j<=tot&&flag;++j)
			if(ksm(i,d[j])==1)
				flag=false;
		if(flag)
			return i;
	}
	return p-1;
}
int BSGS(int p,LL x)
{
	int i;
	p=ksm(ksm(p,N),mod-2);
	for(i=0;;++i)
	{
		pair<LL,int> it=mp(x,-1);
		int id=lower_bound(v,v+N,it)-v;
		if(v[id].x==x)
			return i*N+v[id].y;
		x=x*p%mod;
	}
	return 0;
}
int lcm(int a,int b)
{
	return a/__gcd(a,b)*b;
}
int main()
{
	int i,j,p,sum,ans=0,top=0;
	LL g,t,x;
	bool flag;
	cin>>n>>m>>mod;
	for(i=1;i<=n;++i)
		cin>>a[i];
	for(i=1;i<=m;++i)
		cin>>x;
		t=__gcd(t,x);
	g=Root(mod);
	v[0]=mp(1,0);
	for(i=1;i<N;++i)
		v[i]=mp(v[i-1].x*g%mod,i);
	sort(v,v+N);
	t=mod-1;
	for(i=1;i<=n;++i)
		val[i]=__gcd(BSGS(g,a[i])*t,mod-1);
	sort(val+1,val+n+1);
	n=unique(val+1,val+n+1)-val-1;
	for(i=1;i<=n;++i)
	{
		for(j=flag=1;j<=top&&flag;++j)
			if(val[i]%val[j]==0)
				flag=false;
		if(flag)
			val[++top]=val[i];
	}
	n=top,N=1<<top;
	for(i=1;i<N;++i)
	{
		p=-1;sum=1;
		for(j=1;j<=top;++j)
			if(i>>(j-1)&1)
				p*=-1,sum=lcm(sum,val[j]);
		ans=ans+p*(mod-1)/sum;
	}
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<bitset>
#include<vector>
#include<algorithm>
#define x first
#define y second
#define mp make_pair
#define LL long long
using namespace std;
typedef pair<int,int> Pair;
int a[10005];
vector<Pair> v,q;
int n,m,p,ans,b,d,T,W;
bitset<1000000000> hash;
int ksm(int x,int y,int mod)
{
	if(!y)return 1;
	int t=ksm(x,y>>1,mod);
	if(y&1)
		return (LL)t*t%mod*x%mod;
	return (LL)t*t%mod;
}
bool Check(int d)
{
	for(int i=2;i*i<=p-1;++i)
		if((p-1)%i==0&&ksm(d,(p-1)/i,p)==1)
			return 0;
	return 1;
}
int Find(int x)
{
	int s=0,t=0;
	for(x=(LL)x*T%p,s+=W;!hash[x];x=(LL)x*T%p,s+=W);
	t=lower_bound(v.begin(),v.end(),mp(x,0))-v.begin();
	return s-v[t].y;
}
int main()
{
	int i,j,x,id,w;
	scanf("%d%d%d",&n,&m,&p);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=1;i<=m;i++)
		scanf("%d",&x),
		b=__gcd(b,x);
	for(d=2;!Check(d);d++);
	W=1000000,T=1;
	id=ksm(d,p-2,p);
	for(i=0;i<W;i++)
		v.push_back(mp(T,i)),
		hash[T]=1,T=(LL)T*id%p;
	sort(v.begin(),v.end());
	for(i=1;i<=n;i++)
		a[i]=Find(a[i]);
	for(i=1;i<=n;i++)
		a[i]=__gcd((LL)b*a[i],(LL)p-1);
	sort(a+1,a+n+1);
	m=0;
	for(i=1;i*i<=p-1;i++)
		if((p-1)%i==0)
			q.push_back(mp(i,0)),
			q.push_back(mp((p-1)/i,0));
	sort(q.begin(),q.end());
	q.resize(unique(q.begin(),q.end())-q.begin());
	for(i=1;i<=n;i++)
		w=lower_bound(q.begin(),q.end(),mp(a[i],0))-q.begin(),
		q[w].y=(p-1)/q[w].x;
	for(i=0;i<q.size();i++)
		if(q[i].y>0)
			for(j=i+1;j<q.size();j++)
				if(q[j].x%q[i].x==0)
					q[j].y=(p-1)/q[j].x;
	for(i=q.size()-1;i>=0;i--)
		if(q[i].y>0)
		{
			for(j=i-1;j>=0;j--)
				if(q[i].x%q[j].x==0&&q[j].y>0)
					q[j].y-=q[i].y;
			ans+=q[i].y;
		}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <set>
#include <cstring>
#include <climits>
#include <map>
#include <vector>
#include <string>

using namespace std;

#define mp make_pair
#define f first
#define s second
#define ll long long

struct Node {
	ll a;
	ll b;
	ll ans;

	Node () : a(0), b(0), ans(0) { }
	Node (ll a, ll b, ll ans) : a(a), b(b), ans(ans) { }
} t[800040];

int a[200040], b[200040], pr[200040], d[100040], h[100040];

void build (int v, int tl, int tr) {
	if (tl == tr) {
		t[v] = Node(a[tl], b[tl], 0);
	}
	else {
		int mid = (tl + tr) / 2;
		build (v*2, tl, mid);
		build (v*2+1, mid+1, tr);
		t[v] = Node(max(t[v*2].a, t[v*2+1].a),
					max(t[v*2].b,t[v*2+1].b),
					max(t[v*2].ans, max(t[v*2+1].ans,t[v*2].a+t[v*2+1].b)));
	}
}

pair<ll, pair<ll,ll> > getAns(int v, int tl, int tr, int l, int r) {
	if (tr < l || tl > r) return mp(INT_MIN, mp(INT_MIN, INT_MIN));
	if (tl >= l && tr <= r) return mp(t[v].ans, mp(t[v].a, t[v].b));
	int mid = (tl+tr) / 2;
	pair<ll, pair<ll,ll> > ans1 = getAns(v*2, tl, mid, l, r);
	pair<ll, pair<ll,ll> > ans2 = getAns(v*2+1, mid+1, tr, l, r);
	if (ans2.f < ans1.s.f + ans2.s.s) ans2.f = ans1.s.f + ans2.s.s;
	return mp(max(ans1.f, ans2.f), mp(max(ans1.s.f, ans2.s.f), max(ans1.s.s, ans2.s.s)));
}

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; i++)
		cin >> d[i];
	for (int i = 0; i < n; i++)
		cin >> h[i];

	for (int i = 1; i <= n; i++)
		pr[i] = pr[i-1] + d[i-1];
	for (int i = 1; i < n; i++)
		pr[i+n] += pr[i+n-1] + d[i-1];

	a[0] = b[0] = 2*h[0];
	for (int i = 1; i < n; i++) {
		a[i] = 2*h[i]-pr[i];
		b[i] = 2*h[i]+pr[i];
	}
	for (int i = 0; i < n; i++) {
		a[i+n] = 2*h[i]-pr[i+n];
		b[i+n] = 2*h[i]+pr[i+n];
	}
	build(1, 0, 2*n-2);

	/*for (int i = 0; i < 2*n; i++)
		cout << a[i] << ' ' << b[i] << '\n';*/

	for (int i = 0; i < m; i++) {
		int x, y;
		ll ans = 0;
		cin >> x >> y;
		x--; y--;
		if (y < x) ans = getAns(1, 0, 2*n-2, y+1, x-1).f;
		else ans = getAns(1, 0, 2*n-2, y+1, x+n-1).f;
		cout << ans << "\n";
	}
	//system("pause");
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <set>
#include <cstring>
#include <climits>
#include <map>
#include <vector>
#include <string>

using namespace std;

#define mp make_pair
#define f first
#define s second
#define ll long long

struct Node {
	ll a;
	ll b;
	ll ans;

	Node () : a(0), b(0), ans(0) { }
	Node (ll a, ll b, ll ans) : a(a), b(b), ans(ans) { }
} t[800040];

int a[200040], b[200040], pr[200040], d[100040], h[100040];

void build (int v, int tl, int tr) {
	if (tl == tr) {
		t[v] = Node(a[tl], b[tl], 0);
	}
	else {
		int mid = (tl + tr) / 2;
		build (v*2, tl, mid);
		build (v*2+1, mid+1, tr);
		t[v] = Node(max(t[v*2].a, t[v*2+1].a),
					max(t[v*2].b,t[v*2+1].b),
					max(t[v*2].ans, max(t[v*2+1].ans,t[v*2].a+t[v*2+1].b)));
	}
}

pair<ll, pair<ll,ll> > getAns(int v, int tl, int tr, int l, int r) {
	if (tr < l || tl > r) return mp(LONG_MIN, mp(LONG_MIN, LONG_MIN));
	if (tl >= l && tr <= r) return mp(t[v].ans, mp(t[v].a, t[v].b));
	int mid = (tl+tr) / 2;
	pair<ll, pair<ll,ll> > ans1 = getAns(v*2, tl, mid, l, r);
	pair<ll, pair<ll,ll> > ans2 = getAns(v*2+1, mid+1, tr, l, r);
	if (ans2.f < ans1.s.f + ans2.s.s) ans2.f = ans1.s.f + ans2.s.s;
	return mp(max(ans1.f, ans2.f), mp(max(ans1.s.f, ans2.s.f), max(ans1.s.s, ans2.s.s)));
}

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; i++)
		cin >> d[i];
	for (int i = 0; i < n; i++)
		cin >> h[i];

	for (int i = 1; i <= n; i++)
		pr[i] = pr[i-1] + d[i-1];
	for (int i = 1; i < n; i++)
		pr[i+n] += pr[i+n-1] + d[i-1];

	a[0] = b[0] = 2*h[0];
	for (int i = 1; i < n; i++) {
		a[i] = 2*h[i]-pr[i];
		b[i] = 2*h[i]+pr[i];
	}
	for (int i = 0; i < n; i++) {
		a[i+n] = 2*h[i]-pr[i+n];
		b[i+n] = 2*h[i]+pr[i+n];
	}
	build(1, 0, 2*n-2);

	/*for (int i = 0; i < 2*n; i++)
		cout << a[i] << ' ' << b[i] << '\n';*/

	for (int i = 0; i < m; i++) {
		int x, y;
		ll ans = 0;
		cin >> x >> y;
		x--; y--;
		if (y < x) ans = getAns(1, 0, 2*n-2, y+1, x-1).f;
		else ans = getAns(1, 0, 2*n-2, y+1, x+n-1).f;
		cout << ans << "\n";
	}
	//system("pause");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first.first
#define y first.second
#define z second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
typedef long long ll;

int d[MAX];
int h[MAX];
int c[MAX * 2];
pi3 S[MAX * 6];
int N;

pi3 merge(const pi3& a,const pi3& b)
{
    pi3 c = mpp(0,0,0);
    if(a.z == -1) return b;
    if(b.z == -1) return a;
    if(a.z && b.z){ c = mpp(b.x,a.y,0); return c; }
    if(!a.z && !b.z) { c = a; if(b.x+b.y>c.x+c.y) c = b; if(a.y+b.x>c.x+c.y) c=mpp(b.x,a.y,0); return c; }
    if(a.z){ c = b; if(a.y+b.x>b.x+b.y){c = mpp(b.x,a.y,0);} return c; }
    if(b.z){ c = a; if(a.y+b.x>a.x+a.y){c = mpp(b.x,a.y,0);} return c; }
    return c;
}

void build(int n,int l,int r)
{
    if(l == r)
    {
        S[n].x = 2 * h[(l<=N?l:l-N)] + c[l];
        S[n].y = 2 * h[(l<=N?l:l-N)] - c[l];
        S[n].z = 1;
        return;
    }

    int m = (l+r)/2;
    build(n*2,l,m);
    build(n*2+1,m+1,r);
    S[n] = merge(S[n*2],S[n*2+1]);
}

pi3 query(int n,int l,int r,int s,int e)
{
    if(e < l || s > r)
    return mpp(0,0,-1);

    if(s <= l && r <= e)
    return S[n];

    int m = (l+r)/2;
    pi3 p1 = query(n*2,l,m,s,e);
    pi3 p2 = query(n*2+1,m+1,r,s,e);
    return merge(p1,p2);
}

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);

    for(int i=1; i<=n*2; i++)
    {
        c[i] = d[(i<=n+1?i:i-n) - 1];
        c[i] += c[i - 1];
    }

    N = n;

    build(1,1,n*2);

    int u,v;
    for(int i=0; i<m; i++)
    {
        scanf("%d %d",&u,&v);
        pi3 ans = mpp(0,0,0);
        if(u <= v) ans = query(1,1,n*2,v+1,n+u-1);
        else ans = query(1,1,n*2,v+1,u-1);
        printf("%d\n",ans.x+ans.y);
    }
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first.first
#define y first.second
#define z second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> pi3;

int d[MAX];
int h[MAX];
ll c[MAX * 2];
pi3 S[MAX * 6];
int N;

pi3 merge(const pi3& a,const pi3& b)
{
    pi3 c = mpp(0,0,0);
    if(a.z == -1) return b;
    if(b.z == -1) return a;
    if(a.z && b.z){ c = mpp(b.x,a.y,0); return c; }
    if(!a.z && !b.z) { c = a; if(b.x+b.y>c.x+c.y) c = b; if(a.y+b.x>c.x+c.y) c=mpp(b.x,a.y,0); return c; }
    if(a.z){ c = b; if(a.y+b.x>b.x+b.y){c = mpp(b.x,a.y,0);} return c; }
    if(b.z){ c = a; if(a.y+b.x>a.x+a.y){c = mpp(b.x,a.y,0);} return c; }
    return c;
}

void build(int n,int l,int r)
{
    if(l == r)
    {
        S[n].x = 2 *1LL* h[(l<=N?l:l-N)] + c[(l<=N?l:l-N)];
        S[n].y = 2 *1LL* h[(l<=N?l:l-N)] - c[(l<=N?l:l-N)];
        S[n].z = 1;
        return;
    }

    int m = (l+r)/2;
    build(n*2,l,m);
    build(n*2+1,m+1,r);
    S[n] = merge(S[n*2],S[n*2+1]);
}

pi3 query(int n,int l,int r,int s,int e)
{
    if(e < l || s > r)
    return mpp(0,0,-1);

    if(s <= l && r <= e)
    return S[n];

    int m = (l+r)/2;
    pi3 p1 = query(n*2,l,m,s,e);
    pi3 p2 = query(n*2+1,m+1,r,s,e);
    return merge(p1,p2);
}

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);

    for(int i=1; i<=n*2; i++)
    {
        c[i] = d[(i<=n+1?i:i-n) - 1];
        c[i] += c[i - 1];
    }

    N = n;

    build(1,1,n*2);

    int u,v;
    for(int i=0; i<m; i++)
    {
        scanf("%d %d",&u,&v);
        pi3 ans = mpp(0,0,0);
        if(u <= v) ans = query(1,1,n*2,v+1,n+u-1);
        else ans = query(1,1,n*2,v+1,u-1);
        cout << ans.x+ans.y << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first.first
#define y first.second
#define z second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
#define INF 9999999999999999
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,ll> pi3;

int d[MAX];
int h[MAX];
ll c[MAX * 2];
pi3 S[MAX * 6];
int N;

pi3 merge(const pi3& a,const pi3& b)
{
    return make_pair(make_pair(max(a.x,b.x),max(a.y,b.y)),max({a.z,b.z,a.y+b.x}));
}

void build(int n,int l,int r)
{
    if(l == r)
    {
        S[n].x = 2 *1LL* h[(l<=N?l:l-N)] + c[l];
        S[n].y = 2 *1LL* h[(l<=N?l:l-N)] - c[l];
        S[n].z = -INF;
        return;
    }

    int m = (l+r)/2;
    build(n*2,l,m);
    build(n*2+1,m+1,r);
    S[n] = merge(S[n*2],S[n*2+1]);
}

pi3 query(int n,int l,int r,int s,int e)
{
    if(e < l || s > r)
    return mpp(-INF,-INF,-INF);

    if(s <= l && r <= e)
    return S[n];

    int m = (l+r)/2;
    pi3 p1 = query(n*2,l,m,s,e);
    pi3 p2 = query(n*2+1,m+1,r,s,e);
    return merge(p1,p2);
}

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);

    for(int i=1; i<=n*2; i++)
    {
        c[i] = d[(i<=n+1?i:i-n) - 1];
        c[i] += c[i - 1];
    }

    N = n;

    build(1,1,n*2);

    int u,v;
    for(int i=0; i<m; i++)
    {
        scanf("%d %d",&u,&v);
        pi3 ans = mpp(0,0,0);
        if(u <= v) ans = query(1,1,n*2,v+1,n+u-1);
        else ans = query(1,1,n*2,v+1,u-1);
        cout << ans.z << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first.first
#define y first.second
#define z second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
#define INF 99999999999
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,ll> pi3;

int d[MAX];
int h[MAX];
ll c[MAX * 2];
pi3 S[MAX * 6];
int N;

pi3 merge(const pi3& a,const pi3& b)
{
    return make_pair(make_pair(max(a.x,b.x),max(a.y,b.y)),max({a.z,b.z,a.y+b.x}));
}

void build(int n,int l,int r)
{
    if(l == r)
    {
        S[n].x = 2 *1LL* h[(l<=N?l:l-N)] + c[l];
        S[n].y = 2 *1LL* h[(l<=N?l:l-N)] - c[l];
        S[n].z = -INF;
        return;
    }

    int m = (l+r)/2;
    build(n*2,l,m);
    build(n*2+1,m+1,r);
    S[n] = merge(S[n*2],S[n*2+1]);
}

pi3 query(int n,int l,int r,int s,int e)
{
    if(e < l || s > r)
    return mpp(-INF,-INF,-INF);

    if(s <= l && r <= e)
    return S[n];

    int m = (l+r)/2;
    pi3 p1 = query(n*2,l,m,s,e);
    pi3 p2 = query(n*2+1,m+1,r,s,e);
    return merge(p1,p2);
}

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);

    for(int i=1; i<=n*2; i++)
    {
        c[i] = d[(i<=n+1?i:i-n) - 1];
        c[i] += c[i - 1];
    }

    N = n;

    build(1,1,n*2);

    int u,v;
    for(int i=0; i<m; i++)
    {
        scanf("%d %d",&u,&v);
        pi3 ans = mpp(0,0,0);
        if(u <= v) ans = query(1,1,n*2,v+1,n+u-1);
        else ans = query(1,1,n*2,v+1,u-1);
        cout << ans.z << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first.first
#define y first.second
#define z second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> pi3;

int d[MAX];
int h[MAX];
ll c[MAX * 2];
pi3 S[MAX * 6];
int N;

pi3 merge(const pi3& a,const pi3& b)
{
    pi3 c = mpp(0,0,0);
    if(a.z == -1) return b;
    if(b.z == -1) return a;
    if(a.z && b.z){ c = mpp(b.x,a.y,0); return c; }
    if(!a.z && !b.z) { c = a; if(b.x+b.y>c.x+c.y) c = b; if(a.y+b.x>c.x+c.y) c=mpp(b.x,a.y,0); return c; }
    if(a.z){ c = b; if(a.y+b.x>b.x+b.y){c = mpp(b.x,a.y,0);} return c; }
    if(b.z){ c = a; if(a.y+b.x>a.x+a.y){c = mpp(b.x,a.y,0);} return c; }
    return c;
}

void build(int n,int l,int r)
{
    if(l == r)
    {
        S[n].x = 2 *1LL* h[(l<=N?l:l-N)] + c[l];
        S[n].y = 2 *1LL* h[(l<=N?l:l-N)] - c[l];
        S[n].z = 1;
        return;
    }

    int m = (l+r)/2;
    build(n*2,l,m);
    build(n*2+1,m+1,r);
    S[n] = merge(S[n*2],S[n*2+1]);
}

pi3 query(int n,int l,int r,int s,int e)
{
    if(e < l || s > r)
    return mpp(0,0,-1);

    if(s <= l && r <= e)
    return S[n];

    int m = (l+r)/2;
    pi3 p1 = query(n*2,l,m,s,e);
    pi3 p2 = query(n*2+1,m+1,r,s,e);
    return merge(p1,p2);
}

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);

    for(int i=1; i<=n*2; i++)
    {
        c[i] = d[(i<=n+1?i:i-n) - 1];
        c[i] += c[i - 1];
    }

    N = n;

    build(1,1,n*2);

    int u,v;
    for(int i=0; i<m; i++)
    {
        scanf("%d %d",&u,&v);
        pi3 ans = mpp(0,0,0);
        if(u <= v) ans = query(1,1,n*2,v+1,n+u-1);
        else ans = query(1,1,n*2,v+1,u-1);
        cout << ans.x+ans.y << '\n';
    }
}


#include <iostream>
#include<stdio.h>

#include <cmath>

#include <algorithm>

using namespace std;

#define mp make_pair

#define rep(i,a,b) for(int i=a;i<b;++i)

typedef pair<int, int> ii;

typedef  long long int  ll;
const int SIZE = 10e5 + 10;
ii st[SIZE];
ii S[SIZE];
ll A[2 * 100000 + 10];
ll B[2 * 100000 + 10];

ll energie(const int  x, const int  y) {
    if (x == 0 && y == 0) return 0;
    return (x != y) ? 2 * (B[x] + B[y]) + A[y] - A[x] : 2 * B[x];
}
ll max(const ll a, const ll b, const ll c, ll d) {
    return max(max(a, b), max(c, d));
}
ii combine(const ii& a, const ii& b) {
    ll d1 = energie(a.first, a.second);
    ll  d2 = energie(b.first, b.second);
    ll  d3 = energie(a.first, b.first);
    ll  d4 = energie(a.first, b.second);
    ll maxi1 = max(d1, d2, d3, d4);
    ll d5 = energie(a.second, b.first);
    ll d6 = energie(a.second, b.second);
    ll maxi2 = max(d5, d6);
    ll maxi = max(maxi1, maxi2);
    if (d1 == maxi) return a;
    if (d2 == maxi) return b;
    if (d3 == maxi) return mp(a.first, b.first);
    if (d4 == maxi) return mp(a.first, b.second);
    if (d5 == maxi) return mp(a.second, b.first);
    if (d6 == maxi) return mp(a.second, b.second);
}

void build(int p, int l, int r) {
    if (l == r) st[p] = mp(l, l);

    else {
        int mid = (l + r) >>1;
        build(p << 1, l, mid);
        build((p << 1) + 1, mid + 1, r);
        st[p] = combine(st[p << 1], st[(p << 1) + 1]);
    }
}
void Build(int p,int l, int r) {
    if (l == r) S[p] = mp(l, l);

    else {
        int mid = (l + r) >>1;
        Build(p << 1, l, mid);
        Build((p << 1) + 1, mid + 1, r);
        S[p] = combine(S[p << 1], S[(p << 1) + 1]);
    }
}
ii query(int p, int l, int r, int i, int j) {
    if (j < l || r < i) return mp(-1, -1);
    if (i <= l && r <= j) return st[p];
    int mid = (l + r) >>1;
    ii p1 = query(p << 1, l, mid, i, j);
    ii p2 = query((p << 1) + 1, mid + 1, r, i, j);
    if (p1.first == -1 && p1.second == -1) return p2;
    if (p2.first == -1 && p2.second == -1) return p1;
    return combine(p1, p2);
}
ii Query(int p, ll l, ll r, int i, int j) {
    if (j < l || r < i) return mp(-1, -1);
    if (i <= l && r <= j) return S[p];
    int mid = (l + r) >>1;
    ii p1 = Query(p << 1, l, mid, i, j);
    ii p2 = Query((p << 1) + 1, mid + 1, r, i, j);
    if (p1.first == -1 && p1.second == -1) return p2;
    if (p2.first == -1 && p2.second == -1) return p1;
    return combine(p1, p2);
}
int main()
{

    int n ;
    int m ;
    int l; int r;
    scanf("%d%d",&n,&m);

    A[0] = 0, A[1] = 0;
    B[0] = 0;
    rep(i, 2, n + 2) {
        scanf("%lli",&A[i]);

    }
    rep(i, 1, n + 1) {
        scanf("%lli",&B[i]);

    }
     B[n+1]=B[1];
     rep(j, n + 2, 2 * n + 1) {
        A[j] = A[j - n];
        B[j] = B[j - n];
    }
    rep(i, 2, 2 * n) {
        A[i] += A[i - 1];

    }

     ii b;
    ii A1; ii B1;
    build(1, 1, 2 * n);
    Build(1, 1, n);
    /* rep(k, 1, 8 * n + 1)  cout << "(" << st[k].first << "," << st[k].second << "," << energie(st[k].first, st[k].second) << ")" << "\n";*/

    ll ans;

    rep(i, 1, m + 1) {
        scanf("%d%d",&l,&r);
        if (l <= r) {

            b = query(1, 1, 2 * n, r + 1, n + l - 1);
            A1 = (l != 1) ? Query(1, 1, n, 1, l - 1) : mp(0, 0);
            B1 = ((r + 1) != n) ? Query(1, 1, n, r + 1, n) : mp(0, 0);

            ans = max(1, energie(b.first, b.second), energie(A1.first, A1.second), energie(B1.first, B1.second));

             printf("%lli\n",ans);

        }
        else {
            A1 = Query(1, 1, n, r + 1, l - 1);
            printf("%lli\n",energie(A1.first, A1.second)) ;
        }

    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define out(x) cerr << #x"=" << x << endl

const int N = 1e5 + 10;
const long long INF = (long long)1e17 + 10;

int n, m;
int d[2 * N];
int h[2 * N];
int nxt[2 * N][19];
long long dd[2 * N];
long long mx[2 * N][19], mn[2 * N][19];
long long dx[2 * N][19];

long long getmx(int a, int b) {
  if (a == b) return -INF;
  int p = b - a;
  for (int i = 18; i >= 0; --i) {
    if (p >> i & 1) {
      return max(mx[a][i], getmx(nxt[a][i], b));
    }
  }
}

long long solve(int a, int b) {
  if (a == b) return 0;
  int p = b - a;
  for (int i = 18; i >= 0; --i) {
    if (p >> i & 1) {
      long long t = getmx(nxt[a][i], b);
      return max(t - mn[a][i], max(dx[a][i], solve(nxt[a][i], b)));
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", d + i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", h + i);
  }
  for (int i = n; i < 2 * n; ++i) {
    d[i] = d[i - n];
    h[i] = h[i - n];
  }
  n *= 2;
  for (int i = 1; i < n; ++i) {
    dd[i] = dd[i - 1] + d[i - 1];
  }
  for (int i = 0; i < n; ++i) {
    nxt[i][0] = min(i + 1, n - 1);
    mx[i][0] = dd[i] + 2LL * h[i];
    mn[i][0] = dd[i] - 2LL * h[i];
  }
  /*
  for (int i = 0; i < n; ++i) {
    printf("%d ", mx[i][0]);
  }
  puts("");
  for (int i = 0; i < n; ++i) {
    printf("%d ", mn[i][0]);
  }
  puts("");*/
  for (int j = 1; j < 19; ++j) {
    for (int i = 0; i < n; ++i) {
      int t = nxt[i][j - 1];
      nxt[i][j] = nxt[t][j - 1];
      mx[i][j] = max(mx[i][j - 1], mx[t][j - 1]);
      mn[i][j] = min(mn[i][j - 1], mn[t][j - 1]);
      if (t != i) {
        dx[i][j] = max(dx[i][j - 1], dx[t][j - 1]);
        dx[i][j] = max(dx[i][j], mx[t][j - 1] - mn[i][j - 1]);
      }
    }
  }
  for (int i = 0, a, b; i < m; ++i) {
    scanf("%d%d", &a, &b);
    if (a <= b) {
      a += n / 2;
    }
    swap(a, b);
    b--;
    printf("%I64d\n", solve(a, b));
  }
  return 0;
}


#include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int d[200010],h[200010];
long long L[200010],R[200010];
struct _{
    long long val;
    int id;
    bool operator>(const _ &ob)const{
        return val>ob.val;
    }
};
_ lmax[800010],lse[800010];
_ rmax[800010],rse[800010];
pair<_,_> mix(pair<_,_> x,pair<_,_> y){
    _ a[4]={x.first,x.second,y.first,y.second};
    sort(a,a+4,greater<_>());
    return make_pair(a[0],a[1]);
}
void pushupl(int rt){
    pair<_,_> temp=mix(make_pair(lmax[rt<<1],lse[rt<<1]),make_pair(lmax[rt<<1|1],lse[rt<<1|1]));
    lmax[rt]=temp.first;
    lse[rt]=temp.second;
}
void buildl(int l,int r,int rt){
    if(l==r){
        lmax[rt]=(_){L[l],l};
        lse[rt]=(_){-998244353998244353,-1};
        return;
    }
    int m=l+r>>1;
    buildl(l,m,rt<<1);
    buildl(m+1,r,rt<<1|1);
    pushupl(rt);
}
pair<_,_> queryl(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R) return make_pair(lmax[rt],lse[rt]);
    int m=l+r>>1;
    pair<_,_> t1,t2;
    if(m>=L){
        t1=queryl(L,R,l,m,rt<<1);
        if(m<R){
            t2=queryl(L,R,m+1,r,rt<<1|1);
            return mix(t1,t2);
        }
        return t1;
    }
    else return queryl(L,R,m+1,r,rt<<1|1);
}
void pushupr(int rt){
    pair<_,_> temp=mix(make_pair(rmax[rt<<1],rse[rt<<1]),make_pair(rmax[rt<<1|1],rse[rt<<1|1]));
    rmax[rt]=temp.first;
    rse[rt]=temp.second;
}
void buildr(int l,int r,int rt){
    if(l==r){
        rmax[rt]=(_){R[l],l};
        rse[rt]=(_){-998244353998244353,-1};
        return;
    }
    int m=l+r>>1;
    buildr(l,m,rt<<1);
    buildr(m+1,r,rt<<1|1);
    pushupr(rt);
}
pair<_,_> queryr(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R) return make_pair(rmax[rt],rse[rt]);
    int m=l+r>>1;
    pair<_,_> t1,t2;
    if(m>=L){
        t1=queryr(L,R,l,m,rt<<1);
        if(m<R){
            t2=queryr(L,R,m+1,r,rt<<1|1);
            return mix(t1,t2);
        }
        return t1;
    }
    else return queryr(L,R,m+1,r,rt<<1|1);
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int i;
    for(i=1;i<=n;i++){
        scanf("%d",&d[i%n+1]);
    }
    for(i=1;i<=n;i++){
        scanf("%d",&h[i]);
    }
    for(i=1;i<=n;i++){
        d[i+n]=d[i];
        h[i+n]=h[i];
    }
    long long sum=0;
    for(i=1;i<=n<<1;i++){
        sum+=d[i];
        L[i]=sum+(h[i]<<1);
        R[i]=-sum+(h[i]<<1);
    }
    buildl(1,n<<1,1);
    buildr(1,n<<1,1);
    pair<_,_> t1,t2;
    int l,r;
    for(i=0;i<m;i++){
        scanf("%d%d",&l,&r);
        if(l<=r){
            t1=queryl(r+1,l+n-1,1,n<<1,1);
            t2=queryr(r+1,l+n-1,1,n<<1,1);
        }
        else{
            t1=queryl(r+1,l-1,1,n<<1,1);
            t2=queryr(r+1,l-1,1,n<<1,1);
        }
        if(t1.first.id!=t2.first.id) printf("%lld\n",t1.first.val+t2.first.val);
        else{
            long long t;
            t=max(t1.second.val+t2.first.val,t1.first.val+t2.second.val);
            printf("%lld\n",t);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define N 200010
#define ls x<<1
#define rs x<<1|1
#define INF 0x7fffffffffffffff
#define int long long
int n,m,d[N],h[N];
int gi(){
	int w=0,f=1;char ch;
	while(!isdigit(ch)){f=ch=='-'?-1:1;ch=getchar();}
	while(isdigit(ch)){w=(w<<1)+(w<<3)+ch-48;ch=getchar();}
	return w*f;
}
struct SEGTREE{
	struct node{
		int ans,val1,val2;
	}a[N<<2];
	void clear(){
		for(int i=1;i<=800000;i++)a[i]=(node){-INF,-INF,-INF};
	}
	void update(int x,int l,int r,int p){
		if(l==r){
			a[x].val1=2*h[l]-d[l-1];
			a[x].val2=2*h[l]+d[l-1];
			return;
		}
		int mid=(l+r)>>1;
		if(p<=mid)update(ls,l,mid,p);
		else update(rs,mid+1,r,p);
		a[x].val1=max(a[ls].val1,a[rs].val1);
		a[x].val2=max(a[ls].val2,a[rs].val2);
		a[x].ans=max(max(a[ls].ans,a[rs].ans),a[ls].val1+a[rs].val2);
	}
	node query(int x,int l,int r,int xl,int xr){
		if(l==xl&&r==xr){
			return a[x];
		}
		int mid=(l+r)>>1;
		if(xr<=mid)return query(ls,l,mid,xl,xr);
		else if(xl>mid)return query(rs,mid+1,r,xl,xr);
		else{
			node tmp1=query(ls,l,mid,xl,mid),tmp2=query(rs,mid+1,r,mid+1,xr);
			return (node){max(tmp1.val1+tmp2.val2,max(tmp1.ans,tmp2.ans)),max(tmp1.val1,tmp2.val1),max(tmp1.val2,tmp2.val2)};
		}
	}
}T;
signed main(){
	n=gi();m=gi();
	for(int i=1;i<=n;i++)d[i]=gi(),d[i+n]=d[i];
	for(int i=1;i<=2*n;i++)d[i]=d[i-1]+d[i];
	for(int i=1;i<=n;i++)h[i]=gi(),h[i+n]=h[i];
	n*=2;
	T.clear();
	for(int i=1;i<=n;i++)T.update(1,1,n,i);
	for(int i=1;i<=m;i++){
		int a=gi(),b=gi();
		if(a<=b){
			a+=(n/2);
			a--;b++;
			printf("%lld\n",T.query(1,1,n,b,a).ans);
		}
		else{
			a--;b++;
			printf("%lld\n",T.query(1,1,n,b,a).ans);
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i = (a);i <= (b);++i)
typedef long long ll;
const ll inf = 1e17;
const int maxn = 1e5+5;
const int mod = 1e9+7;

ll qpow(ll a,ll b){ll res = 1;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll h[maxn*2],d[maxn*2],pre[maxn*2];
int a[maxn];
struct node
{
    node(){val1 = val2 = sum = -inf;}
    ll val1,val2,sum;//val1--h+p

};
struct Segment_tree
{
    node tree[maxn<<2];
    node pushup(node root,node root1,node root2){
        root.sum = max(root1.sum,root2.sum);
        root.sum = max(root2.val1+root1.val2,root.sum);
        root.val1 = max(root1.val1,root2.val1);
        root.val2 = max(root1.val2,root2.val2);
        return root;
    }
    void build(int root,int l,int r){
        if(l==r){
            tree[root].val1 = h[l]+pre[l];
            tree[root].val2 = h[l]-pre[l];
            tree[root].sum = 0;
            return;
        }
        int mid = l+r>>1;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
        tree[root]=pushup(tree[root],tree[root<<1],tree[root<<1|1]);
    }
    node query(int root,int l,int r,int ql,int qr){
        if(l>=ql&&r<=qr)return tree[root];
        int mid = l+r>>1;
        node t1,t2,t3;
        if(ql<=mid)t1 = query(root<<1,l,mid,ql,qr);
        if(qr>mid)t2 = query(root<<1|1,mid+1,r,ql,qr);
        return pushup(t3,t1,t2);
    }
}sg;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("simple.in","r",stdin);
    freopen("simple.out","w",stdout);
#endif
    int n,m;
    scanf("%d%d",&n,&m);
    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 = 1;i <= n;++i){
        d[i+n]=d[i];
        h[i+n]=h[i];
    }
    for(int i = 1;i <= n*2;++i){
        pre[i]=pre[i-1]+d[i-1];
    }
    sg.build(1,1,2*n);
    int l,r;
    while(m--){
        scanf("%d%d",&l,&r);
        int ql,qr;
        if(l>r)r+=n;
        ql = r+1,qr = r+(n-(r-l+1));
        printf("%lld\n",sg.query(1,1,2*n,ql,qr).sum);
    }
    return 0;
}
//3 5 1 2 4 3 5 1 2 4


#include <bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i = (a);i <= (b);++i)
typedef long long ll;
const ll inf = 1e17;
const int maxn = 2e5+5;
const int mod = 1e9+7;

ll qpow(ll a,ll b){ll res = 1;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll h[maxn*2],d[maxn*2],pre[maxn*2];

struct node
{
    node(){val1 = val2 = sum = -inf;}
    ll val1,val2,sum;//val1--h+p

};
struct Segment_tree
{
    node tree[maxn<<2];
    node pushup(node root,node root1,node root2){
        root.sum = max(root1.sum,root2.sum);
        root.sum = max(root2.val1+root1.val2,root.sum);
        root.val1 = max(root1.val1,root2.val1);
        root.val2 = max(root1.val2,root2.val2);
        return root;
    }
    void build(int root,int l,int r){
        if(l==r){
            tree[root].val1 = h[l]+pre[l];
            tree[root].val2 = h[l]-pre[l];
            tree[root].sum = 0;
            return;
        }
        int mid = l+r>>1;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
        tree[root]=pushup(tree[root],tree[root<<1],tree[root<<1|1]);
    }
    node query(int root,int l,int r,int ql,int qr){
        if(l>=ql&&r<=qr)return tree[root];
        int mid = l+r>>1;
        node t1,t2,t3;
        if(ql<=mid)t1 = query(root<<1,l,mid,ql,qr);
        if(qr>mid)t2 = query(root<<1|1,mid+1,r,ql,qr);
        return pushup(t3,t1,t2);
    }
}sg;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("simple.in","r",stdin);
    freopen("simple.out","w",stdout);
#endif
    int n,m;
    scanf("%d%d",&n,&m);
    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 = 1;i <= n;++i){
        d[i+n]=d[i];
        h[i+n]=h[i];
    }
    for(int i = 1;i <= n*2;++i){
        pre[i]=pre[i-1]+d[i-1];
    }
    sg.build(1,1,2*n);
    int l,r;
    while(m--){
        scanf("%d%d",&l,&r);
        int ql,qr;
        if(l>r)r+=n;
        ql = r+1,qr = r+(n-(r-l+1));
        printf("%lld\n",sg.query(1,1,2*n,ql,qr).sum);
    }
    return 0;
}
//3 5 1 2 4 3 5 1 2 4


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,ans;
int d[200010],h[200010],sa[200010],sb[200010],dd[200010];
struct Tree{
    int l,r,sum,a,b;
}tree[800010];
int read(){
    int r=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0') r=(r<<1)+(r<<3)+c-'0',c=getchar();
    return r*f;
}
void put(int x){
    if(x<0){putchar('-');x=~x+1;}
    if(x>9) put(x/10);
    putchar(x%10+'0');
}
void build(int p,int l,int r){
    tree[p].l=l,tree[p].r=r;
    if(!(l^r)){tree[p].a=sa[l],tree[p].b=sb[l],tree[p].sum=-1e18;return ;}
    int mid=(tree[p].l+tree[p].r)>>1;
    build(p<<1,l,mid),build((p<<1)+1,mid+1,r);
    tree[p].a=max(tree[p<<1].a,tree[(p<<1)+1].a),tree[p].b=max(tree[p<<1].b,tree[(p<<1)+1].b);
    tree[p].sum=max(tree[(p<<1)+1].a+tree[p<<1].b,max(tree[p<<1].sum,tree[(p<<1)+1].sum));
}
Tree query(int p,int l,int r){
    if(l<=tree[p].l&&r>=tree[p].r) return tree[p];
    int mid=(tree[p].l+tree[p].r)>>1;
    Tree p1,p2,pp;pp.a=p1.a=p2.a=pp.b=p1.b=p2.b=pp.sum=p1.sum=p2.sum=-1e18;
    if(l<=mid) p1=query(p<<1,l,r);
    if(r>mid) p2=query((p<<1)+1,l,r);
    pp.a=max(p1.a,p2.a),pp.b=max(p1.b,p2.b);
    pp.sum=max(p2.a+p1.b,max(p1.sum,p2.sum));
    return pp;
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++) d[n+i]=d[i]=read();
    for(int i=1;i<=n;i++) h[n+i]=h[i]=read();
    for(int i=1;i<=(n<<1);i++) dd[i]=dd[i-1]+d[i],sa[i]=(h[i]<<1)+dd[i-1],sb[i]=(h[i]<<1)-dd[i-1];
    build(1,1,(n<<1));
    while(m--){
        int rs=read()-1,ls=read()+1;
        if(ls>rs) rs+=n;
        ans=query(1,ls,rs).sum;
        put(ans);puts("");
    }
    return 0;
}


#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>

using namespace std;

typedef long long ll;

const ll N = 600010;
const ll M = 200010;

struct node {
	ll ls, rs, mmax;
}t[N];

ll n, m;
ll a[M], h[M], s[M];

void build(ll id, ll l, ll r) {
	if (l == r) {
		t[id].ls = 2 * h[l] - s[l], t[id].rs = 2 * h[l] + s[l];
		return;
	}

	ll mid = (l + r) / 2;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
	t[id].ls = max(t[id << 1].ls, t[id << 1 | 1].ls);
	t[id].rs = max(t[id << 1].rs, t[id << 1 | 1].rs);
	t[id].mmax = max(t[id << 1].ls + t[id << 1 | 1].rs, max(t[id << 1].mmax, t[id << 1 | 1].mmax));
}

node query(ll id, ll l, ll r, ll ql, ll qr) {
	if (l == ql && r == qr) return t[id];

	ll mid = (l + r) / 2;
	if (qr <= mid) return query(id << 1, l, mid, ql, qr);
	else if (ql > mid) return query(id << 1 | 1, mid + 1, r, ql, qr);

	node lchild = query(id << 1, l, mid, ql, mid), rchild = query(id << 1 | 1, mid + 1, r, mid + 1, qr), res;
	res.ls = max(lchild.ls, rchild.ls);
	res.rs = max(lchild.rs, rchild.rs);
	res.mmax = max(lchild.ls + rchild.rs, max(lchild.mmax, rchild.mmax));
	return res;
}

int main() {
	cin >> n >> m;
	for (ll i = 1; i <= n; i++) cin >> a[i];
	for (ll i = 1; i <= n; i++) {
		ll t;
		cin >> t;
		h[i] = h[i + n] = t;
	}
	for (ll i = 2; i <= n + 1; i++) s[i] = s[i - 1] + a[i - 1];
	for (ll i = 2; i <= n; i++) s[i + n] = s[n + 1] + s[i];

	build(1, 1, 2 * n);

	ll x, y;
	while (m--) {
		cin >> x >> y;
		if (x <= y) cout << query(1, 1, 2 * n, y + 1, x + n - 1).mmax << endl;
		else cout << query(1, 1, 2 * n, y + 1, x - 1).mmax << endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define It set<node>::iterator
#define endl '\n'
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=1e5+10;
const int inf=0x3f3f3f3f;

struct seg{
	int l,r,dat1,dat2,id;
}t[8*maxn];

int n,m;
int h[2*maxn],d[2*maxn],pre[2*maxn];

void push_up(int x){
	t[x].dat1=max(t[x<<1].dat1,t[x<<1|1].dat1);
	if(t[x<<1].dat1>t[x<<1|1].dat1) t[x].id=t[x<<1].id;
	else t[x].id=t[x<<1|1].id;
	t[x].dat2=max(t[x<<1].dat2,t[x<<1|1].dat2);
}

void build(int l,int r,int x){
	t[x].l=l,t[x].r=r,t[x].dat1=h[l]-pre[l],t[x].dat2=h[l]+pre[l],t[x].id=x;
	if(l==r) return ;
	int mid=(l+r)>>1;
	build(l,mid,x<<1);
	build(mid+1,r,x<<1|1);
	push_up(x);
}

pii query1(int l,int r,int x){
	int _id;
	if(t[x].l>=l&&t[x].r<=r) return make_pair(t[x].dat1,t[x].id);
	int mid=(t[x].l+t[x].r)>>1,res=-inf;
	if(l<=mid){
		pii k=query1(l,r,x<<1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	if(r>mid){
		pii k=query1(l,r,x<<1|1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	return make_pair(res,_id);
}

pii query2(int l,int r,int x){
	int _id;
	if(t[x].l>=l&&t[x].r<=r) return make_pair(t[x].dat2,t[x].id);
	int mid=(t[x].l+t[x].r)>>1,res=-inf;
	if(l<=mid){
		pii k=query2(l,r,x<<1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	if(r>mid){
		pii k=query2(l,r,x<<1|1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	return make_pair(res,_id);
}

signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;++i) scanf("%lld",&d[i]);
	for(int i=1;i<=n;++i) scanf("%lld",&h[i]),h[i]=h[i]*2;
	for(int i=n+1;i<2*n;++i) h[i]=h[i-n];
	for(int i=2;i<=n+1;++i) pre[i]=pre[i-1]+d[i-1];
	for(int i=n+2;i<2*n;++i) pre[i]=pre[i-1]+d[i-n-1];
//	for(int i=1;i<2*n;++i) cout<<h[i]-pre[i]<<" ";
//	cout<<endl;
	//dat1=Lx=h[x]-pre[x],dat2=Rx=h[x]+pre[x]
	build(1,2*n-1,1);
	for(int i=1;i<=m;++i){
		int a,b;
		scanf("%lld%lld",&a,&b);
		if(a<=b){
			int L=b+1,R=n+a-1;
			pii k=query1(L,R,1);
			int pos=t[k.second].l;
			int res1=k.first+max(query2(L,pos-1,1).first,query2(pos+1,R,1).first);
			k=query2(L,R,1);
			pos=t[k.second].l;
			int res2=k.first+max(query1(L,pos-1,1).first,query1(pos+1,R,1).first);
			printf("%lld\n",max(res1,res2));
//			cout<<L<<" "<<R<<" "<<k.first<<" "<<k.second<<endl;
		}else{
			int L=b+1,R=a-1;
			pii k=query1(L,R,1);
			int pos=t[k.second].l;
			int res1=k.first+max(query2(L,pos-1,1).first,query2(pos+1,R,1).first);
			k=query2(L,R,1);
			pos=t[k.second].l;
			int res2=k.first+max(query1(L,pos-1,1).first,query1(pos+1,R,1).first);
			printf("%lld\n",max(res1,res2));
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define It set<node>::iterator
#define endl '\n'
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=1e5+10;
const int inf=0x3f3f3f3f3f3f3f3f;

struct seg{
	int l,r,dat1,dat2,id;
}t[8*maxn];

int n,m;
int h[2*maxn],d[2*maxn],pre[2*maxn];

void push_up(int x){
	t[x].dat1=max(t[x<<1].dat1,t[x<<1|1].dat1);
	if(t[x<<1].dat1>t[x<<1|1].dat1) t[x].id=t[x<<1].id;
	else t[x].id=t[x<<1|1].id;
	t[x].dat2=max(t[x<<1].dat2,t[x<<1|1].dat2);
}

void build(int l,int r,int x){
	t[x].l=l,t[x].r=r,t[x].dat1=h[l]-pre[l],t[x].dat2=h[l]+pre[l],t[x].id=x;
	if(l==r) return ;
	int mid=(l+r)>>1;
	build(l,mid,x<<1);
	build(mid+1,r,x<<1|1);
	push_up(x);
}

pii query1(int l,int r,int x){
	int _id=x;
	if(t[x].l>=l&&t[x].r<=r) return make_pair(t[x].dat1,t[x].id);
	int mid=(t[x].l+t[x].r)>>1,res=-inf;
	if(l<=mid){
		pii k=query1(l,r,x<<1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	if(r>mid){
		pii k=query1(l,r,x<<1|1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	return make_pair(res,_id);
}

pii query2(int l,int r,int x){
	int _id=x;
	if(t[x].l>=l&&t[x].r<=r) return make_pair(t[x].dat2,t[x].id);
	int mid=(t[x].l+t[x].r)>>1,res=-inf;
	if(l<=mid){
		pii k=query2(l,r,x<<1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	if(r>mid){
		pii k=query2(l,r,x<<1|1);
		if(k.first>res){
			res=k.first;
			_id=k.second;
		}
	}
	return make_pair(res,_id);
}

signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;++i) scanf("%lld",&d[i]);
	for(int i=1;i<=n;++i) scanf("%lld",&h[i]),h[i]=h[i]*2;
	for(int i=n+1;i<2*n;++i) h[i]=h[i-n];
	for(int i=2;i<=n+1;++i) pre[i]=pre[i-1]+d[i-1];
	for(int i=n+2;i<2*n;++i) pre[i]=pre[i-1]+d[i-n-1];
//	for(int i=1;i<2*n;++i) cout<<h[i]-pre[i]<<" ";
//	cout<<endl;
	//dat1=Lx=h[x]-pre[x],dat2=Rx=h[x]+pre[x]
	build(1,2*n-1,1);
	for(int i=1;i<=m;++i){
		int a,b;
		scanf("%lld%lld",&a,&b);
		if(a<=b){
			int L=b+1,R=n+a-1;
			pii k=query1(L,R,1);
			int pos=t[k.second].l;
			int res1=k.first+max(query2(L,pos-1,1).first,query2(pos+1,R,1).first);
			k=query2(L,R,1);
			pos=t[k.second].l;
			int res2=k.first+max(query1(L,pos-1,1).first,query1(pos+1,R,1).first);
			printf("%lld\n",max(res1,res2));
//			cout<<L<<" "<<R<<" "<<k.first<<" "<<k.second<<endl;
		}else{
			int L=b+1,R=a-1;
			pii k=query1(L,R,1);
			int pos=t[k.second].l;
			int res1=k.first+max(query2(L,pos-1,1).first,query2(pos+1,R,1).first);
			k=query2(L,R,1);
			pos=t[k.second].l;
			int res2=k.first+max(query1(L,pos-1,1).first,query1(pos+1,R,1).first);
			printf("%lld\n",max(res1,res2));
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

typedef long long ll;

const ll MAXN = 1e5 + 20;
const ll LOG = 20;

ll n, q;
ll d[MAXN], h[MAXN];
ll seg[4 * MAXN], ss[LOG][4 * MAXN], maxi[LOG][4 * MAXN];
ll le[MAXN], ri[MAXN];

void build(ll v, ll b, ll e, ll de = 0){
	if (e - b == 1){
		seg[v] = h[b];
		return;
	}

	ll mid = (b + e)/ 2;
	build(v<<1, b, mid, de + 1);
	build(v<<1^1, mid, e, de + 1);

	maxi[de][mid - 1] = h[mid - 1];
	ll sum = d[mid - 2];
	for (ll i = mid - 2; i >= b; i--){
		maxi[de][i] = max(h[i] + sum, maxi[de][i + 1]);
		sum += (i == b?0:d[i - 1]);
	}

	maxi[de][mid] = h[mid];
	sum = d[mid];
	for (ll i = mid + 1; i < e; i++){
		maxi[de][i] = max(sum + h[i], maxi[de][i - 1]);
		sum += d[i];
	}

	seg[v] = max(seg[v<<1], seg[v<<1^1]);
	seg[v] = max(seg[v], maxi[de][b] + d[mid - 1] + maxi[de][e - 1]);
}

ll get(ll v, ll b, ll e, ll l, ll r, ll de = 0){
	if (l <= b && e <= r)	return	seg[v];
	if (r <= b || e <= l)	return	0;

	ll mid = (b + e)/ 2;
	ll f = get(v<<1, b, mid, l, r, de + 1), g = get(v<<1^1, mid, e, l, r, de + 1);

	if (g == 0)	return	f;
	if (f == 0)	return	g;
	ll ret = max(f, g);

	l = max(l, b);
	r = min(r - 1, e - 1);
	return	max(ret, maxi[de][l] + maxi[de][r] + d[mid - 1]);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q;
	for (ll i = 0; i < n; i++)	cin >> d[i];
	for (ll i = 0; i < n; i++)	cin >> h[i], h[i] *= 2;

	le[0] = h[0];
	ll sum = d[0];
	for (ll i = 1; i < n; i++){
		le[i] = max(le[i - 1], h[i] + sum);
		sum += d[i];
	}

	ri[n - 1] = h[n - 1];
	sum = d[n - 2];
	for (ll i = n - 2; i >= 0; i--)
		ri[i] = max(ri[i + 1], h[i] + sum), sum += (i == 0?0:d[i - 1]);
	build(1, 0, n);

	while (q--){
		ll l, r;	cin >> l >> r;	l--;
		if (l >= r)
			cout << get(1, 0, n, r, l) << "\n";
		else{
			ll f = 0, g = 0;
			if (l != 0)	f = get(1, 0, n, 0, l);
			if (r != n)	g = get(1, 0, n, r, n);
			if (f == 0)
				cout << g << "\n";
			else if (g == 0)
				cout << f << "\n";
			else
				cout << max(max(f, g), le[l - 1] + ri[r] + d[n - 1]) << "\n";
		}
	}
	return 0;
}


//It's Deemo now :)

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

typedef long long ll;

const ll MAXN = 1e5 + 20;
const ll LOG = 20;

ll n, q;
ll d[MAXN], h[MAXN];
ll seg[4 * MAXN], ss[LOG][4 * MAXN], maxi[LOG][4 * MAXN];
ll le[MAXN], ri[MAXN];

void build(ll v, ll b, ll e, ll de = 0){
	if (e - b == 1){
		seg[v] = h[b];
		return;
	}

	ll mid = (b + e)/ 2;
	build(v<<1, b, mid, de + 1);
	build(v<<1^1, mid, e, de + 1);

	maxi[de][mid - 1] = h[mid - 1];
	ll sum = d[mid - 2];
	for (ll i = mid - 2; i >= b; i--){
		maxi[de][i] = max(h[i] + sum, maxi[de][i + 1]);
		sum += (i == b?0:d[i - 1]);
	}

	maxi[de][mid] = h[mid];
	sum = d[mid];
	for (ll i = mid + 1; i < e; i++){
		maxi[de][i] = max(sum + h[i], maxi[de][i - 1]);
		sum += d[i];
	}

	seg[v] = max(seg[v<<1], seg[v<<1^1]);
	seg[v] = max(seg[v], maxi[de][b] + d[mid - 1] + maxi[de][e - 1]);
}

ll get(ll v, ll b, ll e, ll l, ll r, ll de = 0){
	if (l <= b && e <= r)	return	seg[v];
	if (r <= b || e <= l)	return	0;

	ll mid = (b + e)/ 2;
	ll f = get(v<<1, b, mid, l, r, de + 1), g = get(v<<1^1, mid, e, l, r, de + 1);

	if (g == 0)	return	f;
	if (f == 0)	return	g;
	ll ret = max(f, g);

	l = max(l, b);
	r = min(r - 1, e - 1);
	return	max(ret, maxi[de][l] + maxi[de][r] + d[mid - 1]);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q;
	for (ll i = 0; i < n; i++)	cin >> d[i];
	for (ll i = 0; i < n; i++)	cin >> h[i], h[i] *= 2;

	le[0] = h[0];
	ll sum = d[0];
	for (ll i = 1; i < n; i++){
		le[i] = max(le[i - 1], h[i] + sum);
		sum += d[i];
	}

	ri[n - 1] = h[n - 1];
	sum = d[n - 2];
	for (ll i = n - 2; i >= 0; i--)
		ri[i] = max(ri[i + 1], h[i] + sum), sum += (i == 0?0:d[i - 1]);
	build(1, 0, n);

	while (q--){
		ll l, r;	cin >> l >> r;	l--;
		if (l >= r)
			cout << get(1, 0, n, r, l) << "\n";
		else{
			ll f = 0, g = 0;
			if (l != 0)	f = get(1, 0, n, 0, l);
			if (r != n)	g = get(1, 0, n, r, n);
			if (f == 0)
				cout << g << "\n";
			else if (g == 0)
				cout << f << "\n";
			else
				cout << max(max(f, g), le[l - 1] + ri[r] + d[n - 1]) << "\n";
		}
	}
	return 0;
}


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

#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;
	long long val;
} st[200000<<3];

int n,m;
long long d[400000],h[400000];
long long sum[400000];
long long ans;
int idx;

long long 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;
	long long 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;
	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;
	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);
		long long 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++)
	{
		cin>>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++)
	{
		cin>>h[i];
		h[i+n]=h[i];
	}
	build(root);
	int l,r;
	for(int i=1; i <= m; i++)
	{
		cin>>l>>r;
		ans=0,idx=-1;
		l <= r? query(root,r+1,l+n-1):query(root,r+1,l-1);
		cout<<ans<<endl;
	}
	return 0;
}


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

#define I inline
#define ri register int
#define ll long long
#define ull unsigned long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define rep(i,a,b) for(ri i=(a);i<=(b);++i)
#define dow(i,a,b) for(ri i=(a);i>=(b);--i)
#define go(i,u) for(ri i=lnk[u];i;i=nxt[i])
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define vec vector
#define que queue
#define pb push_back
#define lowbit(x) (x&(-x))
#define mem(f,x) memset(f,x,sizeof(f))

I int gi(){
    int x=0,f=0;char ch=getchar();
    while(!isdigit(ch)){f|=ch=='-',ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48,ch=getchar();}
    return f?-x:x;
}

I void pi(int x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)pi(x/10);
    putchar(x%10+48);
}
I void pib(int x){pi(x);putchar(' ');}
I void pin(int x){pi(x);putchar('\n');}

const int N = 2e5+5,S = N<<2,inf = 0x3f3f3f3f;

struct node{int lx,rx,mx;}t[S];
int d[N],h[N],n,m;

#define lc (p<<1)
#define rc (p<<1|1)

I void pushup(int p){
    t[p].lx = max(t[lc].lx,t[rc].lx);
    t[p].rx = max(t[lc].rx,t[rc].rx);
    t[p].mx = max(t[lc].lx + t[rc].rx ,max(t[lc].mx,t[rc].mx));
}

void build(int p,int l,int r){
    if(l==r){
        t[p]=(node){2*h[l]-d[l-1],2*h[l]+d[l-1],0};
        return;
    }
    int mid=(l+r)>>1;
    build(lc,l,mid);build(rc,mid+1,r);
    pushup(p);
}

node query(int p,int l,int r,int ql,int qr){
    if(ql<=l && r<=qr)return t[p];
    int mid=(l+r)>>1;
    node it;it=(node){-inf,-inf,0};
    node le,re,res;
    le=re=res=it;
    if(ql<=mid)le = query(lc,l,mid,ql,qr);
    if(qr >mid)re = query(rc,mid+1,r,ql,qr);
    if(le.lx == -inf){
        res.lx = max(res.lx,re.lx);
        res.rx = max(res.rx,re.rx);
        res.mx = max(res.mx,re.mx);
    }//不存在左儿子
    else if(re.lx == -inf){
        res.lx = max(res.lx,le.lx);
        res.rx = max(res.rx,le.rx);
        res.mx = max(res.mx,le.mx);
    }//不存在右儿子
    else{
        res.mx = max(le.mx,re.mx);
        res.lx = max(le.lx,re.lx);
        res.rx = max(le.rx,re.rx);
        res.mx = max(le.lx + re.rx,res.mx);
    }
    return res;
}

signed main(){
    n=gi(),m=gi();
    rep(i,1,n)d[i]=d[i+n]=gi();
    rep(i,1,n)h[i]=h[i+n]=gi();
    rep(i,1,n*2)d[i]+=d[i-1];
    build(1,1,n*2);
    while(m--){
        int x=gi(),y=gi();
        if(x<=y)pin(query(1,1,n*2,y+1,n+x-1).mx);
        else pin(query(1,1,n*2,y+1,x-1).mx);
    }
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=6e5+5;
int n,m;
int d[N],h[N];
struct node{int l,r,max;}T[N<<2];

inline node pushup(node L,node R)
{
	node res;
	res.l=max(L.l,R.l);
	res.r=max(R.r,L.r);
	res.max=max(L.l+R.r,max(L.max,R.max));
	return res;
}

void build(int k,int l,int r)
{
	if (l==r)
	{
		T[k].max=0;
		T[k].l=2*h[l]-d[l-1];
		T[k].r=2*h[l]+d[l-1];
	//	printf("l=%lld r=%lld\n",T[k].l,T[k].r);
		return;
	}
	int mid=l+r>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	node L,R;
	L=T[k<<1]; R=T[k<<1|1];
	T[k]=pushup(L,R);
}

node query(int k,int l,int r,int qx,int qy)
{
//	printf("l=%lld r=%lld\n",l,r);
	if (qx<=l && r<=qy) return T[k];
	int mid=l+r>>1;
	if (qx>mid) return query(k<<1|1,mid+1,r,qx,qy);
	if (qy<=mid) return query(k<<1,l,mid,qx,qy);
	node L,R;
	L=query(k<<1,l,mid,qx,qy); R=query(k<<1|1,mid+1,r,qx,qy);
	return pushup(L,R);
}

signed main(){
	scanf("%lld%lld",&n,&m);
	for (register int i=1; i<=n; ++i) scanf("%lld",&d[i]),d[i+n]=d[i];
	for (register int i=1; i<=n; ++i) scanf("%lld",&h[i]),h[i+n]=h[i];
	int len=n;
	n<<=1;
	for (register int i=1; i<=n; ++i) d[i]+=d[i-1];
	build(1,1,n);
	while (m--)
	{
		int x,y;
		scanf("%lld%lld",&x,&y);
		if (x<=y) printf("%lld\n",query(1,1,n,y+1,x+len-1).max);
		else printf("%lld\n",query(1,1,n,y+1,x-1).max);
	}
return 0;
}


#include<bits/stdc++.h>
#define int long long
#define lch(p) p << 1
#define rch(p) p << 1 | 1
#define Inf 0x3f3f3f3f3f3f
#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;
template<class T>inline void read(T &x){
    char t(getchar()), flg(0); x = 0;
    for (; !isdigit(t); t = getchar() ) flg = t == '-';
    for ( ; isdigit(t); t = getchar() ) x = x * 10 + ( t & 15 );
    flg ? x = -x : x;
}
template <class T, class... Args>
inline void read(T &x, Args &... args){read(x);read(args...);}
#define ll long long
const int N = 2e5 + 5;
const ll inf = 1e16;
struct node{
    ll v1, v2, mx;
}a[N << 2];
int n, m;
ll d[N], h[N];
void build(int p, int l, int r){
    if(l == r){
        a[p].v1 = 2 * h[r] + d[r - 1];
        a[p].v2 = 2 * h[l] - d[l - 1];
        a[p].mx = -inf;
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    a[p].v1 = max(a[p << 1].v1, a[p << 1 | 1].v1);
    a[p].v2 = max(a[p << 1].v2, a[p << 1 | 1].v2);
    a[p].mx = max(max(a[p << 1].mx, a[p << 1 | 1].mx), a[p << 1].v2 + a[p << 1 | 1].v1);
}
node query(int p, int l, int r, int L, int R){
    if(l >= L && r <= R) return a[p];
    int mid = (l + r) >> 1;
    node t1 = (node){-inf, -inf, -inf};
    node t2 = t1, t3 = t1;
    if(L <= mid) t1 = query(p << 1, l, mid, L, R);
    if(R > mid) t2 = query(p << 1 | 1, mid + 1, r, L, R);
    t3.v1 = max(t1.v1, t2.v1);
    t3.v2 = max(t1.v2, t2.v2);
    t3.mx = max(max(t1.mx, t2.mx), t1.v2 + t2.v1);
    return t3;
}
int main(){
    read(n, m);
    for(int i = 1; i <= n; ++i) read(d[i]), d[i + n] = d[i];
    for(int i = 1; i <= n; ++i) read(h[i]), h[i + n] = h[i];
    for(int i = 1; i <= 2 * n; ++i) d[i] += d[i - 1];
    build(1, 1, 2 * n);
    while(m--){
        int l, r; read(l, r);
        if(l <= r) printf("%lld\n", query(1, 1, 2 * n, r + 1, l + n - 1).mx);
        else printf("%lld\n", query(1, 1, 2 * n, r + 1, l - 1).mx);
    }
}


#include<bits/stdc++.h>
using namespace std;
template<class T>inline void read(T &x){
    char t(getchar()), flg(0); x = 0;
    for (; !isdigit(t); t = getchar() ) flg = t == '-';
    for ( ; isdigit(t); t = getchar() ) x = x * 10 + ( t & 15 );
    flg ? x = -x : x;
}
template <class T, class... Args>
inline void read(T &x, Args &... args){read(x);read(args...);}
#define ll long long
const int N = 2e5 + 5, inf = 0x3f3f3f3f;
struct node{
    ll v1, v2, mx;
}a[N << 2];
int n, m;
ll d[N], h[N];
void build(int p, int l, int r){
    if(l == r){
        a[p].v1 = 2 * h[r] + d[r - 1];
        a[p].v2 = 2 * h[l] - d[l - 1];
        a[p].mx = -inf;
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    a[p].v1 = max(a[p << 1].v1, a[p << 1 | 1].v1);
    a[p].v2 = max(a[p << 1].v2, a[p << 1 | 1].v2);
    a[p].mx = max(max(a[p << 1].mx, a[p << 1 | 1].mx), a[p << 1].v2 + a[p << 1 | 1].v1);
}
node query(int p, int l, int r, int L, int R){
    if(l >= L && r <= R) return a[p];
    int mid = (l + r) >> 1;
    node t1 = (node){-inf, -inf, -inf};
    node t2 = t1, t3 = t1;
    if(L <= mid) t1 = query(p << 1, l, mid, L, R);
    if(R > mid) t2 = query(p << 1 | 1, mid + 1, r, L, R);
    t3.v1 = max(t1.v1, t2.v1);
    t3.v2 = max(t1.v2, t2.v2);
    t3.mx = max(max(t1.mx, t2.mx), t1.v2 + t2.v1);
    return t3;
}
int main(){
    read(n, m);
    for(int i = 1; i <= n; ++i) read(d[i]), d[i + n] = d[i];
    for(int i = 1; i <= n; ++i) read(h[i]), h[i + n] = h[i];
    for(int i = 1; i <= 2 * n; ++i) d[i] += d[i - 1];
    build(1, 1, 2 * n);
    while(m--){
        int l, r; read(l, r);
        if(l <= r) printf("%lld\n", query(1, 1, 2 * n, r + 1, l + n - 1).mx);
        else printf("%lld\n", query(1, 1, 2 * n, r + 1, l - 1).mx);
    }
}