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

typedef long long ll;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 1e6 + 5;

int n;
int a[maxn];
map<int,map<int,int>> sums, res;

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

  cin>>n;
  for (int i=1; i<=n; i++) {
    cin>>a[i];
  }
  for (int len=1; len<=n; len*=2) {
    for (int i=1; i+len-1<=n; i++) {
      int j = i+len-1;
      if (i==j) {
	sums[i][len]=a[i];
	res[i][len]=0;
      } else {
	res[i][len] = res[i][len/2] + res[i+len/2][len/2];
	sums[i][len] = sums[i][len/2] + sums[i+len/2][len/2];
	if (sums[i][len]>=10) {
	  res[i][len]++;
	}
	sums[i][len] %= 10;
      }
    }
    // watch(len);
    // for (int i=1; i+len-1<=n; i++) {
    //   cout<<res[i][len]<<" ";
    // } cout<<endl;
  }

  // for (auto p: sums) {
  //   for (auto q: p.second) {
  //     cout<<p.first<<" "<<q.first<<" "<<q.second<<endl;
  //   }
  // }

  int q; cin>>q;
  for (;q;q--) {
    int l,r; cin>>l>>r;
    cout<<res[l][r-l+1]<<'\n';
  }

  return 0;
}


#include <bits/stdc++.h>
#define fastIO ios::sync_with_stdio(NULL); cin.tie(NULL);
const int N = 1E6+7;
const int SZ = 2E3+7;

long long dp[SZ][SZ];
long long t[N];
long long f[N];
long long c[N];

int main()
{
    fastIO
    int n;
    cin>>n;

    for (int i=1;i <= n;++i)
    {
        cin>>t[i];
        f[i]=f[i-1]+t[i];
    }
    int t;
    cin>>t;
    for (int i=1;i<=t;++i)
    {
        long long a,b;
        cin>>a>>b;
        cout<<(f[b]-f[a-1])/10<<"\n";
    }
}


#include <bits/stdc++.h>
#define fastIO ios::sync_with_stdio(NULL); cin.tie(NULL);

using namespace std;
const int N = 1E6+7;
const int SZ = 2E3+7;

long long dp[SZ][SZ];
long long t[N];
long long f[N];
long long c[N];

int main()
{
    int n;
    cin>>n;

    for (int i=1;i <= n;++i)
    {
        cin>>t[i];
        f[i]=f[i-1]+t[i];
    }
    int t;
    cin>>t;
    for (int i=1;i<=t;++i)
    {
        int a,b;
        cin>>a>>b;
        cout<<(f[b]-f[a-1])/10<<"\n";
    }
}


#include <bits/stdc++.h>
#define fastIO ios::sync_with_stdio(NULL); cin.tie(NULL);

const int N = 1E6+7;
const int SZ = 2E3+7;

long long dp[SZ][SZ];
long long t[N];
long long f[N];
long long c[N];

int main()
{
    fastIO
    int n;
    cin>>n;

    for (int i=1;i <= n;++i)
    {
        cin>>t[i];
        f[i]=f[i-1]+t[i];
    }
    int t;
    cin>>t;
    for (int i=1;i<=t;++i)
    {
        long long a,b;
        cin>>a>>b;
        cout<<(f[b]-f[a-1])/10<<"\n";
    }
}


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

int main(){

int n;
cin>>n;
int index[n+1];
index[0]=0;
vector<int> a;
for(int i=0;i<n;i++){
    int u;
    cin>>u;
    a.push_back(u);
    index[i+1]=index[i]+u;
}

int q;
cin>>q;

int ans;

for(int k=0;k<q;k++){
    int l,r;
    cin>>l>>r;

    ans=index[r]-index[l-1];
    cout<<(ans/10)<<endl;
}
return 0;
}


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

int main(){

int n;
cin>>n;

vector<int> a;
for(int i=0;i<n;i++){
    int u;
    cin>>u;
    a.push_back(u);
}

int q;
cin>>q;

vector<int> index;

for(int i=0;i<n;i++){
    int s=0;
    for(int j=0;j<=i;j++){
        s=s+a[j];
    }
    index.push_back(s);
}

for(int k=0;k<q;k++){
    int l,r;
    cin>>l>>r;

    int ans=index[r-1]-index[l-1];
    ans=ans/10;
    cout<<ans<<endl;
}
return 0;
}


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

int main(){

int n;
cin>>n;

vector<int> a;
for(int i=0;i<n;i++){
    int u;
    cin>>u;
    a.push_back(u);
}

int q;
cin>>q;

int index[n+1];

for(int i=0;i<n;i++){
    int s=0;
    for(int j=0;j<=i;j++){
        s=s+a[j];
    }
    index[i+1]=s;
}

for(int k=0;k<q;k++){
    int l,r;
    cin>>l>>r;

    int ans=index[r]-index[l-1];
    ans=ans/10;
    cout<<ans<<endl;
}
return 0;
}


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

int main(){

int n;
cin>>n;
int index[n];
vector<int> a;
for(int i=0;i<n;i++){
    int u;
    cin>>u;
    a.push_back(u);
    index[i+1]=index[i]+u;
}
index[0]=0;
int q;
cin>>q;

int ans;

for(int k=0;k<q;k++){
    int l,r;
    cin>>l>>r;

    ans=index[r]-index[l-1];
    cout<<(ans/10)<<endl;
}
return 0;
}


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

int main(){

int n;
cin>>n;

vector<int> a;
for(int i=0;i<n;i++){
    int u;
    cin>>u;
    a.push_back(u);
}

int q;
cin>>q;

for(int k=0;k<q;k++){
    int l,r;
    cin>>l>>r;

    vector<int> b;
    for(int i=l-1;i<r;i++){
        b.push_back(a[i]);
    }
    int c=0;
    while(b.size()!=1){
    for(int i=0;i<b.size();i++){
        b[i]=b[i]+b[i+1];
        if(b[i]>=10){
            c++;
            b[i]=b[i]%10;
        }
        b.erase(b.begin()+(i+1));
    }
    }
    cout<<c<<endl;
}
return 0;
}


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

#define ll long long

int n;

int main(void) {
    scanf("%d", &n);
    std::vector<int> vals;
    for (int i = 0; i < n; ++i) {
        int t; scanf("%d", &t);
        vals.push_back(t);
    }
    int q; scanf("%d", &q);
    while (q --) {
        int l, r; scanf("%d %d", &l, &r);
        std::vector<int> arg;
        l --; r --;
        for (int i = l; i <= r; ++i) {
            arg.push_back(vals[i]);
        }
        int sz = r - l + 1;
        int ans = 0;

        while (sz > 1) {
            std::vector<int> na;
            for (int i = 0; i < sz; i += 2) {
                int sum = arg[i] + arg[i + 1];
                if (sum >= 10) ans ++;
                na.push_back(sum % 10);
            }
            arg.swap(na); sz /= 2;
        }

        printf("%d\n", ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

#define MAX 100000

int n;
std::vector<int> vals;

/* From https://stackoverflow.com/a/994647/4688119 */
static unsigned int mylog2 (unsigned int val) {
    if (val == 0) return UINT_MAX;
    if (val == 1) return 0;
    unsigned int ret = 0;
    while (val > 1) {
        val >>= 1;
        ret++;
    }
    return ret;
}

int main(void) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int t; scanf("%d", &t);
        vals.push_back(t);
    }

    /* pair: sum, candies */
    std::vector<std::vector<std::pair<int, int>>> dp(21, std::vector<std::pair<int, int>>
            (n, std::make_pair(0, 0)));

    for (int i = 0; i < n; ++i)
        dp[0][i] = std::make_pair(vals[i], 0);

    for (int i = 1; i < 21; ++i) {
        int rai = 1 << i;
        for (int j = 0; j < (n - rai + 1); ++j) {
            std::pair<int, int> left = dp[i - 1][j];
            std::pair<int, int> right = dp[i - 1][j + (rai / 2)];
            dp[i][j] = std::make_pair((left.first + right.first) % 10, left.second + right.second);
            if ((left.first + right.first) >= 10)
                dp[i][j].second ++;
        }
    }

    int q;
    scanf("%d", &q);
    while (q --) {
        int l, r; scanf("%d %d", &l, &r); l --; r--;
        int k = mylog2(r - l + 1);
        printf("%d\n", dp[k][l].second);
    }
    return 0;
}


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

#define ll long long

int n;

int foo(std::vector<int> vals, int sz) {
    int ret = 0;
    if (sz == 1) return ret;
    std::vector<int> new_arg;

    for (int i = 0; i < sz; i += 2) {
        int sum = vals[i] + vals[i + 1];
        if (sum >= 10) ret ++;
        new_arg.push_back(sum % 10);
    }
    return ret + foo(new_arg, sz / 2);
}

int main(void) {
    scanf("%d", &n);
    std::vector<int> vals;
    for (int i = 0; i < n; ++i) {
        int t; scanf("%d", &t);
        vals.push_back(t);
    }
    int q; scanf("%d", &q);
    while (q --) {
        int l, r; scanf("%d %d", &l, &r);
        std::vector<int> arg;
        l --; r --;
        for (int i = l; i <= r; ++i) {
            arg.push_back(vals[i]);
        }

        printf("%d\n", foo(arg, r - l + 1));
    }
    return 0;
}


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

#define ll long long

int n;
std::vector<int> vals;

int foo(std::vector<int> vals, int sz) {
    int ret = 0;
    if (sz == 1) return ret;
    std::vector<int> new_arg;

    for (int i = 0; i < sz; i += 2) {
        int sum = vals[i] + vals[i + 1];
        if (sum >= 10) ret ++;
        new_arg.push_back(sum % 10);
    }
    return ret + foo(new_arg, sz / 2);
}

int main(void) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int t; scanf("%d", &t);
        vals.push_back(t);
    }
    int q; scanf("%d", &q);
    while (q --) {
        int l, r; scanf("%d %d", &l, &r);
        std::vector<int> arg;
        l --; r --;
        for (int i = l; i <= r; ++i) {
            arg.push_back(vals[i]);
        }

        printf("%d\n", foo(arg, r - l + 1));
    }
    return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }

    int c;cin>>c;
    for(int i=0;i<c;i++){
        int n,m;cin>>n>>m;
        int counter=0;
        int sum=0;

            for(int j=n-1;j<m-1;j+=2){

                if((v1[j]+v1[j+1])>=10){counter++;}
                sum+=((v1[j]+v1[j+1])%10);
                if(sum>=10){counter++;}
                sum=sum%10;
            }
        printf("%d\n",counter);
    }
    return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }

    int c;cin>>c;
    for(int i=0;i<c;i++){
        int n,m;cin>>n>>m;
        int counter=0;
        int sum=0;

            for(int j=n-1;j<m-1;j+=2){
                int k=v1[j]+v1[j+1];
                if(k>=10){counter++;}
                sum+=k%10;
                if(sum>=10){counter++;}
                sum=sum%10;
            }
        printf("%d\n",counter);
    }
    return 0;
}


#include <bits/stdc++.h>

int main()
{
    using namespace std;
    int a;cin>>a;string s;
    while(a--){
        int b;cin>>b;
        s+=to_string(b);
    }
    vector<vector<int>> v;
    int c;cin>>c;vector<int> l;
    while(c--){
        int q,w;cin>>q>>w;

        l.push_back(q);l.push_back(w);
        v.push_back(l);
        l.clear();
    }
    for(int i=0;i<v.size();i++){
        //cout<<v[i][0]<<' '<<v[i][1]<<'\n';
        string l = s.substr(v[i][0]-1,v[i][1]-v[i][0]+1);
        //cout<<l<<'\n';
        int counter=0;
        while(l.length()>1){
            int i=0;string j;

            while(i<l.length()){
                int k=l[i]-'0'+l[i+1]-'0';
                if(k>=10){counter++;}
                j+=to_string(k%10);
                i+=2;
            }
            l=j;
        }
        if(l[0]-'0'>=10){counter++;}
        cout<<counter<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>

int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }
    vector<vector<int>> v;
    int c;cin>>c;vector<int> l;
    while(c--){
        int q,w;cin>>q>>w;

        l.push_back(q);l.push_back(w);
        v.push_back(l);
        l.clear();
    }
    for(int i=0;i<v.size();i++){
        //cout<<v[i][0]<<' '<<v[i][1]<<'\n';

        //cout<<l<<'\n';
        vector<int> v2=vector<int>(v1.begin()+v[i][0]-1,v1.begin()+v[i][1]);
        int counter=0;

            int j=0;int sum=0;

            while(j<v2.size()){
                int k=v2[j]+v2[j+1];
                if(k>=10){counter++;}
                sum+=k%10;
                if(sum>=10){counter++;}
                sum=sum%10;
                j+=2;
            }

        cout<<counter<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }

    int c;cin>>c;
    for(int i=0;i<c;i++){
        int n,m;cin>>n>>m;
        int counter=0;
        int sum=0;

            for(int j=n-1;j<m-1;j+=2){
                sum+=v1[j]+v1[j+1];

            }
        counter=(sum/10);
        printf("%d\n",counter);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {

    int n;
    cin>>n;
    vector<int> a(n);
    for (int i = 0; i<n; i++) cin>>a[i];

    vector<int> b(n+1);
    for (int i = 1; i<=n; i++) b[i] = b[i-1] + a[i-1];
    int q;
    cin>>q;
    int l, r;
    for (int i = 0; i<q; i++)
    {
        cin>>l>>r; cout<<(b[r]-b[l-1])/10<<endl;
    }
}


#include <bits/stdc++.h>
#include <cstdlib>
int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    int b=a;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }

    int c;cin>>c;
    vector<int> v(b);
    v.push_back(v1[0]);
    for(int i=1;i<v1.size();i++){
        v[i]=v[i-1]+v1[i];
    }

    for(int i=0;i<c;i++){
        int n,m;cin>>n>>m;
        int counter = (v[m-1]-v[n-1])/10;
        printf("%d\n",counter);
    }
    return 0;
}


#include <bits/stdc++.h>

int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }
    vector<vector<int>> v;
    int c;cin>>c;

    for(int i=0;i<c;i++){
        //cout<<v[i][0]<<' '<<v[i][1]<<'\n';

        //cout<<l<<'\n';
        int n,m;cin>>n>>m;
        vector<int> v2=vector<int>(v1.begin()+n-1,v1.begin()+m);
        int counter=0;

            int j=0;int sum=0;

            while(j<v2.size()-1){
                int k=v2[j]+v2[j+1];
                if(k>=10){counter++;}
                sum+=k%10;
                if(sum>=10){counter++;}
                sum=sum%10;
                j+=2;
            }

        cout<<counter<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    int b=a;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }

    int c;cin>>c;
    vector<int> v(b+1);
    v[0]=0;
    for(int i=1;i<b+1;i++){
        v[i]=v[i-1]+v1[i-1];
    }

    for(int i=0;i<c;i++){
        int n,m;cin>>n>>m;
        int counter = (v[m]-v[n-1])/10;
        printf("%d\n",counter);
    }
    return 0;
}


#include <bits/stdc++.h>

int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }
    vector<vector<int>> v;
    int c;cin>>c;vector<int> l;
    while(c--){
        int q,w;cin>>q>>w;

        l.push_back(q);l.push_back(w);
        v.push_back(l);
        l.clear();
    }
    for(int i=0;i<v.size();i++){
        //cout<<v[i][0]<<' '<<v[i][1]<<'\n';

        //cout<<l<<'\n';
        vector<int> v2=vector<int>(v1.begin()+v[i][0]-1,v1.begin()+v[i][1]);
        int counter=0;

            int j=0;int sum=0;

            while(j<v2.size()-1){
                int k=v2[j]+v2[j+1];
                if(k>=10){counter++;}
                sum+=k%10;
                if(sum>=10){counter++;}
                sum=sum%10;
                j+=2;
            }

        cout<<counter<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }
    vector<vector<int>> v;
    int c;cin>>c;

    for(int i=0;i<c;i++){
        //cout<<v[i][0]<<' '<<v[i][1]<<'\n';

        //cout<<l<<'\n';
        int n,m;cin>>n>>m;
        vector<int> v2=vector<int>(v1.begin()+n-1,v1.begin()+m);
        int counter=0;

            int j=0;int sum=0;

            while(j<v2.size()-1){
                int k=v2[j]+v2[j+1];
                if(k>=10){counter++;}
                sum+=k%10;
                if(sum>=10){counter++;}
                sum=sum%10;
                j+=2;
            }

        printf("%d\n",counter);
    }
    return 0;
}


#include <bits/stdc++.h>

int main()
{
    using namespace std;
    int a;cin>>a;vector<int> v1;
    while(a--){
        int b;cin>>b;
        v1.push_back(b);
    }
    vector<vector<int>> v;
    int c;cin>>c;vector<int> l;
    while(c--){
        int q,w;cin>>q>>w;

        l.push_back(q);l.push_back(w);
        v.push_back(l);
        l.clear();
    }
    for(int i=0;i<v.size();i++){
        //cout<<v[i][0]<<' '<<v[i][1]<<'\n';

        //cout<<l<<'\n';
        vector<int> v2=vector<int>(v1.begin()+v[i][0]-1,v1.begin()+v[i][1]);
        int counter=0;
        while(v2.size()>1){
            int i=0;vector<int> j;

            while(i<v2.size()){
                int k=v2[i]+v2[i+1];
                if(k>=10){counter++;}
                j.push_back(k%10);
                i+=2;
            }
            v2=j;
        }
        if(v2[0]>=10){counter++;}
        cout<<counter<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long int ll;
const int N=1e5+5;
int n,l,r,q;
ll a[N];
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        a[i]+=a[i-1];
    }
    cin>>q;
    while(q--){
        cin>>l>>r;
        cout<<(a[r]-a[l-1])/10<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long int ll;
const int N=1e5+5;
int n,a[N],q,l,r,c,c2[N],x,s,b[N],tmp;
map < pair<int,int>, int> mp;
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++){
        x=2;
        while(i+x-1<n){
            tmp=0;
            s=x;
            for(int j=i,ind=0;j<i+x;j++,ind++)
                b[ind]=a[j];
            while(s>1){
                for(int j=0;j<s;j+=2){
                    if(b[j]+b[j+1]>9) tmp++;
                    b[j/2]=(b[j]+b[j+1])%10;
                }
                s/=2;
            }
            mp[make_pair(i,i+x-1)]=tmp;
            x*=2;
        }
    }
    cin>>q;
    while(q--){
        cin>>l>>r;
        l--; r--;
        cout<<mp[make_pair(l,r)]<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N],q,l,r,s,b[N],c[N],c2[N];
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++){
        for(int j=i,in=0;j<n;j++,in++)
            b[in]=a[j];
        s=n-i;
        while(s>1){
            for(int j=0;j<s;j+=2){
                if(b[j]+b[j+1]>9)
                    c[i]++;
                b[j/2]=((b[j]+b[j+1])%10);
            }
            if((s%2)) s++;
            s/=2;
        }
    }
    cin>>q;
    while(q--){
        cin>>l>>r;
        l--; r--;
        //cout<<c[l]<<" "<<c[r]<<'\n';
       cout<<max(0,c[l]-c[r])<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long int ll;
const int N=1e5+5;
int n,a[N],q,l,r,c,c2[N],s,b[N];
map <int,bool> mp;
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=2;i<N;i*=2) mp[i]=1;
    for(int i=0;i<n;i++){
        for(int j=i,in=0;j<n;j++,in++)
            b[in]=a[j];
        s=n-i;
        while(!mp[s]&&s>1) s--;
        while(s>1){
            for(int j=0;j<s;j+=2){
                if(b[j]+b[j+1]>9)
                    c2[i]++;
                b[j/2]=((b[j]+b[j+1])%10);
            }
            s/=2;
        }
    }
    cin>>q;
    while(q--){
        cin>>l>>r;
        l--; r--; c=0; s=r-l+1;
        if(c2[l]-c2[r]<0){
            for(int i=l,in=0;i<=r;i++,in++)
                b[in]=a[i];
               while(s>1){
                 for(int i=0;i<s;i+=2){
                    if(b[i]+b[i+1]>=10) c++;
                    b[i/2]=( (b[i]+b[i+1])%10);
                 }
                s/=2;
            }

        }
        else c= c2[l]-c2[r];
        cout<<c<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long int ll;
const int N=1e5+5;
int n,a[N],q,l,r,c,s,b[N];
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    cin>>q;
    while(q--){
        cin>>l>>r;
        l--; r--; c=0; s=r-l+1;
        for(int i=l,in=0;i<=r;i++,in++)
            b[in]=a[i];
        while(s>1){
            for(int i=0;i<s;i+=2){
                if(b[i]+b[i+1]>=10) c++;
                b[i/2]=( (b[i]+b[i+1])%10);
            }
            s/=2;
        }
        cout<<c<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N],q,l,r,s,b[N],c[N],c2[N];
map<int,bool> mp;
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=2;i<N;i*=2) mp[i]=1;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++){
        for(int j=i,in=0;j<n;j++,in++)
            b[in]=a[j];
        s=n-i;
        while(!mp[s]&&s>1) s--;
        while(s>1){
            for(int j=0;j<s;j+=2){
                if(b[j]+b[j+1]>9)
                    c[i]++;
                b[j/2]=((b[j]+b[j+1])%10);
            }
            s/=2;
        }
    }
    cin>>q;
    while(q--){
        cin>>l>>r;
        l--; r--;
       //cout<<c[l]<<" "<<c[r]<<'\n';
       cout<<abs(c[l]-c[r])<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 500006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 1000000007
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
int n,m,a[maxn];
int tab[20][maxn],val[20][maxn],mm[maxn];
void initRMQ(int n,int b[]){
    mm[0]=-1;
    inc(i,1,n){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        tab[0][i]=b[i];
    }
    inc(j,1,mm[n])for(int i=1;i+(1<<j)-1<=n;i++){
        tab[j][i]=(tab[j-1][i]+tab[j-1][i+(1<<(j-1))])%10;
        val[j][i]=val[j-1][i]+val[j-1][i+(1<<(j-1))]+(tab[j-1][i]+tab[j-1][i+(1<<(j-1))]>=10);
    }
}
int main()
{
    n=read();
    inc(i,1,n)a[i]=read();
    initRMQ(n,a);
    m=read();
    int x,y;
    inc(i,1,m){
        x=read();y=read();
        printf("%d\n",val[mm[y-x+1]][x]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 65540
#define maxm 500006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 1000000007
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
int n,m,a[maxn];
int tab[20][maxn],val[20][maxn],mm[maxn];
void initRMQ(int n,int b[]){
    mm[0]=-1;
    inc(i,1,n){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        tab[0][i]=b[i];
    }
    inc(j,1,mm[n])for(int i=1;i+(1<<j)-1<=n;i++){
        tab[j][i]=(tab[j-1][i]+tab[j-1][i+(1<<(j-1))])%10;
        val[j][i]=val[j-1][i]+val[j-1][i+(1<<(j-1))]+(tab[j-1][i]+tab[j-1][i+(1<<(j-1))]>=10);
    }
}
int main()
{
    n=read();
    inc(i,1,n)a[i]=read();
    initRMQ(n,a);
    m=read();
    int x,y;
    inc(i,1,m){
        x=read();y=read();
        printf("%d\n",val[mm[y-x+1]][x]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 65540
#define maxm 500006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 1000000007
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
int n,m,a[maxn];
int tab[17][maxn],val[17][maxn],mm[maxn];
void initRMQ(int n,int b[]){
    mm[0]=-1;
    inc(i,1,n){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        tab[0][i]=b[i];
    }
    inc(j,1,mm[n])for(int i=1;i+(1<<j)-1<=n;i++){
        tab[j][i]=(tab[j-1][i]+tab[j-1][i+(1<<(j-1))])%10;
        val[j][i]=val[j-1][i]+val[j-1][i+(1<<(j-1))]+(tab[j-1][i]+tab[j-1][i+(1<<(j-1))]>=10);
    }
}
int main()
{
    n=read();
    inc(i,1,n)a[i]=read();
    initRMQ(n,a);
    m=read();
    int x,y;
    inc(i,1,m){
        x=read();y=read();
        printf("%d\n",val[mm[y-x+1]][x]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 500006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 1000000007
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
int n,m,a[maxn];
int t1[maxn<<2],t2[maxn<<2],sum1[maxn<<2],sum2[maxn<<2];
void build1(int x,int l,int r){
    if(l==r){t1[x]=a[l];sum1[x]=0;return ;}
    int mid=(l+r)>>1;
    build1(x<<1,l,mid);build1(x<<1|1,mid+1,r);
    sum1[x]=sum1[x<<1]+sum1[x<<1|1]+((t1[x<<1]+t1[x<<1|1])>=10);
    t1[x]=(t1[x<<1]+t1[x<<1|1])%10;
}
void build2(int x,int l,int r){
    if(l==r){t2[x]=a[l];sum2[x]=0;return ;}
    int mid=(l+r)>>1;
    build2(x<<1,l,mid);build2(x<<1|1,mid+1,r);
    sum2[x]=sum2[x<<1]+sum2[x<<1|1]+((t2[x<<1]+t2[x<<1|1])>=10);
    t2[x]=(t2[x<<1]+t2[x<<1|1])%10;
}
int query1(int x,int l,int r,int lp,int rp){
    if(lp<=l&&r<=rp)return sum1[x];
    int mid=(l+r)>>1,res=0;
    if(lp<=mid)res+=query1(x<<1,l,mid,lp,rp);
    if(mid+1<=rp)res+=query1(x<<1|1,mid+1,r,lp,rp);
    return res;
}
int query2(int x,int l,int r,int lp,int rp){
    if(lp<=l&&r<=rp)return sum2[x];
    int mid=(l+r)>>1,res=0;
    if(lp<=mid)res+=query2(x<<1,l,mid,lp,rp);
    if(mid+1<=rp)res+=query2(x<<1|1,mid+1,r,lp,rp);
    return res;
}
int main()
{
    n=read();
    inc(i,1,n)a[i]=read();
    build1(1,1,n);build2(1,2,n);
    m=read();
    int x,y;
    inc(i,1,m){
        x=read();y=read();
        if(x%2==1)printf("%d\n",query1(1,1,n,x,y));
        else printf("%d\n",query2(1,2,n,x,y));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 65540
#define maxm 500006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 1000000007
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
int n,m,a[maxn];
int t1[maxn<<2],t2[maxn<<2],sum1[maxn<<2],sum2[maxn<<2];
void build1(int x,int l,int r){
    if(l==r){t1[x]=a[l];sum1[x]=0;return ;}
    int mid=(l+r)>>1;
    build1(x<<1,l,mid);build1(x<<1|1,mid+1,r);
    sum1[x]=sum1[x<<1]+sum1[x<<1|1]+((t1[x<<1]+t1[x<<1|1])>=10);
    t1[x]=(t1[x<<1]+t1[x<<1|1])%10;
}
void build2(int x,int l,int r){
    if(l==r){t2[x]=a[l];sum2[x]=0;return ;}
    int mid=(l+r)>>1;
    build2(x<<1,l,mid);build2(x<<1|1,mid+1,r);
    sum2[x]=sum2[x<<1]+sum2[x<<1|1]+((t2[x<<1]+t2[x<<1|1])>=10);
    t2[x]=(t2[x<<1]+t2[x<<1|1])%10;
}
int query1(int x,int l,int r,int lp,int rp){
    if(lp<=l&&r<=rp)return sum1[x];
    int mid=(l+r)>>1,res=0;
    if(lp<=mid)res+=query1(x<<1,l,mid,lp,rp);
    if(mid+1<=rp)res+=query1(x<<1|1,mid+1,r,lp,rp);
    return res;
}
int query2(int x,int l,int r,int lp,int rp){
    if(lp<=l&&r<=rp)return sum2[x];
    int mid=(l+r)>>1,res=0;
    if(lp<=mid)res+=query2(x<<1,l,mid,lp,rp);
    if(mid+1<=rp)res+=query2(x<<1|1,mid+1,r,lp,rp);
    return res;
}
int main()
{
    n=read();
    inc(i,1,n)a[i]=read();
    build1(1,1,n);
    if(n>1)build2(1,2,n);
    m=read();
    int x,y;
    inc(i,1,m){
        x=read();y=read();
        if(x%2==1)printf("%d\n",query1(1,1,n,x,y));
        else printf("%d\n",query2(1,2,n,x,y));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 65540
#define maxm 500006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 1000000007
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
int n,m,a[maxn];
int t1[maxn<<2],t2[maxn<<2],sum1[maxn<<2],sum2[maxn<<2];
void build1(int x,int l,int r){
    if(l==r){t1[x]=a[l];sum1[x]=0;return ;}
    int mid=(l+r)>>1;
    build1(x<<1,l,mid);build1(x<<1|1,mid+1,r);
    sum1[x]=sum1[x<<1]+sum1[x<<1|1]+((t1[x<<1]+t1[x<<1|1])>=10);
    t1[x]=(t1[x<<1]+t1[x<<1|1])%10;
}
void build2(int x,int l,int r){
    if(l==r){t2[x]=a[l];sum2[x]=0;return ;}
    int mid=(l+r)>>1;
    build2(x<<1,l,mid);build2(x<<1|1,mid+1,r);
    sum2[x]=sum2[x<<1]+sum2[x<<1|1]+((t2[x<<1]+t2[x<<1|1])>=10);
    t2[x]=(t2[x<<1]+t2[x<<1|1])%10;
}
int query1(int x,int l,int r,int lp,int rp){
    if(lp<=l&&r<=rp)return sum1[x];
    int mid=(l+r)>>1,res=0;
    if(lp<=mid)res+=query1(x<<1,l,mid,lp,rp);
    if(mid+1<=rp)res+=query1(x<<1|1,mid+1,r,lp,rp);
    return res;
}
int query2(int x,int l,int r,int lp,int rp){
    if(lp<=l&&r<=rp)return sum2[x];
    int mid=(l+r)>>1,res=0;
    if(lp<=mid)res+=query2(x<<1,l,mid,lp,rp);
    if(mid+1<=rp)res+=query2(x<<1|1,mid+1,r,lp,rp);
    return res;
}
int main()
{
    n=read();
    inc(i,1,n)a[i]=read();
    build1(1,1,n);build2(1,2,n);
    m=read();
    int x,y;
    inc(i,1,m){
        x=read();y=read();
        if(x%2==1)printf("%d\n",query1(1,1,n,x,y));
        else printf("%d\n",query2(1,2,n,x,y));
    }
    return 0;
}


#include<bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
#define pb push_back
using namespace std;
const int MAXN=1e5;
typedef long long ll;
typedef pair<ll,ll> pii;
ll n, d, q, a, b, l, r, c, f;
ll p[50];
vector<pii> v[MAXN+5];
int main()
{
	p[0]=1;
	for (ll i=1;i<20;i++) p[i]=p[i-1]*2;
	cin >> n;
	for (ll i=0;i<n;i++)
	{
		cin >> d;
		v[0].pb(MP(d,0));
	}
	for (ll i=1;i<n;i++)
	{
		for (ll j=0;j<n-i;j++)
		{
			a=(v[i-1][j].F+v[i-1][j+1].F)%10;
			b=(v[i-1][j].F+v[i-1][j+1].F)/10+v[i-1][j].S+v[i-1][j+1].S;
			v[i].pb(MP(a, b));
		}
	}
	cin >> q;
	while (q--)
	{
		cin >> l >> r;
		--l,--r;
		c=r-l+1;
		f=lower_bound(p, p+20, c)-p;
		cout << v[f][l].S << "\n";
	}
}


#include<bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
#define pb push_back
using namespace std;
const int MAXN=1e5;
typedef long long ll;
typedef pair<int,int> pii;
int n, d, q, a, b, l, r, c, f;
int p[50];
vector<pii> v[MAXN+5];
int main()
{
	p[0]=1;
	for (int i=1;i<20;i++) p[i]=p[i-1]*2;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> d;
		v[0].pb(MP(d,0));
	}
	for (int i=1;i<n;i++)
	{
		for (int j=0;j<n-i;j++)
		{
			a=(v[i-1][j].F+v[i-1][j+1].F)%10;
			b=(v[i-1][j].F+v[i-1][j+1].F)/10+v[i-1][j].S+v[i-1][j+1].S;
			v[i].pb(MP(a, b));
		}
	}
	cin >> q;
	while (q--)
	{
		cin >> l >> r;
		--l,--r;
		c=r-l+1;
		f=lower_bound(p, p+20, c)-p;
		cout << v[f][l].S << "\n";
	}
}


#include<bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
#define pb push_back
using namespace std;
const ll MAXN=1e5;
typedef long long ll;
typedef pair<ll,ll> pii;
ll n, d, q, a, b, l, r, c, f;
ll p[50];
vector<pii> v[MAXN+5];
int main()
{
	p[0]=1;
	for (ll i=1;i<20;i++) p[i]=p[i-1]*2;
	cin >> n;
	for (ll i=0;i<n;i++)
	{
		cin >> d;
		v[0].pb(MP(d,0));
	}
	for (ll i=1;i<n;i++)
	{
		for (ll j=0;j<n-i;j++)
		{
			a=(v[i-1][j].F+v[i-1][j+1].F)%10;
			b=(v[i-1][j].F+v[i-1][j+1].F)/10+v[i-1][j].S+v[i-1][j+1].S;
			v[i].pb(MP(a, b));
		}
	}
	cin >> q;
	while (q--)
	{
		cin >> l >> r;
		--l,--r;
		c=r-l+1;
		f=lower_bound(p, p+20, c)-p;
		cout << v[f][l].S << "\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5;
int n, d, q, l, r;
int a[MAXN+5];
int main()
{
	cin >> n;
	for (int i=1;i<=n;i++)
	{
		cin >> d;
		a[i]=a[i-1]+d;
	}
	cin >> q;
	while (q--)
	{
		cin >> l >> r;
		cout << (a[r]-a[l-1])/10 << "\n";
	}
}


#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{

int t,n;
cin >> n;
vector <int> a;
for (int i=0;i<n;i++)
{
cin >> t; a.push_back(t);
}

int q;  //queries
cin >> q;
vector <int> q1;
vector <int> q2;
for (int i=0;i<q;i++)
{
cin >> t; q1.push_back(t-1);
cin >> t; q2.push_back(t-1);
}

vector <int> ans;

vector <long long int > dp;
dp.assign(n,0);
long long int sum=0;
for (int i=0;i<n;i++)
{
    sum+=a[i];
    dp[i]=sum;
}

for (int i=0;i<q;i++)
{
    long long int dif = dp[q2[i]] - dp[q1[i]];
    ans.push_back( ( (int)dif + a[q1[i]] )/10 );
}

for (int i=0;i<ans.size();i++)
cout << ans[i] << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{

int t,n;
cin >> n;
vector <int> a;
for (int i=0;i<n;i++)
{
cin >> t; a.push_back(t);
}

int q;  //queries
cin >> q;
vector <int> q1;
vector <int> q2;
for (int i=0;i<q;i++)
{
cin >> t; q1.push_back(t-1);
cin >> t; q2.push_back(t-1);
}

vector <int> ans;

vector <vector <int> > dp;
dp.assign(n,vector <int> (n,0) );

for (int i=0;i<n;i++)
{
    int sum=0;
    for (int j=i;j<n;j++)
    {
        sum+=a[j];
        dp[i][j]=sum/10;
    }
}

for (int i=0;i<q;i++)
{
    ans.push_back(dp[ q1[i] ][q2[i]]  );
}

dp.clear();

for (int i=0;i<ans.size();i++)
cout << ans[i] << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{

int t,n;
cin >> n;
vector <int> a;
for (int i=0;i<n;i++)
{
cin >> t; a.push_back(t);
}

int q;  //queries
cin >> q;
vector <int> q1;
vector <int> q2;
for (int i=0;i<q;i++)
{
cin >> t; q1.push_back(t-1);
cin >> t; q2.push_back(t-1);
}

vector <int> ans;

for (int i=0;i<q;i++)
{
    int s=q1[i];
    int sum=0;

    while (s<=q2[i])
    {
        sum+=a[s];  s++;
    }
    ans.push_back(sum/10);
}

for (int i=0;i<ans.size();i++)
cout << ans[i] << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{

int t,n;
cin >> n;
vector <int> a;
for (int i=0;i<n;i++)
{
cin >> t; a.push_back(t);
}

int q;  //queries
cin >> q;
vector <int> q1;
vector <int> q2;
for (int i=0;i<q;i++)
{
cin >> t; q1.push_back(t-1);
cin >> t; q2.push_back(t-1);
}

vector <int> ans;

vector <vector <int> > dp;
dp.assign(n,vector <int> (n,0) );

for (int i=0;i<n;i++)
{
    int sum=0;
    for (int j=i;j<n;j++)
    {
        sum+=a[j];
        dp[i][j]=sum/10;
    }
}

for (int i=0;i<q;i++)
{
    ans.push_back(dp[ q1[i] ][q2[i]]  );
}

for (int i=0;i<ans.size();i++)
cout << ans[i] << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{

int t,n;
cin >> n;
vector <int> a;
for (int i=0;i<n;i++)
{
cin >> t; a.push_back(t);
}

int q;  //queries
cin >> q;
vector <int> q1;
vector <int> q2;
for (int i=0;i<q;i++)
{
cin >> t; q1.push_back(t-1);
cin >> t; q2.push_back(t-1);
}

vector <int> ans;

vector <int > dp;
dp.assign(n,0);
int sum=0;
for (int i=0;i<n;i++)
{
    sum+=a[i];
    dp[i]=sum/10;
}

for (int i=0;i<q;i++)
{
    ans.push_back( dp[q2[i]] -dp[q1[i]] );
}

for (int i=0;i<ans.size();i++)
cout << ans[i] << endl;

    return 0;
}


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

const int N=2e5+10;
int n,q;
int a[N],l,r;
int sum[N][20];
int ans[N][20];
map<int,int>powe;

int main(){
	for(int i=0;i<=18;i++) powe[1<<i]=i;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) sum[i][0]=a[i];
	for(int i=1;i<=18;i++){
		int tmp=1<<(i-1);
		for(int j=1;j<=n;j++){
			sum[j][i]=sum[j][i-1]+sum[j+tmp][i-1];
			ans[j][i]=ans[j][i-1]+ans[j+tmp][i-1];
			if(sum[j][i]>10) ans[j][i]++,sum[j][i]%=10;
		}
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d %d",&l,&r);
		int tmp=r-l+1;
		int cnt=powe[tmp];
		printf("%d\n",ans[l][cnt]);
	}
	return 0;
}


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

const int N=3e5+10;
int n,q;
int a[N],l,r;
int sum[N][21];
int ans[N][21];
map<int,int>powe;

int main(){
	for(int i=0;i<=18;i++) powe[1<<i]=i;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) sum[i][0]=a[i];
	for(int i=1;i<=18;i++){
		int tmp=1<<(i-1);
		for(int j=1;j<=n;j++){
			sum[j][i]=sum[j][i-1]+sum[j+tmp][i-1];
			ans[j][i]=ans[j][i-1]+ans[j+tmp][i-1];
			if(sum[j][i]>=10) ans[j][i]++,sum[j][i]%=10;
		}
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d %d",&l,&r);
		int tmp=r-l+1;
		int cnt=powe[tmp];
		printf("%d\n",ans[l][cnt]);
	}
	return 0;
}


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

const int N=1e5+10;
int n,q;
int a[N],l,r;
int sum[N][20];
int ans[N][20];
map<int,int>powe;

int main(){
	for(int i=0;i<=20;i++){
		powe[1<<i]=i;
	}
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) sum[i][0]=a[i];
	for(int i=1;i<=20;i++){
		int tmp=1<<(i-1);
		for(int j=1;j<=n;j++){
			if(j+i>n) break;
			sum[j][i]=sum[j][i-1]+sum[j+tmp][i-1];
			ans[j][i]=ans[j][i-1]+ans[j+tmp][i-1];
			if(sum[j][i]>10) ans[j][i]++,sum[j][i]%=10;
		}
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d %d",&l,&r);
		int tmp=r-l+1;
		int cnt=powe[tmp];
		printf("%d\n",ans[l][cnt]);
	}
	return 0;
}


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

const int N=1e5+10;
int n,q;
int a[N],l,r;
int sum[N][20];
int ans[N][20];
map<int,int>powe;

int main(){
	for(int i=0;i<=20;i++) powe[1<<i]=i;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) sum[i][0]=a[i];
	for(int i=1;i<=20;i++){
		int tmp=1<<(i-1);
		for(int j=1;j<=n;j++){
			if(j+tmp*2>n) break;
			sum[j][i]=sum[j][i-1]+sum[j+tmp][i-1];
			ans[j][i]=ans[j][i-1]+ans[j+tmp][i-1];
			if(sum[j][i]>10) ans[j][i]++,sum[j][i]%=10;
		}
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d %d",&l,&r);
		int tmp=r-l+1;
		int cnt=powe[tmp];
		printf("%d\n",ans[l][cnt]);
	}
	return 0;
}


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

const int N=1e5+10;
int n,q;
int a[N],l,r;
int sum[N][20];
int ans[N][20];
map<int,int>powe;

int main(){
	for(int i=0;i<=20;i++) powe[1<<i]=i;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) sum[i][0]=a[i];
	for(int i=1;i<=20;i++){
		int tmp=1<<(i-1);
		if(n+tmp>=N-1000) break;
		for(int j=1;j<=n;j++){
			if(tmp>n) break;
			sum[j][i]=sum[j][i-1]+sum[j+tmp][i-1];
			ans[j][i]=ans[j][i-1]+ans[j+tmp][i-1];
			if(sum[j][i]>10) ans[j][i]++,sum[j][i]%=10;
		}
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d %d",&l,&r);
		int tmp=r-l+1;
		int cnt=powe[tmp];
		printf("%d\n",ans[l][cnt]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int fun(int l,int r,queue<int> &vec)
{
    int candy = 0;
    if(l == r) return 0;
    int t1,t2;
    //cout << vec.size();
    while(vec.size()!=1)
    {
        t1 = vec.front();
        vec.pop();
        t2 = vec.front();
        vec.pop();
        int sum = t1 + t2;
        if(sum >= 10){
            candy++;
            sum = sum % 10;
        }
        vec.push(sum);
    }
    return candy;
}
int main()
{
    int len;
    cin >> len;
    vector<int> arr;
    //queue<int> arr;
    for (int i = 0; i < len; i++)
    {
        int tmp;
        cin >> tmp;
        arr.push_back(tmp);
    }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr[i];
    // }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr2[i];
    // }
    int t;
    cin >> t;
    while(t--)
    {
        //queue<int> arr2;
        int l=0,r=0;
        cin >> l >> r;
        // for(int i = l-1; i <= r-1; i++)
        // {
        //     arr2.push(arr[i]);
        // }
        // cout<<fun(l,r,arr2)<<endl;
        // while(arr2.size()!=0){
        //     arr2.pop();
        // }
        int sum = 0;
        for (int i = l-1; i <= r - 1; i++)
        {
            sum = sum + arr[i];
        }
        cout << sum/10<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int fun(int l,int r,queue<int> &vec)
{
    int candy = 0;
    if(l == r) return 0;
    int t1,t2;
    //cout << vec.size();
    while(vec.size()!=1)
    {
        t1 = vec.front();
        vec.pop();
        t2 = vec.front();
        vec.pop();
        int sum = t1 + t2;
        if(sum > 10){
            candy++;
            sum = sum % 10;
        }
        vec.push(sum);
    }
    return candy;
}
int main()
{
    int len;
    cin >> len;
    vector<int> arr;
    //queue<int> arr;
    for (int i = 0; i < len; i++)
    {
        int tmp;
        cin >> tmp;
        arr.push_back(tmp);
    }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr[i];
    // }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr2[i];
    // }
    int t;
    cin >> t;
    while(t--)
    {
        queue<int> arr2;
        int l=0,r=0;
        cin >> l >> r;
        for(int i = l-1; i <= r-1; i++)
        {
            arr2.push(arr[i]);
        }
        cout<<fun(l,r,arr2)<<endl;
        while(arr2.size()!=0){
            arr2.pop();
        }

    }
}


#include<bits/stdc++.h>
using namespace std;
int fun(int l,int r,queue<int> &vec)
{
    int candy = 0;
    if(l == r) return 0;
    int t1,t2;
    //cout << vec.size();
    while(vec.size()!=1)
    {
        t1 = vec.front();
        vec.pop();
        t2 = vec.front();
        vec.pop();
        int sum = t1 + t2;
        if(sum >= 10){
            candy++;
            sum = sum % 10;
        }
        vec.push(sum);
    }
    return candy;
}
int main()
{
    int len;
    cin >> len;
    vector<int> arr;
    arr.push_back(0);
    //queue<int> arr;
    for (int i = 0; i < len; i++)
    {
        int tmp;
        cin >> tmp;
        arr.push_back(tmp+arr[i]);
    }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr[i];
    // }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr2[i];
    // }
    int t;
    cin >> t;
    while(t--)
    {
        //queue<int> arr2;
        int l=0,r=0;
        cin >> l >> r;
        // for(int i = l-1; i <= r-1; i++)
        // {
        //     arr2.push(arr[i]);
        // }
        // cout<<fun(l,r,arr2)<<endl;
        // while(arr2.size()!=0){
        //     arr2.pop();
        // }
        cout << (arr[r]-arr[l-1])/10<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int fun(int l,int r,queue<int> &vec)
{
    int candy = 0;
    if(l == r) return 0;
    int t1,t2;
    //cout << vec.size();
    while(vec.size()!=1)
    {
        t1 = vec.front();
        vec.pop();
        t2 = vec.front();
        vec.pop();
        int sum = t1 + t2;
        if(sum >= 10){
            candy++;
            sum = sum % 10;
        }
        vec.push(sum);
    }
    return candy;
}
int main()
{
    int len;
    cin >> len;
    vector<int> arr;
    //queue<int> arr;
    for (int i = 0; i < len; i++)
    {
        int tmp;
        cin >> tmp;
        arr.push_back(tmp);
    }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr[i];
    // }
    // for (int i = 0; i < len; i++)
    // {
    //     cout << arr2[i];
    // }
    int t;
    cin >> t;
    while(t--)
    {
        queue<int> arr2;
        int l=0,r=0;
        cin >> l >> r;
        for(int i = l-1; i <= r-1; i++)
        {
            arr2.push(arr[i]);
        }
        cout<<fun(l,r,arr2)<<endl;
        while(arr2.size()!=0){
            arr2.pop();
        }

    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],f[110000],sum[110000],st,ed;
int work(int x,int y)
{
    if(x==y) return 0;
    if(x+1==y)
    {
        if(a[x]+a[y]>=10) return 1;return 0;
    }
    int mid=(y+x)/2;
    int fx=work(x,mid),fy=work(mid+1,y);
    if((sum[mid]-sum[x-1])%10+(sum[y]-sum[mid])%10>=10)
        return fx+fy+1;
    else return fx+fy;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    cin>>m;
    while(m--)
    {
        cin>>st>>ed;
        cout<<work(st,ed)<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],ans=0,sum[100100],res[25],cnt=0;
int st,ed;
struct node
{
    int sum,num;
}t[400010];
struct hehe
{
    int sum,num;
}qaq[400010];
void build(int x,int y,int root)
{
    if(x==y)
    {
        t[root].sum=0;
        t[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build(x,mid,root*2);
    build(mid+1,y,root*2+1);
    if(t[root*2].num+t[root*2+1].num>=10) t[root].sum=t[root*2].sum+t[root*2+1].sum+1;
    else t[root].sum=t[root*2].sum+t[root*2+1].sum;
    t[root].num=(t[root*2].num+t[root*2+1].num)%10;
}
void build2(int x,int y,int root)
{
    if(x==y)
    {
        qaq[root].sum=0;
        qaq[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build2(x,mid,root*2);
    build2(mid+1,y,root*2+1);
    if(qaq[root*2].num+qaq[root*2+1].num>=10) qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum+1;
    else qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum;
    qaq[root].num=(qaq[root*2].num+qaq[root*2+1].num)%10;
}
int find(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return t[root].sum;
    int mid=(x+y)/2;
    int fx=find(x,mid,root*2),fy=find(mid+1,y,root*2+1);
    return fx+fy;
}
int find2(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return qaq[root].sum;
    int mid=(x+y)/2;
    int fx=find2(x,mid,root*2),fy=find2(mid+1,y,root*2+1);
    return fx+fy;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    build(1,n,1);
    build2(2,n-1,1);
    cin>>m;
    while(m--)
    {
        cin>>st>>ed;
        if(st%2==1) cout<<find(1,n,1)<<endl;
        else        cout<<find2(2,n-1,1)<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],ans=0,cnt=0;
int st,ed;
struct node
{
    int sum,num;
}t[400010];
struct hehe
{
    int sum,num;
}qaq[400010];
void build(int x,int y,int root)
{
    if(x==y)
    {
        t[root].sum=0;
        t[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build(x,mid,root*2);
    build(mid+1,y,root*2+1);
    if(t[root*2].num+t[root*2+1].num>=10) t[root].sum=t[root*2].sum+t[root*2+1].sum+1;
    else t[root].sum=t[root*2].sum+t[root*2+1].sum;
    t[root].num=(t[root*2].num+t[root*2+1].num)%10;
}
void build2(int x,int y,int root)
{
    if(x==y)
    {
        qaq[root].sum=0;
        qaq[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build2(x,mid,root*2);
    build2(mid+1,y,root*2+1);
    if(qaq[root*2].num+qaq[root*2+1].num>=10) qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum+1;
    else qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum;
    qaq[root].num=(qaq[root*2].num+qaq[root*2+1].num)%10;
}
int find(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return t[root].sum;
    int mid=(x+y)/2;
    int fx=find(x,mid,root*2),fy=find(mid+1,y,root*2+1);
    return fx+fy;
}
int find2(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return qaq[root].sum;
    int mid=(x+y)/2;
    int fx=find2(x,mid,root*2),fy=find2(mid+1,y,root*2+1);
    return fx+fy;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    build(1,n,1);
    build2(2,n-1,1);
    cin>>m;
    while(m--)
    {
        cin>>st>>ed;
        if(st%2==1) cout<<find(1,n,1)<<endl;
        else        cout<<find2(2,n-1,1)<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],ans=0,sum[100100],cnt=0;
int st,ed;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>st>>ed;
        cout<<(sum[ed]-sum[st-1])/10<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],ans=0,sum[100100],cnt=0;
int st,ed;
struct node
{
    int sum,num;
}t[35];
struct hehe
{
    int sum,num;
}qaq[35];
void build(int x,int y,int root)
{
    if(x==y)
    {
        t[root].sum=0;
        t[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build(x,mid,root*2);
    build(mid+1,y,root*2+1);
    if(t[root*2].num+t[root*2+1].num>=10) t[root].sum=t[root*2].sum+t[root*2+1].sum+1;
    else t[root].sum=t[root*2].sum+t[root*2+1].sum;
    t[root].num=(t[root*2].num+t[root*2+1].num)%10;
}
void build2(int x,int y,int root)
{
    if(x==y)
    {
        qaq[root].sum=0;
        qaq[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build2(x,mid,root*2);
    build2(mid+1,y,root*2+1);
    if(qaq[root*2].num+qaq[root*2+1].num>=10) qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum+1;
    else qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum;
    qaq[root].num=(qaq[root*2].num+qaq[root*2+1].num)%10;
}
int find(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return t[root].sum;
    int mid=(x+y)/2;
    int fx=find(x,mid,root*2),fy=find(mid+1,y,root*2+1);
    return fx+fy;
}
int find2(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return qaq[root].sum;
    int mid=(x+y)/2;
    int fx=find2(x,mid,root*2),fy=find2(mid+1,y,root*2+1);
    return fx+fy;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    build(1,n,1);
    build2(2,n-1,1);
    cin>>m;
    while(m--)
    {
        cin>>st>>ed;
        if(st%2==1) cout<<find(1,n,1)<<endl;
        else        cout<<find2(2,n-1,1)<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],ans=0,sum[100100],cnt=0;
int st,ed;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>st>>ed;
        cout<<(sum[ed]-sum[st]-1)/10<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m,a[110000],ans=0,sum[100100],res[25],cnt=0;
int st,ed;
struct node
{
    int sum,num;
}t[450000];
struct hehe
{
    int sum,num;
}qaq[450000];
void build(int x,int y,int root)
{
    if(x==y)
    {
        t[root].sum=0;
        t[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build(x,mid,root*2);
    build(mid+1,y,root*2+1);
    if(t[root*2].num+t[root*2+1].num>=10) t[root].sum=t[root*2].sum+t[root*2+1].sum+1;
    else t[root].sum=t[root*2].sum+t[root*2+1].sum;
    t[root].num=(t[root*2].num+t[root*2+1].num)%10;
}
void build2(int x,int y,int root)
{
    if(x==y)
    {
        qaq[root].sum=0;
        qaq[root].num=a[x];
        return;
    }int mid=(x+y)/2;
    build2(x,mid,root*2);
    build2(mid+1,y,root*2+1);
    if(qaq[root*2].num+qaq[root*2+1].num>=10) qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum+1;
    else qaq[root].sum=qaq[root*2].sum+qaq[root*2+1].sum;
    qaq[root].num=(qaq[root*2].num+qaq[root*2+1].num)%10;
}
int find(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return t[root].sum;
    int mid=(x+y)/2;
    int fx=find(x,mid,root*2),fy=find(mid+1,y,root*2+1);
    return fx+fy;
}
int find2(int x,int y,int root)
{
    if(x>ed||y<st) return 0;
    if(x>=st&&y<=ed) return qaq[root].sum;
    int mid=(x+y)/2;
    int fx=find2(x,mid,root*2),fy=find2(mid+1,y,root*2+1);
    return fx+fy;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    build(1,n,1);
    build2(2,n-1,1);
    cin>>m;
    while(m--)
    {
        cin>>st>>ed;
        if(st%2==1) cout<<find(1,n,1)<<endl;
        else        cout<<find2(2,n-1,1)<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int test,temp1,temp,i,a,b,c,d,result,e,temp2,temp3;
	cin>>test;
	for(i=0;i<test;i++)
	{
		cin>>a>>b>>c>>d;
		temp=max(c,d);
		temp1=min(c,d);
		e=temp1;
		if(b==0)
		{
			result=abs(c-d);
		}
		else if((temp+b)<=a)
		{
			result=max((temp+b)-temp1,(c-d));
			//cout<<result-temp1;
		}
		else if(temp==a&&(temp1>b))
		{
			result=temp-(temp1-b);

		}
		else if((temp+b)>a)
		{
//			for(i=1;i<=b;i++)
//			{
//				temp++;
//				if(temp==a)
//				{
//					temp1--;
//					if(temp1==1)
//					break;
//				}
//			}

//100 0 99 100
//100 98 99 100
//100 97 99 100
//100 0 100 99
//100 98 100 99
//100 97 100 99

             temp2=(a-temp);
             //cout<<temp2<<" ";
             temp3=(b-temp2);
             //cout<<temp3<<temp1<<" ";
             if(temp3>temp1)
             {
             	temp1=1;
			 }
			 else
			 {
			 	temp1=(d-temp3);

			 }
			 //cout<<(temp-temp1)<<"\n";
			 //cout<<(a-temp1)<<"\n";
			result=max((a-temp1),(temp-e));
			//cout<<result;

		}
		cout<<result<<endl;
		result=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int test,temp1,temp,i,a,b,c,d,result,e,temp2,temp3;
	cin>>test;
	for(i=0;i<test;i++)
	{
		cin>>a>>b>>c>>d;
		temp=max(c,d);
		temp1=min(c,d);
		e=temp1;
		if(b==0)
		{
			result=abs(c-d);
		}
		else if((temp+b)<=a)
		{
			result=max((temp+b)-temp1,(c-d));
			//cout<<result-temp1;
		}
		else if(temp==a)
		{
			result=temp-temp1;
		}
		else if((temp+b)>a)
		{
//			for(i=1;i<=b;i++)
//			{
//				temp++;
//				if(temp==a)
//				{
//					temp1--;
//					if(temp1==1)
//					break;
//				}
//			}
             temp2=(a-temp);
            // cout<<temp2<<" ";
             temp3=(b-temp2);
             //cout<<temp3<<temp1;
             if(temp3>temp1)
             {
             	temp1=1;
			 }
			 else
			 {
			 	temp1=(d-temp3);
			 }
			 //cout<<(temp-temp1)<<"\n";
			 //cout<<(a-temp1)<<"\n";
			result=max((a-temp1),(temp-e));
			//cout<<result;

		}
		cout<<result<<endl;
		result=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test,result,i,n,a,b,max1,min1,student,swap;
    vector<int> v;
    cin>>test;
    while(test--)
    {
        cin>>student>>swap;
        cin>>a>>b;
        v.push_back(a);
        v.push_back(b);
        if(swap==0)
        {
            result=abs(v[1]-v[0]);
            //cout<<result;

        }
        else{
            max1=max(v[0],v[1]);//100 33 100 1
            min1=min(v[0],v[1]);
            v.clear();
            //cout<<min1<<" "<<max1;
            while(swap--)
            {
                if(max1==student)
                {
                    break;
                }
                else
                     max1++;

            }
            result=max1-min1;
        }
        cout<<result<<endl;
        result=0;
    }
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int test,temp1,temp,i,a,b,c,d,result,e,temp2,temp3;
	cin>>test;
	for(i=0;i<test;i++)
	{
		cin>>a>>b>>c>>d;
		temp=max(c,d);
		temp1=min(c,d);
		e=temp1;
		if(b==0)
		{
			result=abs(c-d);
		}
		else if((temp+b)<=a)
		{
			result=max((temp+b)-temp1,(c-d));
			//cout<<result-temp1;
		}
		else if(temp==a&&(temp1>b))
		{
			result=temp-(temp1-b);

		}
		else if((temp+b)>a)
		{
			//93 20 55 90
             temp2=(a-temp);
             //cout<<temp2<<" ";
             temp3=(b-temp2);
             //cout<<temp3<<temp1<<" ";//51 42 38 47
             if(temp3>=temp1)
             {
             	temp1=1;
			 }
			 else
			 {
			 	temp1=(temp1-temp3);

			 }
//			 cout<<temp1<<" ";
//			 cout<<(temp-temp1)<<"\n";
//			 cout<<(a-temp1)<<"\n";
			result=max((a-temp1),(temp-e));
			//cout<<result;

		}
		cout<<result<<endl;

		result=0;
	}
	//51 42 38 47

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int test,temp1,temp,i,a,b,c,d,result,e,temp2,temp3;
	cin>>test;
	for(i=0;i<test;i++)
	{
		cin>>a>>b>>c>>d;
		temp=max(c,d);
		temp1=min(c,d);
		e=temp1;
		if(b==0)
		{
			result=abs(c-d);
		}
		else if((temp+b)<=a)
		{
			result=max((temp+b)-temp1,(c-d));
			//cout<<result-temp1;
		}
		else if(temp==a&&(temp1>b))
		{
			result=temp-(temp1-b);

		}
		else if((temp+b)>a)
		{
//			for(i=1;i<=b;i++)
//			{
//				temp++;
//				if(temp==a)
//				{
//					temp1--;
//					if(temp1==1)
//					break;
//				}
//			}

//100 0 99 100
//100 98 99 100
//100 97 99 100
//100 0 100 99
//100 98 100 99
//100 97 100 99

             temp2=(a-temp);
             cout<<temp2<<" ";
             temp3=(b-temp2);
             cout<<temp3<<temp1<<" ";
             if(temp3>temp1)
             {
             	temp1=1;
			 }
			 else
			 {
			 	temp1=(d-temp3);

			 }
			 //cout<<(temp-temp1)<<"\n";
			 //cout<<(a-temp1)<<"\n";
			result=max((a-temp1),(temp-e));
			//cout<<result;

		}
		cout<<result<<endl;
		result=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int t,n,x,a,b;
    cin>>t;
    while(t--){
        cin>>n>>x>>a>>b;
        if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
            //cout<<"a : "<<a<<" b : "<<b<<endl;
        }
        int m=a,n=b;
        int cnt=0;
        while(cnt<=x && b<=n){
            cnt++;
            b++;
        }
        cnt=0;
        while(cnt<=x && a>=1){
            cnt++;
            a--;
        }
        cout<<max(n-m,max(m-a,b-n))<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int t,n,x,a,b;
    cin>>t;
    while(t--){
        cin>>n>>x>>a>>b;
        if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
            //cout<<"a : "<<a<<" b : "<<b<<endl;
        }
        int cnt=0;
        if(cnt<x && b<n){
            cnt++;
            b++;
        }else if(cnt<x && a>1){
            cnt++;
            a--;
        }
        cout<<(b-a)<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int t,n,x,a,b;
    cin>>t;
    while(t--){
        cin>>n>>x>>a>>b;
        if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
            //cout<<"a : "<<a<<" b : "<<b<<endl;
        }
        int mx=0;
        int m=a-x,p=b+x;
        if(m>=1)mx=max(b-m,mx);
        if(p<=n)mx=max(mx,p-a);
        mx=max(mx,b-a);
        cout<<mx<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int t,n,x,a,b;
    cin>>t;
    while(t--){
        cin>>n>>x>>a>>b;
        if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
        }
        //cout<<"a : "<<a<<" b : "<<b<<endl;
        int m=a,p=b;
        int cnt=0;
        while(cnt<x && b<n){
            cnt++;
            b++;
        }
        while(cnt<x && a>1){
            cnt++;
            a--;
        }
        cout<<max(p-m,max(p-a,max(b-m,b-a)))<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int t,n,x,a,b;
    cin>>t;
    while(t--){
        cin>>n>>x>>a>>b;
        if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
            //cout<<"a : "<<a<<" b : "<<b<<endl;
        }
        int m=a,n=b;
        int cnt=0;
        while(cnt<x && b<n){
            cnt++;
            b++;
        }
        while(cnt<x && a>1){
            cnt++;
            a--;
        }
        cout<<max(n-m,b-a)<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q; cin>>q;
    while(q--){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int mi=min(a,b);
    int ma=max(a,b);
    int res=abs(a-b);
    if(x==0){
        cout<<res<<endl;
    }
    else if(a==n||b==n&&a==1||b==1){
        cout<<n-1<<endl;
    }
    else if(a==n||b==n){
            if((n-ma)+mi>x){
                cout<<(x+ma)-mi<<endl;

            }
            else if((n-ma)+mi<x){
                cout<<n-1<<endl;
            }
    }
    else{
            if(n-ma+mi>x)
            cout<<(x+ma)-mi<<endl;
    else if(n-ma+mi<x)
        cout<<n-1<<endl;

    }
    }

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q; cin>>q;
    while(q--){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int mi=min(a,b);
    int ma=max(a,b);
    int res=abs(a-b);
    if(x==0){
        cout<<res<<endl;
    }
    else if(a==n&&b==1||a==1&&b==n){
        cout<<n-1<<endl;
    }
    else if(a==n||b==n){
            if(n-ma+mi>x){
                cout<<(x+ma)-mi<<endl;

            }
            else if((n-ma)+mi<x){
                cout<<n-1<<endl;
            }
    }
    else{
            if(n-ma+mi>x)
            cout<<(x+ma)-mi<<endl;
    else if(n-ma+mi<x)
        cout<<n-1<<endl;

    }
    }

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c[5005];
int cnt;
int main()
{
int q; cin>>q;
while(q--){

    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int mi=min(a,b);
    int ma=max(a,b);
    if(x==0){
        cout<<abs(a-b)<<endl;

    }
    else if(a==n&&b==1||a==1&&b==n){
        cout<<n-1<<endl;

    }
    else if(a==n || b==n){
        cout<<abs(mi-ma)+x<<endl;

    }
    else{
            if((n-ma+mi)>x){
                cout<<(ma+x)-mi<<endl;
            }
            else{
                cout<<n-1<<endl;
            }

    }
}

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q; cin>>q;
    while(q--){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int res=abs(a-b);
    int d = (n-res)-1;
    cout<<res+min(d,x)<<endl;

    }

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c[5005];
int cnt;
int main()
{
int q; cin>>q;
while(q--){

    int n,x,a,b;
    cin>>n>>x>>a>>b;
    for(int i=1; i<=n; i++){
        c[i]=i;
    }
    int res = abs(a-b);
    int ans = max(a,b);
    int ans2 = min(a,b);
    int res2 = abs(a-b);
    if(x>=1){
        for(int i=ans+1; i<=n; i++){
            if(res<abs(ans2-c[i])){
                res = abs(ans2-c[i]);
                x--;
            }
            if(x==0)
                break;

        }
        for(int i=ans2-1;i>=0; i--){
                if(res2<abs(ans-c[i])){
                    res2=abs(ans-c[i]);
                    x--;
                    if(x==0)
                        break;
                }

        }
    }
    cout<<max(res,res2)<<endl;
    res=0;
    res2=0;
}

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c[5005];
int cnt;
int main()
{
int q; cin>>q;
while(q--){

    int n,x,a,b;
    cin>>n>>x>>a>>b;
    for(int i=0; i<n; i++){
        c[i]=i+1;
    }
    int res = abs(a-b);
    int ans = max(a,b);
    int ans2 = min(a,b);
    int res2 = abs(a-b);
    if(x>=1){
        for(int i=ans-1; i<n; i++){
            if(res<abs(a-c[i])){
                res = abs(a-c[i]);
                x--;
            }
            if(x==0)
                break;

        }
        for(int i=ans2-1;i>=0; i--){
                if(res2<abs(b-c[i])){
                    res2=abs(b-c[i]);
                    x--;
                    if(x==0)
                        break;
                }

        }
    }
    cout<<max(res,res2)<<endl;
    res=0;
    res2=0;
}

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c[5005];
int cnt;
int main()
{
    int q; cin>>q;
    while(q--){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    for(int i=0; i<n; i++){
        c[i]=i+1;
    }
    int res = abs(a-b);
    int ans = max(a,b);
    if(x>=1){
        for(int i=ans; i<n; i++){
            if(res<abs(a-c[i])){
                res = abs(a-c[i]);
                x--;
            }
            if(x==0)
                break;

        }
    }

 cout<<res<<endl;
 res = 0;
    }

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c[5005];
int cnt;
int main()
{
    int q; cin>>q;
    while(q--){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int mi=min(a,b);
    int ma=max(a,b);
    int res=abs(a-b);
    int res2=abs(a-b);

    if(x>=1){
    for(int i=ma+1;i<=n;i++){
         if(res<abs(mi-i)){
            res=abs(mi-i);
            x--;
         }
         if(x==0)
            break;
    }
    for(int i=mi-1;i>0;i--){
         if(res2<abs(i-ma)){
            res2 = abs(i-ma);
            x--;
         }
         if(x==0)
            break;

    }
    }
    cout<<max(res,res2)<<endl;
    res=0;
    res2=0;
}

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q; cin>>q;
    while(q--){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int mi=min(a,b);
    int ma=max(a,b);
    int res=abs(a-b);
    if(x==0){
        cout<<res<<endl;
    }
    else if(a==n&&b==1||a==1&&b==n){
        cout<<n-1<<endl;
    }
    else if(a==n||b==n){
            if(n-ma+mi>x){
                cout<<(x+ma)-mi<<endl;

            }
            else if((n-ma)+mi<x){
                cout<<n-1<<endl;
            }
    }
    else{
            if(n-ma+mi>x)
            cout<<(x+ma)-mi<<endl;
    else if(n-ma+mi<=x)
        cout<<n-1<<endl;

    }
    }

 return 0;
}


/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/

/*I LOVE KYRGYZSTAN
/******************
 *** **** **** ****
 ** ********** ****
 *** ******** *****
 **** ****** ******
 ***** **** *******
 ******** *********
 ZAMIROV NURSULTAN*/

/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/
#include<iostream>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<vector>
#include<cstring>
#include<ctime>

typedef signed long long int ull;

using namespace std;

int main(){

clock_t start = clock();

ios_base::sync_with_stdio(0); cin.tie(0);

int n; cin >> n;

for(int i = 0; i < n; i++){

	int n; cin >> n;

	int x; cin >> x;

	int a; cin >> a;

	int b; cin >> b;

	int a1 = min (a,b);

	int b1 = max (a,b);

	for(int j = 1; j <= x; j++){

		if(a1 > 1)a1--;

		else if(a1 == 1 && b1 < n) b1++;

		if(a1 == 1 && b1 == n)break;

	}

	cout << b1 - a1<< endl;

}

clock_t end = clock();

double seconds = (double)(end - start) / CLOCKS_PER_SEC;

//cout <<endl<< seconds;

return 0;}


/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/

/*I LOVE KYRGYZSTAN
/******************
 *** **** **** ****
 ** ********** ****
 *** ******** *****
 **** ****** ******
 ***** **** *******
 ******** *********
 ZAMIROV NURSULTAN*/

/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/
#include<iostream>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<vector>
#include<cstring>
#include<ctime>

typedef signed long long int ull;

using namespace std;

int main(){

clock_t start = clock();

ios_base::sync_with_stdio(0); cin.tie(0);

int n; cin >> n;

for(int i = 0; i < n; i++){

	int n; cin >> n;

	int x; cin >> x;

	int a; cin >> a;

	int b; cin >> b;

	int a1 = min (a,b);

	int b1 = max (a,b);

	for(int j = 1; j <= x; j++){

		if(a1 > 1)a1--;

		else if(a1 == 1 && b1 <= n) b1++;

		if(a1 == 1 && b1 == n)break;

	}

	cout << b1 - a1<< endl;

}

clock_t end = clock();

double seconds = (double)(end - start) / CLOCKS_PER_SEC;

cout <<endl<< seconds;

return 0;}


/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/

/*I LOVE KYRGYZSTAN
/******************
 *** **** **** ****
 ** ********** ****
 *** ******** *****
 **** ****** ******
 ***** **** *******
 ******** *********
 ZAMIROV NURSULTAN*/

/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/
#include<iostream>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<vector>
#include<cstring>
#include<ctime>

typedef signed long long int ull;

using namespace std;

int main(){

clock_t start = clock();

ios_base::sync_with_stdio(0); cin.tie(0);

int n; cin >> n;

for(int i = 0; i < n; i++){

	int n; cin >> n;

	int x; cin >> x;

	int a; cin >> a;

	int b; cin >> b;

	int a1 = min (a,b);

	int b1 = max (a,b);

	for(int j = 1; j <= x; j++){

		if(a1 > 1)a1--;

		else if(a1 == 1 && b1 < n) b1++;

		if(a1 == 1 && b1 == n)break;

	}

	cout << b1 - a1<< endl;

}

clock_t end = clock();

double seconds = (double)(end - start) / CLOCKS_PER_SEC;

cout <<endl<< seconds;

return 0;}


/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/

/*I LOVE KYRGYZSTAN
/******************
 *** **** **** ****
 ** ********** ****
 *** ******** *****
 **** ****** ******
 ***** **** *******
 ******** *********
 ZAMIROV NURSULTAN*/

/************************************/
/*KYRGYZSTAN - a magnificent country*/
/************************************/
#include<iostream>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<vector>
#include<cstring>
#include<ctime>

typedef signed long long int ull;

using namespace std;

int main(){

clock_t start = clock();

ios_base::sync_with_stdio(0); cin.tie(0);

int n; cin >> n;

for(int i = 0; i < n; i++){

	int n; cin >> n;

	int x; cin >> x;

	int a; cin >> a;

	int b; cin >> b;

	int a1 = min (a,b);

	int b1 = max (a,b);

	for(int j = 1; j <= x; j++){

		if(a1 > 1)a1--;

		else if(a1 == 1 && b1 < n) b++;

		if(a1 == 1 && b1 == n)break;

	}

	cout << b1 - a1 << endl;

}

clock_t end = clock();

double seconds = (double)(end - start) / CLOCKS_PER_SEC;

//cout <<endl<< seconds;

return 0;}


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

using namespace std;

int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n, x, a, b;
    scanf("%d %d %d %d", &n, &x, &a, &b);
    if (x == 0) {
      printf("%d\n", abs(a-b));
    } else if (x == 1) {
      printf("%d\n", max(max(abs(1-b), abs(1-a)), max(abs(n-b), abs(n-a))));
    } else {
      printf("%d\n", n-1);
    }
  }
  return 0;
}


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

using namespace std;

int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n, x, a, b;
    scanf("%d %d %d %d", &n, &x, &a, &b);
    if (x == 0) {
      printf("%d\n", abs(a-b));
    } else if (x == 1) {
      printf("%d\n", max(max(abs(1-b), abs(1-a)), max(abs(1-b), abs(1-a))));
    } else {
      printf("%d\n", n-1);
    }
  }
  return 0;
}


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

using namespace std;

int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n, x, a, b;
    scanf("%d %d %d %d", &n, &x, &a, &b);
    printf("%d\n", min(n-1, abs(a-b) + x));
  }
  return 0;
}


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

using namespace std;

int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n, x, a, b;
    scanf("%d %d %d %d", &n, &x, &a, &b);
    if (x == 0) {
      printf("%d\n", abs(a-b));
    } else if (x == 1) {
      printf("%d\n", max(abs(1-b), abs(1-a)));
    } else {
      printf("%d\n", n-1);
    }
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
#define forr(i, a, b) for(int i = a; i < b; i++)
#define print(x) cout << x << endl
#define has(v, x) count(v.begin(), v.end(), x)
#define show(a, n) forr(i, 0, n) cout << a[i] << " ";

int main() {

    int n, x, a, b, t;
    cin >> t;
    int ans[t];
    int i = 0;

    forr(i, 0, t) {
        int flag = 0;
        cin >> n >> x >> a >> b;
        if(a > b) {
            int c = b;
            b = a;
            a = c;
        }
        //cout << a << b << endl;
        if(x == 0) flag = b - a;
        else if(abs(a - b) == (n - 1)) flag = n - 1;
        else if(x >= n) flag = n - 1;
        else {

            if((b + x) <= n) flag = b + x - a;
            else {
                a -= (b + x - n);
                if(a >= 1) flag = n - a;
                else flag = n - 1;
            }
        }
        ans[i] = flag;
    }

    for(int i : ans) print(i);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
#define forr(i, a, b) for(int i = a; i < b; i++)
#define print(x) cout << x << endl
#define has(v, x) count(v.begin(), v.end(), x)
#define show(a, n) forr(i, 0, n) cout << a[i] << " ";

int main() {

    int n, x, a, b, t;
    cin >> t;
    int ans[t];
    int i = 0;

    forr(i, 0, t) {
        int flag = 0;
        cin >> n >> x >> a >> b;
        if(a > b) {
            int c = b;
            b = a;
            a = c;
        }
        //cout << a << b << endl;
        if(x == 0) flag = 1;
        else if(abs(a - b) == (n - 1)) flag = n - 1;
        else if(x >= n) flag = n - 1;
        else {
            // if(a > b) {
            //     int t = b;
            //     b = a;
            //     a = t;
            // }
            if((n - b) >= x) flag = abs(x + b - a);
            else {
                flag = abs(a - 2 * n + b);
            }
        }
        ans[i] = flag;
    }

    for(int i : ans) print(i);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
#define forr(i, a, b) for(int i = a; i < b; i++)
#define print(x) cout << x << endl
#define has(v, x) count(v.begin(), v.end(), x)
#define show(a, n) forr(i, 0, n) cout << a[i] << " ";

int main() {

    int n, x, a, b, t;
    cin >> t;
    int ans[t];
    int i = 0;

    forr(i, 0, t) {
        int flag = 0;
        cin >> n >> x >> a >> b;
        if(x == 0) flag = 1;
        else if(abs(a - b) == (n - 1)) flag = n - 1;
        else if(x >= n) flag = n - 1;
        else {
            if(a > b) {
                int t = b;
                b = a;
                a = t;
            }
            if((n - b) >= x) flag = abs(x + b - n);
            else {
                flag = abs(a - 2 * n + b);
            }
        }
        ans[i] = flag;
    }

    for(int i : ans) print(i);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
#define forr(i, a, b) for(int i = a; i < b; i++)
#define print(x) cout << x << endl
#define has(v, x) count(v.begin(), v.end(), x)
#define show(a, n) forr(i, 0, n) cout << a[i] << " ";

int main() {

    int n, x, a, b, t;
    cin >> t;
    int ans[t];
    int i = 0;

    forr(i, 0, t) {
        int flag = 0;
        cin >> n >> x >> a >> b;
        if(a > b) {
            int c = b;
            b = a;
            a = c;
        }
        //cout << a << b << endl;
        if(x == 0) flag = b - a;
        else if(abs(a - b) == (n - 1)) flag = n - 1;
        else if(x >= n) flag = n - 1;
        else {
            // if(a > b) {
            //     int t = b;
            //     b = a;
            //     a = t;
            // }
            if((n - b) >= x) flag = abs(x + b - a);
            else {
                flag = abs(a - 2 * n + b);
            }
        }
        ans[i] = flag;
    }

    for(int i : ans) print(i);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
#define forr(i, a, b) for(int i = a; i < b; i++)
#define print(x) cout << x << endl
#define has(v, x) count(v.begin(), v.end(), x)
#define show(a, n) forr(i, 0, n) cout << a[i] << " ";

int main() {

    int n, x, a, b, t;
    cin >> t;
    int ans[t];
    int i = 0;

    forr(i, 0, t) {
        int flag = 0;
        cin >> n >> x >> a >> b;
        if(a > b) {
            int c = b;
            b = a;
            a = c;
        }
        //cout << a << b << endl;
        if(x == 0) flag = b - a;
        else if(abs(a - b) == (n - 1)) flag = n - 1;
        else if(x >= n) flag = n - 1;
        else {

            if((b + x) <= n) flag = b + x - a;
            else {
                flag = n - (a - b - x + n);
            }
        }
        ans[i] = flag;
    }

    for(int i : ans) print(i);

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
int a[100];
int main()
{
	int T,i,j;
	scanf("%d",&T);
	while(T--)
	{
		int n,a,b,k;
		scanf("%d %d %d %d",&n,&k,&a,&b);
		if(a>b)
		swap(a,b);
		while(k>0&&b<n)
		{
			k--;
			b++;
		}
		while(k>0&&a>1)
		{
			k--;
			a--;
		}
		printf("%d\n",b-a);
	 }
	return 0;
}


// Two Rival Students
// https://codeforces.com/problemset/problem/1257/A
// Aman Kumar

/*
	INSIGHT:
		- See code
*/

#include <bits/stdc++.h>

using namespace std ;

#define ll long long int

void func() ;

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

	int t ;
	cin >> t ;

	while( t-- )
	{
		func() ;
	}

	return 0 ;
}

void func()
{
	int n, x, a, b ;
	cin >> n >> x >> a >> b ;

	if( a > b )
	{
		int temp = a ;
		a = b ;
		b = temp ;
	}

	// If no swaps are possible or both are at ends
	if( x == 0 || ( a==1 && b== n ) )
		cout << abs(a-b) ;

	else
	{
		int bpos = b + x ;

		if( bpos > n )
			x = bpos-n ;
		else
			x = 0 ;

		int apos = a ;

		if( x > 0 )
		{
			int apos = apos - x ;

			if( apos < 0 )
				apos = 0 ;
		}
		cout << abs( bpos - apos ) ;
	}
	cout << "\n" ;
}


// Two Rival Students
// https://codeforces.com/problemset/problem/1257/A
// Aman Kumar

/*
	INSIGHT:
		- See code
*/

#include <bits/stdc++.h>

using namespace std ;

#define ll long long int

void func() ;

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

	int t ;
	cin >> t ;

	while( t-- )
	{
		func() ;
	}

	return 0 ;
}

void func()
{
	int n, x, a, b ;
	cin >> n >> x >> a >> b ;

	if( a > b )
	{
		int temp = a ;
		a = b ;
		b = temp ;
	}

	// If no swaps are possible or both are at ends
	if( x == 0 || ( a==1 && b == n ) )
		cout << abs(a-b) ;

	else
	{
		int bpos = b + x ;

		if( bpos > n )
			x = bpos-n ;
		else
			x = 0 ;

		int apos = a ;

		if( x > 0 )
		{
			int apos = apos - x ;

			if( apos < 1 )
				apos = 1 ;
		}
		cout << abs( bpos - apos ) ;
	}
	cout << "\n" ;
}


// Two Rival Students
// https://codeforces.com/problemset/problem/1257/A
// Aman Kumar

/*
	INSIGHT:
		- See code
*/

#include <bits/stdc++.h>

using namespace std ;

#define ll long long int

void func() ;

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

	int t ;
	cin >> t ;

	while( t-- )
	{
		func() ;
	}

	return 0 ;
}

void func()
{
	int n, x, a, b ;
	cin >> n >> x >> a >> b ;

	if( a > b )
	{
		int temp = a ;
		a = b ;
		b = temp ;
	}

	// If no swaps are possible or both are at ends
	if( x == 0 || ( a==1 && b == n ) )
		cout << abs(a-b) ;

	else
	{
		int bpos = b + x ;

		if( bpos > n )
		{
			x = bpos-n ;
			bpos = n ;
		}
		else
			x = 0 ;

		int apos = a ;

		if( x > 0 )
		{
			apos = a - x ;

			if( apos < 1 )
				apos = 1 ;
		}
		cout << abs( bpos - apos ) ;
	}
	cout << "\n" ;
}


// Two Rival Students
// https://codeforces.com/problemset/problem/1257/A
// Aman Kumar

/*
	INSIGHT:
		- See code
*/

#include <bits/stdc++.h>

using namespace std ;

#define ll long long int

void func() ;

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

	int t ;
	cin >> t ;

	while( t-- )
	{
		func() ;
	}

	return 0 ;
}

void func()
{
	int n, x, a, b ;
	cin >> n >> x >> a >> b ;

	if( a > b )
	{
		int temp = a ;
		a = b ;
		b = temp ;
	}

	// If no swaps are possible or both are at ends
	if( x == 0 || ( a==1 && b == n ) )
		cout << abs(a-b) ;

	else
	{
		int bpos = b + x ;

		if( bpos > n )
			x = bpos-n ;
		else
			x = 0 ;

		int apos = a ;

		if( x > 0 )
		{
			int apos = a - x ;

			if( apos < 1 )
				apos = 1 ;
		}
		cout << abs( bpos - apos ) ;
	}
	cout << "\n" ;
}


// Two Rival Students
// https://codeforces.com/problemset/problem/1257/A
// Aman Kumar

/*
	INSIGHT:
		- See code
*/

#include <bits/stdc++.h>

using namespace std ;

#define ll long long int

void func() ;

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

	int t ;
	cin >> t ;

	while( t-- )
	{
		func() ;
	}

	return 0 ;
}

void func()
{
	int n, x, a, b ;
	cin >> n >> x >> a >> b ;

	if( a > b )
	{
		int temp = a ;
		a = b ;
		b = temp ;
	}

	// If no swaps are possible or both are at ends
	if( x == 0 || ( a==1 && b == n ) )
		cout << abs(a-b) ;

	else
	{
		int bpos = b + x ;

		if( bpos > n )
		{
			x = bpos-n ;
			bpos = n ;
		}
		else
			x = 0 ;

		int apos = a ;

		if( x > 0 )
		{
			int apos = a - x ;

			if( apos < 1 )
				apos = 1 ;
		}
		cout << abs( bpos - apos ) ;
	}
	cout << "\n" ;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,c,d,e;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>b>>c>>d>>e;

        if(d<e)
            swap(e,d);

        if(d==100&&e==1)
        {
            cout<<abs(d-e)<<"\n";
        }
        else
        {
            int x=d-e+1;

            int y=b-(d-e+1);

            if(c>=y)
            {
                cout<<abs(d-e)<<"\n";
            }
            else
                cout<<abs(abs(d-e)+c)<<"\n";

        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,c,d,e;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>b>>c>>d>>e;

        if(d<e)
            swap(e,d);

        if(d==b&&e==1)
        {
            cout<<abs(d-e)<<"\n";
        }
        else
        {
            int x=e-1;

            int y=b-d;

            if(c>=x+y)
            {
                cout<<abs(b-1)<<"\n";
            }
            else
                cout<<abs(abs(d-e)+c)<<"\n";

        }
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;

    while(t--)
    {
        int a,b,c,d;

        cin>>a>>b>>c>>d;

        int arr[a];

        for(int j=0;j<a;j++)
        {
            arr[j]=0;
        }

        if(c>d)
            swap(c,d);

        arr[c-1]=1;
        arr[d-1]=2;

        int m=c-1,n=d-1;

        while(m>=1&&b>0)
        {
            swap(arr[m],arr[m-1]);

            m--;

            b--;
        }

        while(n<a-1&&b>0)
        {
            swap(arr[n],arr[n+1]);

            n++;

            b--;
        }

        int len1=0,len2=0;

        for(int j=0;j<a;j++)
        {
            if(arr[j]==1)
            {
                len1=j+1;
            }

            if(arr[j]==2)
                len2=j+1;
        }

        cout<<abs(len1-len2)<<"\n";
    }
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        int a,b,c,d;

        cin>>a>>b>>c>>d;

        if(c>d)
            swap(c,d);

        int m=c,n=d;

        while(m>=1&&b>0)
        {

            if(m==1)
                break;

            m--;

            b--;
        }

        while(n<=a&&b>0)
        {

          if(n==a)
            break;
            n++;

            b--;
        }

        cout<<abs(n-m)<<"\n";
    }
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		//cout<<n<<' '<<x<<' '<<a<<' '<<b<<endl;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a == 1 && b == n){
			a = 1;
		}else if (a == 1){
			if (b+x<=n) b += x;
			else b = n;
		}else if (b == n){
			if (a <= x+1) a = 1;
			else a -= x;
		}else{
			if (a <= x){
				a = 1;
				x -= (a-1);
				if ((n-b)<=x){
					b = n;
				}else{
					b += x;
				}
			}else{
				a -= x;
				x = 0;
			}
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a <= x){
			a = 1;
			x -= a+1;
		}else{
			a -= x;
			x = 0;
		}
		if ((n-b)>=x){
			b += x;
			x = 0;
		}else{
			b = n;
			x -= (n-x);
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		//cout<<n<<' '<<x<<' '<<a<<' '<<b<<endl;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a == 1 && b == n){
			a = 1;
		}else if (a == 1){
			if (b+x<=n) b += x;
			else b = n;
		}else if (b == n){
			if (a <= x+1) a = 1;
			else a -= x;
		}else{
			if (a <= x){
				int pos = a;
				a = 1;
				x -= (pos-1);
				//cout<<"x="<<x<<"a="<<a<<endl;
				if ((n-b)<=x){
					b = n;
				}else{
					b += x;
				}
				//cout<<"b="<<b<<endl;
			}else{
				a -= x;
				x = 0;
			}
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		//cout<<n<<' '<<x<<' '<<a<<' '<<b<<endl;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a == 1 && b == n){
			a = 1;
		}else if (a == 1){
			if (b+x<=n) b += x;
			else b = n;
		}else if (b == n){
			if (a <= x+1) a = 1;
			else a -= x;
		}else{
			if (a <= x){
				a = 1;
				x -= (a+1);
				if ((n-b)<=x){
					b = n;
				}else{
					b += x;
				}
			}else{
				a -= x;
				x = 0;
			}
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		//cout<<n<<' '<<x<<' '<<a<<' '<<b<<endl;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a == 1 && b == n){
			a = 1;
		}else if (a == 1){
			if (b+x<=n) b += x;
			else b = n;
		}else if (b == n){
			if (a <= x+1) a = 1;
			else a -= x;
		}else{
			if (a <= x){
				int pos = a;
				a = 1;
				x -= (pos-1);
				//cout<<"x="<<x<<"a="<<a<<endl;
				if ((n-b)<=x){
					b = n;
				}else{
					b += x;
				}
				cout<<"b="<<b<<endl;
			}else{
				a -= x;
				x = 0;
			}
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		//cout<<n<<' '<<x<<' '<<a<<' '<<b<<endl;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a == 1 && b == n){
			a = 1;
		}else if (a == 1){
			if (b+x<=n) b += x;
			else b = n;
		}else if (b == n){
			if (a <= x+1) a = 1;
			else a -= x;
		}else{
			if (a <= x+1){
				a = 1;
				x -= (a-1);
				if ((n-b)<=x){
					b = n;
				}else{
					b += x;
				}
			}else{
				a -= x;
				x = 0;
			}
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a <= x){
			a = 1;
			x -= a;
		}else{
			a -= x;
			x = 0;
		}
		if ((n-b)>=x){
			b += x;
			x = 0;
		}else{
			b = n;
			x -= (n-x);
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
	//freopen("D:\\c - free\\codeforce_education_70_A\\input.txt","r",stdin);
	int t;cin>>t;
	int n,x,a,b;
	while(t--){
		cin>>n>>x>>a>>b;
		//cout<<n<<' '<<x<<' '<<a<<' '<<b<<endl;
		int ma;
		if (a > b){
			ma = a;
			a = b;
			b = ma;
		}
		if (a == 1 && b == n){
			a = 1;
		}else if (a == 1){
			if (b+x<=n) b += x;
			else b = n;
		}else if (b == n){
			if (a <= x+1) a = 1;
			else a -= x;
		}else{
			if (a <= x){
				int pos = a;
				a = 1;
				x -= (pos-1);
				cout<<"x="<<x<<"a="<<a<<endl;
				if ((n-b)<=x){
					b = n;
				}else{
					b += x;
				}
				cout<<"b="<<b<<endl;
			}else{
				a -= x;
				x = 0;
			}
		}
		cout<<b-a<<endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int mn,mx,i,t,n,x,a,b;
int main ()
{
	cin>>t;
	for (i=1;i<=t;i++)
	{
		cin>>n>>x>>a>>b;
		mx=max(a,b); mn=min(a,b); ans[i]=mx-mn;
	    mx=mx+x; if (mx>n) { x=x+mx-n; mx=n;
		}
		mn=mn-x; if (mn<1)  mn=1;
		cout<<mx-mn;
		}

}


#include <iostream>
using namespace std;
int mn,mx,i,t,n,x,a,b;
int main ()
{
	cin>>t;
	for (i=1;i<=t;i++)
	{
		cin>>n>>x>>a>>b;
		mx=max(a,b); mn=min(a,b);
	    mx=mx+x; if (mx>n) { x=x+mx-n; mx=n;
		}
		mn=mn-x; if (mn<1)  mn=1;
		cout<<mx-mn;
		}

}


#include <iostream>
using namespace std;
int mn,mx,i,t,n,x,a,b;
int main ()
{
	cin>>t;
	for (i=1;i<=t;i++)
	{
		cin>>n>>x>>a>>b;
		mx=max(a,b); mn=min(a,b);
	    mx=mx+x; if (mx>n) { x=x+mx-n; mx=n;
		} else x=0;
		mn=mn-x; if (mn<1)  mn=1;
		cout<<mx-mn;
		}

}


#include <iostream>
using namespace std;
int mn,mx,i,t,n,x,a,b,ans[101];
int main ()
{
	cin>>t;
	for (i=1;i<=t;i++)
	{
		cin>>n>>x>>a>>b;
		mx=max(a,b); mn=min(a,b); ans[i]=mx-mn;
		while (mx<n && x>0)
		{
			x--;
			mx++;
		}
		if (x==0) ans[i]=mx-mn;
		else while (mn>1 && x>0)
		{
			x--;
			mn--; ans[i]=mx-mn;
		}
	}
	for (i=1;i<=t;i++)
	cout<<ans[i]<<endl;

}


#include <iostream>
using namespace std;
int mn,mx,i,t,n,x,a,b;
int main ()
{
	cin>>t;
	for (i=1;i<=t;i++)
	{
		cin>>n>>x>>a>>b;
		mx=max(a,b); mn=min(a,b);
	    mx=mx+x; if (mx>n) { x=x+mx-n; mx=n;
		} else x=0;
		mn=mn-x; if (mn<1)  mn=1;
		cout<<mx-mn<<endl;
		}

}


#include <iostream>
using namespace std;
int mn,mx,i,t,n,x,a,b;
int main ()
{
	cin>>t;
	for (i=1;i<=t;i++)
	{
		cin>>n>>x>>a>>b;
		mx=max(a,b); mn=min(a,b);
	    mx=mx+x; if (mx>n) { x=mx-n; mx=n;
		} else x=0;
		mn=mn-x; if (mn<1)  mn=1;
		cout<<mx-mn<<endl;
		}

}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;

int main(){
    int t, n, x, a, b, i;
    cin >> t;
    while(t>0){
        cin >> n;
        cin >> x;
        cin >> a;
        cin >> b;
        while(x>0){
            if(a<b && a>0){
                a--;
            }else if(b<a && b>0){
                b--;
            }else if(b>a && b<n-1){
                b++;
            }else if(a>b && a<n-1){
                a++;
            }
            x--;
        }
        if(a==0){
            a++;
        }
        if(b==0){
            b++;
        }
        cout << abs(a-b);
        t--;
    }


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;

int main(){
    int t, n, x, a, b, i;
    cin >> t;
    while(t>0){
        cin >> n;
        cin >> x;
        cin >> a;
        cin >> b;
        while(x>0){
            if(a<b && a>0){
                a--;
            }else if(b<a && b>0){
                b--;
            }else if(b>a && b<n-1){
                b++;
            }else if(a>b && a<n-1){
                a++;
            }
            x--;
        }
        if(a==0){
            a++;
        }
        if(b==0){
            b++;
        }
        cout << abs(a-b);
        cout << "\n";
        t--;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;

int main(){
    int t, n, x, a, b, i;
    cin >> t;
    while(t>0){
        cin >> n;
        cin >> x;
        cin >> a;
        cin >> b;
        while(x>0){
            if(a<b && a>0){
                a--;
            }else if(b<a && b>0){
                b--;
            }else if(b>a && b<n-1){
                b++;
            }else if(a>b && a<n-1){
                a++;
            }
            x--;
        }
        if(a==0){
            a++;
        }
        if(b==0){
            b++;
        }
        cout << abs(a-b);
        t--;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;

int main(){
    int t, n, x, a, b, i;
    cin >> t;
    while(t>0){
        cin >> n;
        cin >> x;
        cin >> a;
        cin >> b;
        while(x>0){
            if(a<b && a>0){
                a--;
            }else if(b<a && b>0){
                b--;
            }else if(b>a && b<n){
                b++;
            }else if(a>b && a<n){
                a++;
            }
            x--;
        }
        if(a==0){
            a++;
        }
        if(b==0){
            b++;
        }
        cout << abs(a-b);
        cout << "\n";
        t--;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;

int main(){
    int t, n, x, a, b, i;
    cin >> t;
    while(t>0){
        cin >> n;
        cin >> x;
        cin >> a;
        cin >> b;
        while(x>0){
            if(a<b && a>1){
                a--;
            }else if(b<a && b>1){
                b--;
            }else if(b>a && b<n){
                b++;
            }else if(a>b && a<n){
                a++;
            }
            x--;
        }
        if(a==0){
            a++;
        }
        if(b==0){
            b++;
        }
        cout << abs(a-b);
        cout << "\n";
        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,x,a,b,res;
        cin>>n>>x>>a>>b;

        if(x==0)
        {
            if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }

        else if((a==n && b==1 )||(b==n && a==1) )
        {   if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;
        }
        else if(a>b)
        { int l=(a-b)+x;
         if (l>=n)
         {
            cout<<n-1<<endl;
         }
         else
         {
             cout<<l<<endl;
         }
        }
         else
        { int r =(b-a)+x;
         if (r>=n)
         {
            cout<<n-1<<endl;
         }
         else
         {
             cout<<r<<endl;
         }
        }

     }

    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,x,a,b,res;
        cin>>n>>x>>a>>b;

        if(x==0)
        {
            if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }

        else if((a==n && b==1 )||(b==n && a==1) )
        {

             if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }
        else if(b>a)
        {
            cout<<a-(b-x)<<endl;

        }
        else if((a==n && x>n) || (b==n && x>n))
        {
            cout<<n-1<<endl;
        }
        else
              {
                if(a>b)
                {
                    int r=(a+x)-b;
                    if(r>n)
                    {
                      cout<<n-1<<endl;
                    }
                   else
                   {
                       cout<<(a+x)-b<<endl;
                   }
                }

            }

     }

    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,x,a,b,res;
        cin>>n>>x>>a>>b;

        if(x==0)
        {
            if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }

        else if((a==n && b==1 )||(b==n && a==1) )
        {   if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;
        }
        else if(a>b)
        { int l=(a-b)+x;
         if (l>n)
         {
            cout<<n-1<<endl;
         }
         else
         {
             cout<<l<<endl;
         }
        }
         else
        { int r =(b-a)+x;
         if (r>n)
         {
            cout<<n-1<<endl;
         }
         else
         {
             cout<<r<<endl;
         }
        }

     }

    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,x,a,b,res;
        cin>>n>>x>>a>>b;
        if(x==0 || a==n || x>n || b==n)
        {
            if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }
        else if(b>a)
        {
             cout<<(a-x)-b<<endl;

        }
        else
        {
            cout<<(a+x)-b<<endl;

        }

     }

    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,x,a,b,res;
        cin>>n>>x>>a>>b;

        if(x==0)
        {
            if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }

        else if((a==n && b==1 )||(b==n && a==1) )
        {

             if(a>b)
            cout<<a-b<<endl;
            else
            cout<<b-a<<endl;

        }
        else if(b>a)
        {
            cout<<(b+x)-a<<endl;

        }
         else if(a>b)
        {
            cout<<(a+x)-b<<endl;

        }
        else if((a==n && x>n) || (b==n && x>n))
        {
            cout<<n-1<<endl;
        }

        else
              {
                if(a>b)
                {
                    int r=(a+x)-b;
                    if(r>n)
                    {
                      cout<<n-1<<endl;
                    }
                   else
                   {
                       cout<<(a+x)-b<<endl;
                   }
                }

            }

     }

    }


#include<bits/stdc++.h>
using namespace std;
int a[1000];
int main(){
	int m;
	cin>>m;
	while(m--){
		int n,x,a,b;
		cin>>n>>x>>a>>b;
		int sum=abs(b-a);
		while(x!=0&&sum!=n-1){
			if(b+x<=n&&b<=n&&x>0){
				x--;
				sum++;
				b++;
			}
			if(a-x>=1&&a>0&&x>0){
				x--;
				sum++;
				a--;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1000];
int main(){
	int m;
	cin>>m;
	while(m--){
		int n,x,a,b;
		cin>>n>>x>>a>>b;
		int c=min(a,b);
		int d=max(a,b);
		a=c;
		b=d;
		int sum=abs(b-a);
		while(x!=0){
			if(b<n&&x>0){
				x--;
				sum++;
				b++;
			}
			if(a>1&&x>0){
				x--;
				sum++;
				a--;
			}
			if(a==1&&b==n){
				break;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int m;
	cin>>m;
	while(m--){
		int n,x,a,b;
		cin>>n>>x>>a>>b;
		if(a-b==n-1){
			cout<<n-1<<endl;
			continue;
		}
		else if(a-x<=0&&b+x<=n){
			cout<<max(b-1,abs(a-b))<<endl;
			continue;
		}
		else if(x+b>n&&a-x>=1){
			cout<<max(n-a,abs(a-b))<<endl;
			continue;
		}
		else if(x==0){
			cout<<abs(a-b)<<endl;
			continue;
		}
		else if(a-x>0&&b+x<=n){
			cout<<abs(a-b)+x<<endl;
			continue;
		}
		else if(a-x<=0&&b+x>n){
			cout<<max(b-1,n-a)<<endl;
			continue;
		}
	}
}