#include <bits/stdc++.h>
using namespace std;
int main () {
    int t;
    cin >> t;
    while (t--) {
        long long int n, k, d = 0;
        cin >> n >> k;
        vector<long long int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        if (k >= 5) {
            k = 5 + (k - 5) % 2;
        }
        while (k--) {
            d = a[0];
            for (long long int x : a) {
                if (d < x) d = x;
            }
            for (long long int & x : a) {
                x = d - x;
            }
        }
        for (long long int x : a) {
            cout << x << " ";
        }
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
int main () {
    int t;
    cin >> t;
    while (t--) {
        int n, k, d = 0;
        cin >> n >> k;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        if (k >= 5) {
            k = 5 - (k-5)%2;
        }
        while (k--) {
            d = a[0];
            for (int x : a) {
                d = max(d, x);
            }
            for (int x : a) {
                x = d - x;
            }
        }
        for (int x : a) {
            cout << x << " ";
        }
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
int main () {
    int t;
    cin >> t;
    while (t--) {
        int n, k, d = 0;
        cin >> n >> k;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        if (k >= 5) {
            k = 5 + (k - 5) % 2;
        }
        while (k--) {
            d = a[0];
            for (int x : a) {
                if (d < x) d = x;
            }
            for (int& x : a) {
                x = d - x;
            }
        }
        for (int x : a) {
            cout << x << " ";
        }
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
int main () {
    int t;
    cin >> t;
    while (t--) {
        long long int n, k, d = 0, dd = 0;
        cin >> n >> k;
        vector<long long int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            if (d < a[i]) d = a[i];
        }
        while (k--) {
            for (int i = 0; i < n; i++) {
                a[i] = d-a[i];
                if (dd < a[i]) {
                    dd = a[i];
                }
            }
            if (dd == 0 || d == 0) {
                break;
            }
            d = dd;
            dd = 0;
        }
        for (int i = 0; i < n; i++) {
            cout << a[i] << " ";
        }
        cout << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define mod 1000000007
#define pi acos(-1.0)
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long

void solve()
{
int n,k,i,d;
cin>>n>>k;
int arr[n];
for (i=0;i<n;i++)
    cin>>arr[i];
    while (k--){
    d=*max_element(arr,arr+n);
    for (i=0;i<n;i++)
        arr[i]=d-(arr[i]);
}
for (i=0;i<n;i++)
    cout<<arr[i]<<" ";
cout<<endl;

}
int main()
{
    FastIO;
    int t;
    cin>>t;
    while (t--)
        solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define mod 1000000007
#define pi acos(-1.0)
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long

void solve()
{
ll n,k,i,d,x,y;
cin>>n>>k;
int arr[n];
for (i=0;i<n;i++)
    cin>>arr[i];
     while (k--){
    d=*max_element(arr,arr+n);
    for (i=0;i<n;i++)
        arr[i]=d-(arr[i]);
}
for (i=0;i<n;i++)
    cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
    FastIO;
    int t;
    cin>>t;
    while (t--)
        solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define mod 1000000007
#define pi acos(-1.0)
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long

void solve()
{
ll n,k,i,d,x,y;
cin>>n>>k;
int arr[n];
for (i=0;i<n;i++)
    cin>>arr[i];
     while (k--){
    d=*max_element(arr,arr+n);
    for (i=0;i<n;i++)
        arr[i]=d-(arr[i]);
     if (k==k/2)
     break;
}
for (i=0;i<n;i++)
    cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
    FastIO;
    int t;
    cin>>t;
    while (t--)
        solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define mod 1000000007
#define pi acos(-1.0)
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long

void solve()
{
ll n,k,i,d,x,y;
cin>>n>>k;
int arr[n];
for (i=0;i<n;i++)
    cin>>arr[i];
    x=0;
     while (x<=1){
    d=*max_element(arr,arr+n);
    for (i=0;i<n;i++)
        arr[i]=d-(arr[i]);
    x++;
}
for (i=0;i<n;i++)
    cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
    FastIO;
    int t;
    cin>>t;
    while (t--)
        solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define mod 1000000007
#define pi acos(-1.0)
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long

void solve()
{
ll n,k,i,d,x,y;
cin>>n>>k;
int arr[n];
for (i=0;i<n;i++)
    cin>>arr[i];
x=*max_element(arr,arr+n);
//y=*min_element(arr,arr+n);
  for (i=0;i<n;i++){
    arr[i]=x-arr[i];
  }
  for (i=0;i<n;i++)
    cout<<arr[i]<<" ";
   cout<<endl;
}
int main()
{
    FastIO;
    int t;
    cin>>t;
    while (t--)
        solve();
    return 0;
}


I=input
for _ in[0]*int(I()):
 I();m=-1e9;l=0
 for x in map(int,I().split()):m=max(m,x);l=max(l,m-x)
 print(l and len(f'{l:b}'))


for _ in range(int(input())):
    n  = int(input())
    ar = list(map(int,input().split()))
    flag = True
    for i in range(n-1):
        if ar[i]>ar[i+1]:
            flag = False
    if flag:
        print(0)
        continue
    time = 0
    maxSoFar = ar[0]
    diff = 0
    for i in range(n):
        if ar[i]>maxSoFar:
            maxSoFar = ar[i]
        else:
            diff=max(diff,maxSoFar-ar[i])
    while time*(time+1)//2<diff:
        time += 1
    print(time)


for _ in range(int(input())):
    n  = int(input())
    ar = list(map(int,input().split()))
    flag = True
    for i in range(n-1):
        if ar[i]>ar[i+1]:
            flag = False
    if flag:
        print(0)
        continue
    time = 0
    maxSoFar = ar[0]
    diff = 0
    for i in range(n):
        if ar[i]>maxSoFar:
            maxSoFar = ar[i]
        else:
            diff=max(diff,maxSoFar-ar[i])
    while 2**time<=diff:
        time += 1
    print(time)


import sys
input=sys.stdin.readline

t=int(input())
import math
for i1 in range(t):
    n=int(input())
    l=list(map(int,input().split(" ")))
    x=0
    m=l[-1]
    suff=[0]*n
    for i in range(n-2,-1,-1):
        suff[i]=m
        m=min(m,l[i])
    max1=0
    for i in range(n-1):
        if(l[i]>suff[i]):
            max1=max(abs(l[i]-suff[i]),max1)

    if(max1>0):
        b=math.log(max1,2)
        if(b==math.ceil(b)):
            b+=1
        else:
            b=math.ceil(b)

        x=b
    # print(l)
    print(int(x))


import sys
input=sys.stdin.readline

t=int(input())
import math
for i1 in range(t):
    n=int(input())
    l=list(map(int,input().split(" ")))
    x=0
    for i in range(1,n):
        # if(x==0):
        if(l[i]<l[i-1] and x==0):
            diff=l[i-1]-l[i]
            b=math.log(diff,2)
            if(b==math.ceil(b)):
                b+=1
            else:
                b=math.ceil(b)
            z=math.pow(2,b)-1
            l[i]+=z
            x=b
        elif(l[i]<l[i-1] and x!=0):
            z=l[i]+math.pow(2,x)-1
            if(z<l[i-1]):
                diff=l[i-1]-l[i]
                b=math.log(diff,2)
                if(b==math.ceil(b)):
                    b+=1
                else:
                    b=math.ceil(b)
                z=math.pow(2,b)-1
                l[i]+=z
                x=b
            else:
                l[i]=z

        else:
            l[i]+=math.pow(2,x)-1

    print(int(x))


import sys
input=sys.stdin.readline

t=int(input())
import math
for i1 in range(t):
    n=int(input())
    l=list(map(int,input().split(" ")))
    x=0
    for i in range(1,n):
        # if(x==0):
        if(l[i]<l[i-1] and x==0):
            diff=l[i-1]-l[i]
            b=math.log(diff,2)
            if(b==math.ceil(b)):
                b+=1
            else:
                b=math.ceil(b)
            z=math.pow(2,b)-1
            l[i]+=z
            x=b
        elif(l[i]<l[i-1] and x!=0):
            z=l[i]+math.pow(2,x)-1
            if(l[i]<l[i-1]):
                diff=l[i-1]-l[i]
                b=math.log(diff,2)
                if(b==math.ceil(b)):
                    b+=1
                else:
                    b=math.ceil(b)
                z=math.pow(2,b)-1
                l[i]+=z
                x=b
            else:
                l[i]=z

        else:
            l[i]+=math.pow(2,x)-1

    print(int(x))


import sys
input=sys.stdin.readline

t=int(input())
import math
for i1 in range(t):
    n=int(input())
    l=list(map(int,input().split(" ")))
    x=0
    for i in range(1,n):
        # if(x==0):
        if(l[i]<l[i-1] and x==0):
            diff=l[i-1]-l[i]
            b=math.log(diff,2)
            if(b==math.ceil(b)):
                b+=1
            else:
                b=math.ceil(b)
            z=math.pow(2,b)-1
            l[i]+=z
            x=b
        elif(l[i]<l[i-1] and x!=0):
            z=l[i]+math.pow(2,x)-1
            if(l[i]<l[i-1]):
                diff=l[i-1]-l[i]
                b=math.log(diff,2)
                if(b==math.ceil(b)):
                    b+=1
                else:
                    b=math.ceil(b)
                z=math.pow(2,b)-1
                l[i]+=z
                x=b

        else:
            l[i]+=math.pow(2,x)-1

    print(int(x))


numcases = int(input())

def isNonDecreasing():
    dindex = []
    a = 0
    b = 0
    for i in range(listlength - 1):
        a = declist[i]
        b = declist[i + 1]
        if a > b:
            dindex.append(i + 1)
    return dindex

def solve():
    count = 0
    dindex = []
    elapsed = [0]*listlength
    while True:
        dindex = isNonDecreasing()
        if len(dindex) == 0:
            break
        else:
            for i in range(len(dindex)):
                elapsed[i] = elapsed[i] + 1
                declist[dindex[i]] = declist[dindex[i]] + 2**(elapsed[i] - 1)
    for i in range(listlength):
        if elapsed[i] > count:
            count = elapsed[i]
    print(count)

for j in range(numcases):
    listlength = int(input())
    declist = list(map(int, input().split(' ')))
    solve()


numcases = int(input())

def isNonDecreasing():
    dindex = []
    a = 0
    b = 0
    for i in range(listlength - 1):
        a = declist[i]
        b = declist[i + 1]
        if a > b:
            dindex.append(i + 1)
    return dindex

def solve():
    count = 0
    dindex = []
    elapsed = [0]*listlength
    while True:
        dindex = isNonDecreasing()
        if len(dindex) == 0:
            break
        else:
            for i in range(len(dindex)):
                elapsed[dindex[i]] = elapsed[dindex[i]] + 1
                declist[dindex[i]] = declist[dindex[i]] + 2**(elapsed[i] - 1)
    print(elapsed)
    for i in range(listlength):
        if elapsed[i] > count:
            count = elapsed[i]
    print(count)

for j in range(numcases):
    listlength = int(input())
    declist = list(map(int, input().split(' ')))
    solve()


numcases = int(input())

def isNonDecreasing(index):
    a = declist[index]
    b = declist[index + 1]
    if a > b:
        return 1
    else:
        return 0

def solve():
    count = 0
    amountdecreasing = 0
    elapsed = [0]*listlength
    for j in range(listlength - 1):
        if isNonDecreasing(j) == 1:
            amountdecreasing = declist[j] - declist[j + 1]
            declist[j + 1] = declist[j + 1] + amountdecreasing
            amountdecreasing = bin(amountdecreasing)
            elapsed[j] = elapsed[j] + len(amountdecreasing) - 2
    for i in range(listlength):
        if elapsed[i] > count:
            count = elapsed[i]
    print(count)

for j in range(numcases):
    listlength = int(input())
    declist = list(map(int, input().split(' ')))
    solve()


numcases = int(input())

def isNonDecreasing():
    dindex = []
    a = 0
    b = 0
    for i in range(listlength - 1):
        a = declist[i]
        b = declist[i + 1]
        if a > b:
            dindex.append(i + 1)
    return dindex

def solve():
    count = 0
    dindex = []
    elapsed = [0]*listlength
    while True:
        dindex = isNonDecreasing()
        if len(dindex) == 0:
            break
        else:
            for i in range(len(dindex)):
                elapsed[dindex[i]] = elapsed[dindex[i]] + 1
                declist[dindex[i]] = declist[dindex[i]] + 2**(elapsed[dindex[i]] - 1)
    for i in range(listlength):
        if elapsed[i] > count:
            count = elapsed[i]
    print(count)

for j in range(numcases):
    listlength = int(input())
    declist = list(map(int, input().split(' ')))
    solve()


numcases = int(input())

def isNonDecreasing():
    dindex = []
    a = 0
    b = 0
    for i in range(listlength - 1):
        a = declist[i]
        b = declist[i + 1]
        if a > b:
            dindex.append(i + 1)
    return dindex

def solve():
    count = 0
    dindex = []
    while True:
        dindex = isNonDecreasing()
        if len(dindex) == 0:
            break
        else:
            count = count + 1
            for i in range(len(dindex)):
                declist[dindex[i]] = declist[dindex[i]] + 2**(count - 1)
    print(count)

for j in range(numcases):
    listlength = int(input())
    declist = list(map(int, input().split(' ')))
    solve()


import math
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().strip().split()))
    m=0
    for i in range(1,len(arr)):
        if(arr[i-1]>arr[i]):
            val=arr[i-1]-arr[i]
            k=math.ceil(math.log2(val +1 ))
            if(pow(2,k-1)>=val):
                arr[i]=arr[i] + pow(2,k-1)
            else:
                arr[i]=arr[i] + int(pow(2,k)) -1

            if(m<k):
                m=k;

    print(m)


import math
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().strip().split()))
    m=0
    for i in range(1,len(arr)):
        if(arr[i-1]>arr[i]):
            val=arr[i-1]-arr[i]
            k=math.floor(math.log2(val))
            arr[i]=arr[i-1]
            if(m<k):
                m=k

    print(m+1)


import math
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().strip().split()))
    m=0
    for i in range(1,len(arr)):
        if(arr[i-1]>arr[i]):
            val=arr[i-1]-arr[i]
            k=math.ceil(math.log2(val))
            arr[i]=arr[i-1]
            if(m<k):
                m=k

    print(m)


import math
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().strip().split()))
    m=-1
    for i in range(1,len(arr)):
        if(arr[i-1]>arr[i]):
            val=arr[i-1]-arr[i]
            k=math.floor(math.log2(val))
            arr[i]=arr[i-1]
            if(m<k):
                m=k
    if(m==-1):
        print(0)
    else:
        print(m+1)


import math
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().strip().split()))
    m=0
    for i in range(1,len(arr)):
        if(arr[i-1]>arr[i]):
            val=arr[i-1]-arr[i]
            k=math.ceil(math.log2(val +1 ))
            arr[i]=arr[i]+ int(pow(2,k)) -1
            if(m<k):
                m=k;

    print(m)


T = int(input())

for t in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    last_val = a[0]
    max_x = 0
    for i in range(1, len(a)):
        if a[i] < last_val:
            x = 1
            add_on = 1
            while True:
                new_val = a[i] + add_on
                if new_val >= last_val:
                    last_val = new_val
                    break
                add_on += (1 << x)
                x += 1
            max_x = max(max_x, x)
        else:
            last_val = a[i]

    print(max_x)


T = int(input())

for t in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    last_val = a[0]
    max_x = 0
    for i in range(1, len(a)):
        if a[i] < last_val:
            diff = last_val - a[i]
            x = 1
            while True:
                new_val = a[i] + (1 << x) - 1
                if new_val >= last_val:
                    last_val = max(new_val, last_val)
                    break
                x += 1
            max_x = max(max_x, x)
        else:
            last_val = a[i]

    print(max_x)


T = int(input())

import math

for t in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    max_diff = 0
    last = a[0]
    for i in range(1, len(a)):
        if a[i] < last:
            diff = last - a[i]
            max_diff = max(diff, max_diff)
        else:
            last = a[i]

    if max_diff == 0:
        print(0)
    else:
        res = int(math.log2(max_diff)) + 1

        print(res)


T = int(input())

for t in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    last_val = a[0]
    max_x = 0
    for i in range(1, len(a)):
        if a[i] < last_val:
            diff = last_val - a[i]
            x = 1
            add_on = 1
            while True:
                new_val = a[i] + add_on
                if new_val >= last_val:
                    last_val = max(new_val, last_val)
                    break
                add_on += (1 << x)
                x += 1
            max_x = max(max_x, x)
        else:
            last_val = a[i]

    print(max_x)


T = int(input())

for t in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    last_val = a[0]
    max_x = 0
    for i in range(1, len(a)):
        if a[i] < last_val:
            x = 1
            add_on = 1
            while True:
                new_val = a[i] + add_on
                if new_val >= last_val:
                    last_val = max(new_val, last_val)
                    break
                add_on += (1 << x)
                x += 1
            max_x = max(max_x, x)
        else:
            last_val = a[i]

    print(max_x)


testCases= int(input())
for _ in range(testCases):

    maximum= -1e9
    time= 0
    input()

    a= map(int, input().split())
    for x in a:
        maximum= max(maximum, x)
        time= max(time, maximum- x)

    print(time)


I=input
for _ in range(int(I())):
 I();m=-1e9;l=0
 for x in map(int,I().split()):m=max(m,x);l=max(l,m-x)
 print(l and len(f'{l:b}'))


def countSetBits(num):
    binary = bin(num)
    setBits = [ones for ones in binary[2:] if ones == '1']

    return len(setBits)

tt = int(input())
while tt:
    n = int(input())
    A = [int(x) for x in input().split()]
    temp = A[0]
    maxx = temp
    for i in range(1,n):
        if A[i]>temp:
            temp = A[i]
            if temp>maxx:
                maxx = temp
        else:
            break
    minn = temp
    while i<n:
        if A[i] < minn:
            minn = A[i]
        i += 1
    #print(maxx - minn)
    if abs(maxx - minn) == 0:
        print(0)
    else:
        print(int(abs(maxx - minn)/2)+1)
    tt -= 1


def countSetBits(num):
    binary = bin(num)
    binary = binary[2:]
    if binary == "0":
        return 0
    return len(binary)

#1 7 6 5 9 3
tt = int(input())
while tt:
    n = int(input())
    A = [int(x) for x in input().split()]
    temp = A[0]
    maxx = temp
    ans = 0
    for i in range(1, n):
        if A[i] > temp:
            temp = A[i]
            maxx = temp
        else:
            #print("Maxx = ",maxx)
            ans = max(abs(maxx - A[i]), ans)

    #print(ans)
    print(countSetBits(ans))
    tt -= 1


def countSetBits(num):
    binary = bin(num)
    binary = binary[2:]
    return len(binary)

print(countSetBits(32))

tt = int(input())
while tt:
    n = int(input())
    A = [int(x) for x in input().split()]
    temp = A[0]
    maxx = temp
    for i in range(1,n):
        if A[i]>temp:
            temp = A[i]
            if temp>maxx:
                maxx = temp
        else:
            break
    minn = temp
    while i<n:
        if A[i] < minn:
            minn = A[i]
        i += 1
    #print(maxx - minn)
    if abs(maxx - minn) == 0:
        print(0)
    else:
        print(countSetBits(abs(maxx - minn)))
    tt -= 1


def countSetBits(num):
    binary = bin(num)
    binary = binary[2:]
    return len(binary)

tt = int(input())
while tt:
    n = int(input())
    A = [int(x) for x in input().split()]
    temp = A[0]
    maxx = temp
    for i in range(1,n):
        if A[i]>temp:
            temp = A[i]
            if temp>maxx:
                maxx = temp
        else:
            break
    minn = temp
    while i<n:
        if A[i] < minn:
            minn = A[i]
        i += 1
    #print(maxx - minn)
    if abs(maxx - minn) == 0:
        print(0)
    else:
        print(countSetBits(abs(maxx - minn)))
    tt -= 1


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = True
    t, x = 0, 1
    while flag:
        flag2 = 1
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                print(arr[i+1], arr[i])
                arr[i+1]+=int(2**(x-1))
                flag2=0
                break
        if flag2==1:
            flag=False
            break
        x+=1
        t+=1
        print(t)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = True
    t, x = 0, 1
    while flag:
        flag2 = 1
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                arr[i+1]+=int(2**(x-1))
                flag2=0
        if flag2==1:
            flag=False
        x+=1
        t+=1
    print(t=1)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    m = arr[0]
    x = 0
    add = 0
    for a in arr:
        while m>a+add:
            x+=1
            add+=2**(x-1)
        m=max(a, m)
    print(x)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = True
    t, x = 0, 1
    while flag:
        flag2 = 1
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                arr[i+1]+=int(2**(x-1))
                flag2=0
                break
        if flag2==1:
            flag=False
            break
        x+=1
        t+=1
        print(t)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = True
    t, x = 0, 1
    while flag:
        flag2 = 1
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                arr[i+1]+=int(2**(x-1))
                flag2=0
                break
        if flag2==1:
            flag=False
            break
        x+=1
        t+=1
    print(t)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = True
    t, x = 0, 1
    while flag:
        flag2 = 1
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                arr[i+1]+=int(2**(x-1))
                flag2=0
        if flag2==1:
            flag=False
            break
        x+=1
        t+=1
    print(t)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    flag = True
    t, x = 0, 1
    while flag:
        flag2 = 1
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                arr[i+1]+=int(2**(x-1))
                flag2=0
        if flag2==1:
            flag=False
        x+=1
        t+=1
    print(t-1)


import math
t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    m = -float('INF')
    count = 0
    flag = 0

    for i in range(1,n):
        if a[i]<a[i-1]:
            if m< a[i-1] - a[i]:
                m = a[i-1] - a[i]
                #print(m)
                count = 0
                flag = 1
            elif flag:
                count += 1

    v = 0
    if m != -float('INF'):
        v = math.ceil(math.log(abs(m),2))
        #print(v,m)
        print(count+ v+1)
    else:
        print(count)


import math
t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    m = 0
    max_so_far = a[0]

    for i in range(1,n):
        if a[i]<a[i-1]:

            max_so_far = max(max_so_far,a[i-1])
            m = max(m,int(math.log2(max_so_far-a[i])) + 1)

    print(m)


t = int(input())

for _ in range(t):
    n = int(input())
    print(n)


for T in range(int(input())) :
    n =int(input())
    arr = list(map(int,input().split()))

    maxi = float('-inf')
    diff  = 0

    for i in range(n) :
        if arr[i] > maxi :
            maxi = arr[i]
        else :
            if abs(maxi-arr[i]) >diff :
                diff = abs(maxi - arr[i])

    x = 0
    i =1
    count =0
    while x < diff :
        x += i
        i *=2
        count +=1
    print(count)


dp = [0]*(2*(10**9) +1)
dp[1] = 1
for i in  range(2,2*(10**9) +1) :
    dp[i]  = dp[i-1] + i

def binary_search(arr, k,l,r) :
    while r-l >1 :
        mid = l + (r-l)//2
        if arr[mid] == k:
            return mid
        elif arr[mid] > k :
            r = mid
        else :
            l = mid
    return r

for T in range(int(input())) :
    n =int(input())
    arr = list(map(int,input().split()))

    maxi = float('-inf')
    diff  = 0
    print(arr)

    for i in range(n) :
        if arr[i] > maxi :
            maxi = arr[i]
        else :
            if abs(maxi-arr[i]) >diff :
                diff = abs(maxi - arr[i])

    ans = binary_search(dp,diff,0,2*(10**9))
    print(ans)


for T in range(int(input())) :
    n =int(input())
    arr = list(map(int,input().split()))

    maxi = float('-inf')
    diff  = 0

    for i in range(n) :
        if arr[i] > maxi :
            maxi = arr[i]
        else :
            if abs(maxi-arr[i]) >diff :
                diff = abs(maxi - arr[i])

    x = 0
    i =1
    while x < diff :
        x += i
        i +=1
    print(i-1)


import sys
from functools import reduce
import math
from itertools import groupby

def I(): return int(input())
def IL(): return list(map(int,input().split(' ')))
def solve(n,l):
	# ll = [ l[i-1]-l[i] for i in range(1,len(l)) if l[i-1]-l[i]>0 ]
	# print(l)
	# print(ll)
	# ll.sort()
	maxx = 0
	cnt = 0
	for i in range(1,len(l)):
		if l[i-1] > l[i] :
			# print(l[i],l[i-1],i,maxx)
			maxx = max(maxx,l[i-1]-l[i])
			l[i] = l[i-1]

	while(maxx>0):
		cnt+=1
		maxx= maxx//2
	return cnt
if __name__ == "__main__":
	t = int(input())
	while(t>0):
		n = I()
		l = IL()
		ans = solve(n,l)
		print(ans)
		t -= 1


import sys
from functools import reduce
import math
from itertools import groupby

def I(): return int(input())
def IL(): return list(map(int,input().split(' ')))
def solve(n,l):
	ll = [ l[i-1]-l[i] for i in range(1,len(l)) if l[i-1]-l[i]>0 ]
	# print(l)
	# print(ll)
	ll.sort()
	x = 1; cnt = 0
	for i in range(0,len(ll)):
		tt = 0
		# print("ll: ",ll[i])
		if x < ll[i]:
			while tt<ll[i] :
				tt+=x
				cnt+=1
				x*=2
				# print("tt: ",tt,x)
		else :
			cnt+=1
			x*=2
	return cnt
if __name__ == "__main__":
	t = int(input())
	while(t>0):
		n = I()
		l = IL()
		ans = solve(n,l)
		print(ans)
		t -= 1


import sys
from functools import reduce
import math
from itertools import groupby

def I(): return int(input())
def IL(): return list(map(int,input().split(' ')))
def solve(n,l):
	ll = [ l[i-1]-l[i] for i in range(1,len(l)) if l[i-1]-l[i]>0 ]
	# print(l)
	# print(ll)
	ll.sort()
	x = 1; cnt = 0
	for i in range(0,len(ll)):
		while x<ll[i] :
			cnt+=1
			x+=1
			x*=2
			x-=1
		cnt+=1
		x+=1
		x*=2
		x-=1
	return cnt
if __name__ == "__main__":
	t = int(input())
	while(t>0):
		n = I()
		l = IL()
		ans = solve(n,l)
		print(ans)
		t -= 1


import math

for _ in range (int(input())):
    n = int(input())
    ar = [i for i in map(int, input().split())]
    # ar = [map(int, input().split())]

    # ar =list(map(int,input().split()))
    # x = map(int, input().split())
    if n <= 1:
    #    print('0')
        continue
    ans = 0
    for i in range (1, n):
        if ar[i] < ar[i-1]:
            # print('hola')
            lg = int(math.log2(ar[i-1] - ar[i]))
            if ar[i] + (1 << lg) >= ar[i-1]:
                ar[i] += (1 << lg)
                ans = max (ans, lg)
                #print('h1', lg)
            else:
                ar[i] += (1 << (lg+1))
                ans = max (ans, lg+1)
                #print('h2', lg+1)
    #    print(ar)

    # print(ar)
    print(ans+1 if ans > 0 else ans)


import math

for _ in range (int(input())):
    n = int(input())
    ar = [i for i in map(int, input().split())]

    if n <= 1:
        print('0')
        continue
    ans = 0
    for i in range (1, n):
        if ar[i] < ar[i-1]:
            diff = ar[i-1] - ar[i]
            ar[i] = ar[i-1]
            if (diff > (1 << ans)):
                lg = int(math.ceil(math.log2(diff)))
                if (1 << lg) - 1 >= diff:
                    lg -= 1
                    ans = max (lg, ans)
                else:
                    ans = max (lg, ans)

    print(ans+1 if ans > 0 else 0)


import math

for _ in range (int(input())):
    n = int(input())
    ar = [i for i in map(int, input().split())]

    if n <= 1:
        print('0')
        continue
    ans = 0
    for i in range (1, n):
        if ar[i] < ar[i-1]:
            diff = ar[i-1] - ar[i]
            lg = int(math.log2(diff))
            if (1 << lg) - 1 >= diff:
                ar[i] += (1 << lg) - 1
                ans = max (ans, lg)
            else:
                ar[i] += (1 << lg)
                ans = max (ans, lg+1)

    print(ans)


import math

for _ in range (int(input())):
    n = int(input())
    ar = [i for i in map(int, input().split())]

    if n <= 1:
        print('0')
        continue
    ans = 0
    for i in range (1, n):
        if ar[i] < ar[i-1]:
            diff = ar[i-1] - ar[i]
            lg = int(math.ceil(math.log2(diff)))
            if (1 << lg) - 1 >= diff:
                # print('h1')
                ar[i] += (1 << lg) - 1
                ans = max (ans, lg)
            else:
                # print('h2')
                ar[i] += (1 << lg)
                ans = max (ans, lg+1)
        # print(i, ans)

    print(ans)


import math

for _ in range (int(input())):
    n = int(input())
    ar = [i for i in map(int, input().split())]

    if n <= 1:
        print('0')
        continue
    f = False
    ans = 0
    for i in range (1, n):
        if ar[i] < ar[i-1]:
            f = True
            diff = ar[i-1] - ar[i]
            ar[i] = ar[i-1]
            if (diff > (1 << ans)):
                lg = int(math.ceil(math.log2(diff)))
                if (1 << lg) - 1 >= diff:
                    lg -= 1
                    ans = max (lg, ans)
                else:
                    ans = max (lg, ans)

    print(ans+1 if f else 0)
    # print(ans+1 if ans > 0 else 0)


import math
for _ in range(int(input())):
    am = int(input())
    arr = list(map(int,input().split()))
    arrN = [0]
    s = 0

    for i in range(1,am):
        arrN.append(max(0,arr[i-1]-arr[i]+arrN[i-1]))
    if max(arrN) == 0:
        print(0)
        continue
    got = 0
    m = max(arrN)
    i = 0
    while got < m:
        got += 2**i
        i+=1
    print(i)


import math
for _ in range(int(input())):
    am = int(input())
    arr = list(map(int,input().split()))
    arrN = [0]
    s = 0

    for i in range(1,am):
        arrN.append(max(0,arr[i-1]-arr[i]+arrN[i-1]))
    if max(arrN) == 0:
        print(0)
        continue
    print(math.ceil(math.log2(max(arrN)))+1)


import math

for _ in range(int(input())):
	n = int(input())
	l = list(map(int,input().split()))
	ans = 0
	prev = -float('inf')
	for i in range(n):
		if l[i] < prev:
			diff = prev - l[i]
			diff += 1
			power = math.ceil(math.log2(diff))
			ans = max(ans,power)
			prev = l[i]+pow(2,power)
		else:
			prev = l[i]

	print(ans)


import math
for _ in range(int(sys.stdin.readline())):
	n = int(input())
	l = list(map(int,input().split()))
	ans = 0
	prev = -float('inf')
	for i in range(n):
		if l[i] < prev:
			diff = prev - l[i]
			diff += 1
			power = math.ceil(math.log2(diff))
			ans = max(ans,power)
		else:
			prev = l[i]
	print(ans)


import math
for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    ans = 0
    prev = -float('inf')
    for i in range(n):
        if l[i] < prev:
            diff = prev - l[i]
            diff += 1
            power = math.ceil(math.log2(diff))
            ans = max(ans,power)
        else:
            prev = l[i]
    print(ans)


import math
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    used = [0 for i in range(40)]
    for i in range(1, n):
        if (a[i] < a[i-1]):
            diff = a[i-1] - a[i]
            need =  int(math.log(diff,2))+1
            for i in range(need, 40):
                if (not(used[i])):
                    ans = max(ans,i )
                    used[i] = 1
                    break
    print(ans)


import math
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    used = [0 for i in range(40)]
    a2 = [a[0]]
    for i in range(1, n):
        if (a[i] < a2[i-1]):
            a2.append(a2[i-1])
        else:
            a2.append(a[i])
    for i in range(n):
        if (a2[i] == a[i]):
            continue
        ans = max(int(math.log(a2[i]-a[i],2))+1, ans)

    print(ans)


import math
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    maxi = 0
    for i in range(1, n):
        if (a[i] < a[i-1]):
            diff = a[i-1] - a[i]
            ans  = ans + max(1, int(math.log(diff,2))+1 - ans)
    print(ans)


import sys
def binarySearch(arr, l, r, x):
    ans=-1
    while l <= r:

        mid = l + (r - l)//2;

        # Check if x is present at mid
        if arr[mid] == x:
            ans=mid
        # If x is greater, ignore left half
        elif arr[mid] < x:
            l = mid + 1

        # If x is smaller, ignore right half
        else:
            ans=mid
            r = mid - 1

    # If we reach here, then the element was not present
    return ans
t=int(sys.stdin.readline())
for _ in range(t):
    n=int(sys.stdin.readline())
    l=list(map(int,sys.stdin.readline().split()))
    maxx=l[0]
    ind=0
    for i in range(1,n):
        if l[i]>maxx:
            maxx=l[i]
            ind=i
    k=0
    for i in range(ind+1,n):
        k=max(k,l[ind]-l[i])
    if k==0:
        print(0)
    else:
        a=[1]
        for i in range(1,32):
            a.append(2**i+a[-1])
        ans=binarySearch(a,0,len(a)-1,k)
        print(ans+1)


import sys
def binarySearch(arr, l, r, x):
    ans=-1
    while l <= r:

        mid = l + (r - l)//2;

        # Check if x is present at mid
        if arr[mid] == x:
            ans=mid
        # If x is greater, ignore left half
        elif arr[mid] < x:
            l = mid + 1

        # If x is smaller, ignore right half
        else:
            ans=mid
            r = mid - 1

    # If we reach here, then the element was not present
    return ans
t=int(sys.stdin.readline())
for _ in range(t):
    n=int(sys.stdin.readline())
    l=list(map(int,sys.stdin.readline().split()))
    maxx=l[0]
    ind=0
    for i in range(1,n):
        if l[i]>maxx:
            maxx=l[i]
            ind=i
    k=0
    for i in range(ind+1,n):
        k=max(k,l[ind]-l[i])
    if k==0:
        print(0)
    else:
        a=[1]
        for i in range(1,64):
            a.append(2**i+a[-1])
        ans=binarySearch(a,0,len(a)-1,k)
        print(ans+1)


import sys,math
t=int(sys.stdin.readline())
for _ in range(t):
    n=int(sys.stdin.readline())
    l=list(map(int,sys.stdin.readline().split()))
    maxx=l[0]
    ind=0
    ans=[]
    req=[0]*n
    for i in range(1,n):
        if l[i]<=l[i-1]:
            ans.append(max(maxx,l[i-1])-l[i])
            maxx=max(maxx,l[i-1])
            req[i]=l[i-1]-l[i]
        else:
            maxx=max(maxx,l[i])
    k=-1
    for i in range(ind+1,n):
        k=max(k,l[ind]-l[i])
    if len(ans)==0 :
        print(0)
    else:
        mx=max(ans)
        print(math.ceil(math.log2(mx+1)))


import math
t = int(input())
for _ in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]
    best = 0
    for i in range(1 , n):
        if arr[i] >= arr[i-1]:
            continue
        temp = arr[i-1] - arr[i]
        other = math.ceil(math.log2(temp))
        arr[i] += 2**other
        best = max(other+1 , best)
    print(best)


import math
t = int(input())
acum = [1]*70
acum[0] = 0
for i in range(2 , 70):
    acum[i] = acum[i-1] + 2**(i-1)
for _ in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]
    best = 0
    for i in range(1 , n):
        if arr[i] >= arr[i-1]:
            continue
        temp = arr[i-1] - arr[i]
        other = math.floor(math.log2(temp))
        sum = 0
        j = 0
        while (1 << j) <= temp:
            if temp & (1 << j):
                sum += 2**j
            j += 1
        arr[i] += sum
        best = max(other+1, best)
    # print(arr , '----------------')
    print(best)


import math
t = int(input())
acum = [1]*70
for i in range(1 , 70):
    acum[i] = acum[i-1] + 2**i
for _ in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]
    maxi = 0
    for i in range(1 , n):
        if arr[i] >= arr[i-1]:
            continue
        temp = arr[i-1] - arr[i]
        maxi = max(temp , maxi)
        arr[i] = arr[i-1]
    print(math.floor(math.log2(maxi) + 1) if maxi > 0 else '0')


t = int(input())
for test in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    deg = 0
    value = 0
    st = 0
    if n == 1:
        print(0)
    else:
        for i in range(n-1):
            if (arr[i+1] < arr[i]):
                sep = abs(arr[i] - arr[i+1])
                while (sep > value):
                    if value == 0:
                        value += 1
                        deg += 1
                        st += 1
                    else:
                        st *= 2
                        value += st
                        deg += 1
                arr[i+1] = arr[i]
        print(deg)


t = int(input())
for test in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    deg = 0
    value = 0
    if n == 1:
        print(0)
    else:
        for i in range(n-1):
            if (arr[i+1] < arr[i]):
                sep = abs(arr[i] - arr[i+1])
                while (sep > value):
                    if value == 0:
                        value += 1
                        deg += 1
                    else:
                        value *= 2
                        deg += 1
                arr[i+1] = arr[i]
        print(deg)


t = int(input())
for test in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    deg = 0
    value = 0
    if n <= 1:
        print(0)
    else:
        for i in range(n-1):
            if (arr[i+1] < arr[i]):
                sep = arr[i] - arr[i+1]
                while (sep > value):
                    if value == 0:
                        value += 1
                        deg += 1
                    else:
                        value *= 2
                        deg += 1
                arr[i+1] = arr[i]
    print(deg)


t = int(input())
for test in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    deg = 0
    value = 0
    if n <= 1:
        print(0)
    else:
        for i in range(n-1):
            if (arr[i+1] < arr[i]):
                sep = arr[i] - arr[i+1]
                while (sep > value):
                    if value == 0:
                        value += 1
                        deg += 1
                    else:
                        value *= 2
                        deg += 1
                arr[i+1] = arr[i]
        print(deg)


from math import floor,log
for _ in range(int(input())):
	n=int(input())
	l=list(map(int,input().split()))
	r=[0]
	for i in range(1,n):
		if l[i-1]>l[i]:
			t=l[i-1]+r[i-1]-l[i]
			r.append(t)
		else:
			r.append(0)
	if max(r)>0:
		print(floor(log(max(r),2))+1)
	else:
		print("0")


from math import log2
for _ in range(int(input())):
	n=int(input())
	l=list(map(int,input().split()))
	r=[0]
	for i in range(1,n):
		if l[i-1]>l[i]:
			t=l[i-1]+r[i-1]-l[i]
			r.append(t)
		else:
			r.append(0)
	if max(r)>0:
		print(int(log2(max(r)))+1)
	else:
		print("0")


from math import log2
for _ in range(int(input())):
	n=int(input())
	l=list(map(int,input().split()))
	ans=0
	for i in range(1,n):
		if l[i-1]>l[i]:
			ans=max(ans,int(log2(l[i-1]-l[i]))+1)
			l[i]=l[i-1]
	print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int,input().split(' ')))
    ans = 0
    def cal(num):
        res = 0
        while num>0:
            res += 1
            num >>= 1
        return res
    for i in range(1,len(arr)):
        if arr[i]<arr[i-1]:
            tmp = cal(arr[i-1]-arr[i])
            ans = max(ans,tmp)
            arr[i] = arr[i-1]+2**(tmp-1)
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int,input().split(' ')))
    ans = 0
    def cal(num):
        res = 0
        while num>0:
            res += 1
            num >>= 1
        return res
    for i in range(1,len(arr)):
        if arr[i]<arr[i-1]:

            ans = max(ans,cal(arr[i-1]-arr[i]))
            arr[i] = arr[i-1]+2**(ans-1)
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int,input().split(' ')))
    ans = 0
    def cal(num):
        res = 0
        while num>0:
            res += 1
            num >>= 1
        return res
    for i in range(1,len(arr)):
        if arr[i]<arr[i-1]:
            tmp = cal(arr[i-1]-arr[i])
            ans = max(ans,tmp)
            arr[i] = arr[i]+2**(tmp-1)
    print(ans)


def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        nums = list(map(int, input().split()))
        if n == 1:
            print(0)
        else:
            ans = 0
            def cal(x):
                res = 0
                while x:
                    res += 1
                    x >>= 1
                return res
            for i in range(1, n):
                if nums[i] >= nums[i-1]:
                    continue
                diff = nums[i-1] - nums[i]
                ans = max(ans, cal(diff))
                nums[i] = nums[i-1]
            print(ans)

if __name__ == "__main__":
    main()


t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int,input().split(' ')))
    ans = 0
    def cal(num):
        res = 0
        while num>0:
            res += 1
            num >>= 1
        return res
    for i in range(1,len(arr)):
        if arr[i]<arr[i-1]:
            ans += cal(arr[i-1]-arr[i])
    print(ans)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))
    flag = False
    I = -1
    for i in range(n-1):
        if arr[i+1] < arr[i]:
            flag = True
            I = i+1
            break

    if not(flag):
        print(0)
    else:
        ans = 1
        maxx = 0
        prev = arr[I-1]
        for i in range(I,n):
            if arr[i] < prev:
                maxx = max(maxx,prev-arr[i])
            else:
                prev = arr[i]
        temp = 1
        while temp < (maxx+1):
            temp += (temp*2)
            ans += 1
        print(ans)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))
    flag = False
    I = -1
    for i in range(n-1):
        if arr[i+1] < arr[i]:
            flag = True
            I = i+1
            break

    if not(flag):
        print(0)
    else:
        ans = 1
        diff = 0
        prev = arr[I-1]
        num1,num2 = 0,0
        for i in range(I,n):
            if arr[i] < prev:
                if diff < prev-arr[i]:
                    diff = prev-arr[i]
                    num1 = arr[i]
                    num2 = prev
            else:
                prev = arr[i]
        temp = 1
        num1 += 1
        while num1 < num2:
            temp = (temp*2)
            ans += 1
            num1 += temp
        print(ans)


def solve(arr):

    sorted = [arr[0]]
    second = 0
    for i in range(1, len(arr)):
        if arr[i] >= sorted[-1]:
            sorted.append(arr[i])

        else:
            ind, num = get_ind(sorted[-1] - arr[i])
            sorted.append(num+arr[i] - 1)
            second = max(ind, second)

    return second

def get_ind(num):
    n = num
    count = 0
    while( n != 0):
        n >>= 1
        count += 1
    x = 1 << count
    small_x = x >> 1
    if small_x > num:
        return count-1, small_x
    else:
        return count, x

m = int(input())
for _ in range(m):
    input()
    arr = list(map(int, input().split()))
    print(solve(arr))


def solve(arr):

    sorted = [arr[0]]
    second = 0
    for i in range(1, len(arr)):
        if arr[i] >= sorted[-1]:
            sorted.append(arr[i])
        else:
            ind = get_ind(sorted[-1] - arr[i])
            sorted.append(sorted[-1])
            second = max(ind, second)

    return second

def get_ind(n):
    count = 0
    while( n != 0):
        n >>= 1
        count += 1
    return count

m = int(input())
for _ in range(m):
    input()
    arr = list(map(int, input().split()))
    print(solve(arr))


import math
t=int(input())
for _  in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    temp=[]
    diff=[]
    c=0
    if l==sorted(l):
        print(0)
    elif l==sorted(l,reverse=True):
        print(int(math.log2(l[0]-l[n-1])) + 1)
    else:
        for i in range(n):
            if i == n - 1:
                if l[i] <= l[i - 1]:
                    diff.append(abs(temp[c - 1] - l[i]))
            elif i==0 :
                if l[i]>=l[i+1]:
                    temp.append(l[i])
                    c=c+1

            else:
                if l[i] >=l[i - 1] and l[i] >= l[i + 1]:
                    if c == 0:
                        temp.append(l[i])
                        c = c + 1

                    else:
                        if l[i] > temp[c - 1]:
                            temp.append(l[i])
                            c = c + 1

                if l[i] <= l[i - 1] and l[i] <= l[i + 1]:

                    diff.append(abs(temp[c - 1] - l[i]))
        m=max(diff)
        print(int(math.log2(m)) + 1)


import math
t=int(input())
for _  in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    temp=[]
    diff=[]
    c=0
    if l==sorted(l):
        print(0)
    elif l==sorted(l,reverse=True):
        print(int(math.log2(l[0]-l[n-1])) + 1)
    else:
        for i in range( n):
            if i == n - 1:
                if l[i] < l[i - 1]:
                    diff.append(abs(temp[c - 1] - l[i]))
            elif i==0 :
                if l[i]>l[i+1]:
                    temp.append(l[i])
                    c=c+1

            else:
                if l[i] > l[i - 1] and l[i] > l[i + 1]:
                    if c == 0:
                        temp.append(l[i])
                        c = c + 1

                    else:
                        if l[i] > temp[c - 1]:
                            temp.append(l[i])
                            c = c + 1

                elif l[i] < l[i - 1] and l[i] < l[i + 1]:

                    diff.append(abs(temp[c - 1] - l[i]))
        m=max(diff)
        print(int(math.log2(m)) + 1)


import math
t=int(input())
for _  in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    temp=[]
    diff=[]
    c=0
    if l==sorted(l):
        print(0)
    elif l==sorted(l,reverse=True):
        print(int(math.log2(l[0]-l[n-1])) + 1)
    else:
        for i in range(1, n):
            if i == n - 1:
                if l[i] < l[i - 1]:
                    diff.append(abs(temp[c - 1] - l[i]))
            else:
                if l[i] > l[i - 1] and l[i] > l[i + 1]:
                    if c == 0:
                        temp.append(l[i])
                        c = c + 1
                    else:
                        if l[i] > temp[c - 1]:
                            temp.append(l[i])
                            c = c + 1
                if l[i] < l[i - 1] and l[i] < l[i + 1]:
                    diff.append(abs(temp[c - 1] - l[i]))
        m=max(diff)
        print(int(math.log2(m)) + 1)


import math
t=int(input())
for _  in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    temp=[]
    diff=[]
    c=0
    if l==sorted(l):
        print(0)
    elif l==sorted(l,reverse=True):
        print(int(math.log2(l[0]-l[n-1])) + 1)
    else:
        for i in range( n):
            if i == n - 1:
                if l[i] < l[i - 1]:
                    diff.append(abs(temp[c - 1] - l[i]))

            else:
                if l[i] > l[i - 1] and l[i] > l[i + 1]:
                    if c == 0:
                        temp.append(l[i])
                        c = c + 1
                    else:
                        if l[i] > temp[c - 1]:
                            temp.append(l[i])
                            c = c + 1
                if l[i] < l[i - 1] and l[i] < l[i + 1]:
                    if c==0:
                        diff.append(abs(l[0]-l[i]))
                    else:
                        diff.append(abs(temp[c - 1] - l[i]))
        m=max(diff)
        print(int(math.log2(m)) + 1)


"""
"""

from math import log2, sqrt

R = lambda: map(int, input().split(" "))

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        s = 0
        m = 0
        total = 0
        it = iter(R())
        prev = next(it)
        for cur in it:
            if prev > cur + total:
                tmp = int(log2(prev - cur)) + 1
                m = max(tmp, m)
                prev = cur + 2 ** tmp - 1 + total
                total = 2 ** m - 1
            else:
                prev = cur
        print(m)
        # if s > 0:
        #     if s == int(s):
        #         print(int(log2(s)))
        #     else:
        #         print(int(log2(s)) + 1)
        # else:
        #     print(0)

if __name__ == "__main__":
    main()


"""
"""

from math import log2, sqrt

R = lambda: list(map(int, input().split(" ")))

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        s = 0
        arr = R()
        prev = arr[0]
        for i in range(1, n):
            cur = arr[i]
            if prev > cur:
                s += log2(prev - cur) + 1
            prev = cur
        print(int(s))

if __name__ == "__main__":
    main()


"""
"""

from math import log2, sqrt

R = lambda: list(map(int, input().split(" ")))

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        s = 0
        arr = R()
        prev = arr[0]
        for i in range(1, n):
            cur = arr[i]
            if prev > cur:
                s += prev - cur
            prev = cur
        print(int(log2(s)) + 1 if s > 0 else 0)

if __name__ == "__main__":
    main()


"""
"""

from math import log2, sqrt

R = lambda: map(int, input().split(" "))

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        arr = R()
        max_diff = 0
        prev = next(arr)
        max_num = prev
        for i in arr:
            if max_num < i:
                max_num = i
            else:
                max_diff = max(max_diff, max_num - i)
        if max_diff == 0:
            print(0)
        else:
            print(len(bin(max_diff)) - 2)

if __name__ == "__main__":
    main()


from bisect import bisect_left

def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

t = int(input())

for w in range(t):

    n = int(input())
    values = [int(i) for i in input().split()]

    orden = values.copy()
    #del orden[0]
    orden.sort()

    # i = 0
    # while i+1 < n and values[i] <= values[i+1]:
    #     i += 1

    maxDiff = 0
    for i in range(n-1):
        diff = values[i] - orden[0]

        if diff > maxDiff:
            maxDiff = diff

        x = index(orden, values[i])
        del orden[x]

    resp = bin(maxDiff)[2:]
    if resp == "0":
        print(0)
    else:
        print(len(resp))


t = int(input())

for w in range(t):

    n = int(input())
    values = [int(i) for i in input().split()]

    i = 0
    while i+1 < n and values[i] <= values[i+1]:
        i += 1
    maxDiff = 0

    for k in range(i, n-1):
        diff = values[k] - min(values[k+1:])

        if diff > maxDiff:
            maxDiff = diff

    resp = bin(maxDiff)[2:]
    if resp == "0":
        print(0)
    else:
        print(len(resp))


t = int(input())

for w in range(t):

    n = int(input())
    values = [int(i) for i in input().split()]

    i = 0
    while i+1 < n and values[i] <= values[i+1]:
        i += 1
    maxDiff = 0

    for k in range(i, n-1):
        diff = values[k] - min(values[i+1:])

        if diff > maxDiff:
            maxDiff = diff

    resp = bin(maxDiff)[2:]
    if resp == "0":
        print(0)
    else:
        print(len(resp))


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    maxi=l[0]
    diff=0
    for i in range(1,n):
        diff=max(diff,max-l[i])
        maxi=max(max,l[i])
    ans=0
    while(diff>0):
        diff=diff//2
        ans+=1
    print(ans)


t=int(input())
for _ in range(t):
    maxi=l[0]
    diff=0
    for i in range(1,n):
        diff=max(diff,max-l[i])
        maxi=max(max,l[i])
    ans=0
    while(diff>0):
        diff=diff//2
        ans+=1
    print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    maxi=l[0]
    diff=0
    for i in range(1,n):
        diff=max(diff,maxi-l[i])
        maxi=max(maxi,l[i])
    ans=0
    while(diff>0):
        diff=diff//2
        ans+=1
    print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    ans=0
    for i in range(n-1):
        if l[i]>l[i+1]:
            x=1
            while(l[i]>l[i+1]):
                l[i+1]+=2**(x-1)
                x+=1
            if x-1>ans:
                ans=x-1
    print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    p=sorted(l)
    maxi=0
    for i in range(n):
        if p[i]-l[i]<maxi:
            maxi=p[i]-l[i]
    if maxi==0:
        print(0)
    else:
        x=1
        while(maxi<0):
            maxi+=(2**(x-1))
            x+=1
        print(x-1)


import bisect
mem=[1]
for i in range(35):
    mem.append((2**i)+mem[-1])
#print(mem[-1])

for t in range(int(input())):
    n=int(input())
    #a,b=map(int,input().split())
    l=list(map(int,input().split()))
    mx,mn,diff=l[0],l[0],0
    for i in range(1,n):
        mx=max(mx,l[i])
        #print(mx,l[i])
        if l[i]<mx:
            diff=max(diff,mx-l[i])
    #print(diff)
    ans=bisect.bisect_right(mem,diff)
    print(ans)


import bisect
mem=[0,1]
for i in range(1,35):
    x=(2**i)+mem[-1]
    mem.append(x)
#print(mem)

for t in range(int(input())):
    n=int(input())
    #a,b=map(int,input().split())
    l=list(map(int,input().split()))
    mx,mn,diff=l[0],l[0],0
    for i in range(1,n):
        mx=max(mx,l[i])
        #print(mx,l[i])
        if l[i]<mx:
            diff=max(diff,mx-l[i])
    #print(diff)
    ans=bisect.bisect_right(mem,diff)
    if mem[ans-1]==diff:
        print(ans-1)
    else:
        print(ans)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 20:46:47 2020

@author: maruf
"""

import math
for _ in range(int(input())):
    n=int(input())
    a=list(map(int, input().strip().split()))[:n]
    c=0
    for i in range(1,n):
        if a[i]-a[i-1]<0:
            c=max(int(math.log((a[i-1]-a[i]),2))+1,c)
            a[i]=a[i-1]
    print(c)
    '''
    while(a!=sorted(a)):
        for i in range(n-1):
            if(a[i+1]<a[i]):
                a[i+1]=a[i+1]+2**(x-1)
                x+=1
                ans+=1
    '''

#    print(ans)
#    x=1
#    ans=0


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 20:46:47 2020

@author: maruf
"""

a=[]
ans=0
x=1
for _ in range(int(input())):
    n=int(input())
    a=list(map(int, input().strip().split()))[:n]
    while(a!=sorted(a)):
        for i in range(n-1):
            if(a[i+1]<a[i]):
                a[i+1]=a[i+1]+2**(x-1)
                x+=1
                ans+=1

    print(ans)
    x=1
    ans=0


#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i,a,b) for(lli i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
#define inf 1000000000000000
#define lld long double
#define pll pair<long long int,long long int>
#define vll vector<lli>
#define eps 0.000001
//#define endl '\n'

using namespace std ;

#define MAXN 100010
//lli spf[MAXN];
//void sieve();
//set<lli> factors(lli x);
lli power(lli x,lli y,lli p);
//void getZarr(string str, lli Z[]);
//vll prefix_function(vll s);

typedef tree<lli,null_type,less<lli>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;

int dp[5001][5001];
bool ispal[5001][5001];

int main()
{

///////////////////////////////////////////
    fastio;
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
///////////////////////////////////////////

  string s;
  cin>>s;

  int n = s.length() ;

  loop(i,0,n) dp[i][i]=1;

  loop(i,0,n)
  {
    loop(j,0,n)
    {
      if(i>=j) ispal[i][j]=1;

      else break;
    }
  }

  loop(i,1,n)
  {
    loop(j,0,n)
    {
      if(j+i >= n) break;
      ispal[j][j+i] = ispal[j+1][j+i-1]&(s[j] == s[j+i]);
    }
  }

 /* loopl(i,0,n)
  {
    loopl(j,0,n)
    cout<<dp[i][j]<<" ";
    cout<<endl;
  }*/

  loop(i,1,n)
  {
    loop(j,0,n)
    {
      if(j+i>=n) break;
      dp[j][j+i] = dp[j][j+i-1] + dp[j+1][j+i] - dp[j+1][j+i-1] + (ispal[j][j+i]);
    }
  }

  int q;
  cin>>q;

  while(q--)
  {
    int l,r;
    cin>>l>>r;
    cout<<dp[l-1][r-1]<<endl;
  }

}

/*vll prefix_function(vll s)
{
    lli n = (lli)s.size();
    vll pi(n);

    for (lli i = 1; i < n; i++)
    {
        lli j = pi[i-1];

        while (j > 0 && s[i] != s[j])
            j = pi[j-1];

        if (s[i] == s[j])
            j++;

        pi[i] = j;
    }
    return pi;
}
*/
/*void sieve()
{
    spf[1] = 1;
    loopl(i,2,MAXN)
        spf[i] = i;
    for (lli i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (lli i=3; i*i<MAXN; i++) {
        if (spf[i] == i) {
            for (lli j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }

}

multiset<lli> factors(lli x)
{
    multiset<lli> pr;
    while (x != 1)
    {
        pr.insert(spf[x]);
        x = x / spf[x];
    }
    return pr;
}

*/

/*
void dfs(lli q,lli root)
{
    vis[q]=1;

    ans[q]=1;

    for(auto x: v[q])
    {
        if(!vis[x])
        {
            if(a[x]==a[root] && x<root) continue;
            if(a[x]<a[root] || a[x]>a[root]+d) continue;
            dfs(x,root);
            ans[q]=(ans[q]*((ans[x]+1)%mod))%mod ;
        }
    }

}
*/

/*void getZarr(string str, lli Z[])
{
    lli n = str.length();
    lli L, R, k;
    L = R = 0;
    for (lli i = 1; i < n; ++i){
        if (i > R){
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        } else {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

*/

lli power(lli x, lli y, lli p)
{
    lli res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}


#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i,a,b) for(lli i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
#define inf 1000000000000000
#define lld long double
#define pll pair<long long int,long long int>
#define vll vector<lli>
#define eps 0.000001
//#define endl '\n'

using namespace std ;

#define MAXN 100010
//lli spf[MAXN];
//void sieve();
//set<lli> factors(lli x);
lli power(lli x,lli y,lli p);
//void getZarr(string str, lli Z[]);
//vll prefix_function(vll s);

typedef tree<lli,null_type,less<lli>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;

int main()
{

///////////////////////////////////////////
    fastio;
    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
///////////////////////////////////////////

  string s;
  cin>>s;

  lli n = s.length() ;

  lli dp[n][n];
  bool ispal[n][n];

  loopl(i,0,n)
  {
    loopl(j,0,n)
    {
      if(i>=j)
        ispal[i][j] = 1;

      if(i>j) dp[i][j]=0;
      else if(i==j) dp[i][i]=1;
      else dp[i][j]=0;

    }
  }

  loopl(i,1,n)
  {
    loopl(j,0,n)
    ispal[j][j+i] = ispal[j+1][j+i-1]&(s[j] == s[j+i]);
  }

 /* loopl(i,0,n)
  {
    loopl(j,0,n)
    cout<<dp[i][j]<<" ";
    cout<<endl;
  }*/

  loopl(i,1,n)
  {
    loopl(j,0,n)
    dp[j][j+i] = dp[j][j+i-1] + dp[j+1][j+i] - dp[j+1][j+i-1] + (ispal[j][j+i]);
  }

  lli q;
  cin>>q;

  while(q--)
  {
    lli l,r;
    cin>>l>>r;
    cout<<dp[l-1][r-1]<<endl;
  }

}

/*vll prefix_function(vll s)
{
    lli n = (lli)s.size();
    vll pi(n);

    for (lli i = 1; i < n; i++)
    {
        lli j = pi[i-1];

        while (j > 0 && s[i] != s[j])
            j = pi[j-1];

        if (s[i] == s[j])
            j++;

        pi[i] = j;
    }
    return pi;
}
*/
/*void sieve()
{
    spf[1] = 1;
    loopl(i,2,MAXN)
        spf[i] = i;
    for (lli i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (lli i=3; i*i<MAXN; i++) {
        if (spf[i] == i) {
            for (lli j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }

}

multiset<lli> factors(lli x)
{
    multiset<lli> pr;
    while (x != 1)
    {
        pr.insert(spf[x]);
        x = x / spf[x];
    }
    return pr;
}

*/

/*
void dfs(lli q,lli root)
{
    vis[q]=1;

    ans[q]=1;

    for(auto x: v[q])
    {
        if(!vis[x])
        {
            if(a[x]==a[root] && x<root) continue;
            if(a[x]<a[root] || a[x]>a[root]+d) continue;
            dfs(x,root);
            ans[q]=(ans[q]*((ans[x]+1)%mod))%mod ;
        }
    }

}
*/

/*void getZarr(string str, lli Z[])
{
    lli n = str.length();
    lli L, R, k;
    L = R = 0;
    for (lli i = 1; i < n; ++i){
        if (i > R){
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        } else {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

*/

lli power(lli x, lli y, lli p)
{
    lli res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    string str;
    cin>>str;
    int n =str.size();
    int  dp[n+2][n+2];
    map<pair<int, int > , int >pal_even;
    map<pair<int, int > , int >pal_odd;

    for(int i =0 ; i< n; i++){
        dp[i][0]=1;
        pal_odd.insert({{i,i},1});
    }
    for(int i=0;i<n-1;i++){
        if(str[i]==str[i+1]){
            pal_even.insert({{i,i+1},1});
            dp[i][1]=3;
        } else{
            dp[i][1]=2;
        }
    }

    for(int i = 2 ; i< n ; i++){
        map<pair<int , int > , int >temp;
        for(int j =0 ; j< n-i ; j++ ){
            dp[j][i]=dp[j+1][i-1]+dp[j][i-1]-dp[j+1][i-2];
            if(i%2==0){ // work on pal_odd
                if(pal_odd.count({j+1,j+i-1})){
                    if(str[j]==str[i+j]){
                        temp.insert({ {j,i+j},1});
                        dp[j][i]++;
                    }
                }
                continue;
            }
            if(pal_even.count({j+1,j+i-1})){
                if(str[j]==str[i+j]){
                    temp.insert({ {j,i+j},1});
                    dp[j][i]++;
                }
            }
        }
        if(i%2==0)pal_odd=temp;
        else pal_even=temp;
    }

    int q ;
    cin>>q ;
    while (q--){
        int l , r;
        cin>>l>>r;
        cout<<dp[l-1][r-l]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    char str[5002];
    scanf("%s", str);

    int n =strlen(str);
    int dp[n+2][n+2];
    bool pal[n+2][n+2];
    memset(pal,0,sizeof(pal));
    for(int i =0 ; i< n; i++){
        dp[i][0]=1;
        pal[i][i]=1;
    }
    for(int i = 1 ; i< n ; i++){
        for(int j =0 ; j< n-i ; j++ ){
            dp[j][i]=dp[j+1][i-1]+dp[j][i-1]-(i!=1?dp[j+1][i-2]:0);
            if(pal[j+1][j+i-1]||i==1){
                if(str[j]==str[i+j]){
                    pal [j][i+j]=true;
                    dp[j][i]++;
                }
            }
        }
    }
    int q ;
    scanf("%d", &q);
    while (q--){
        int l , r;
        scanf("%d %d", &l, &r);
        printf("%d\n",dp[l-1][r-l]);
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
map<pair<ll , ll > , ll >dp;
map<int , int> nums;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    IO;
    string str;
    cin>>str;
    int n =str.size();
    map<pair<int, int>, int > dp;
    for(int i =0 ; i< n; i++)dp[{i,i}]=1;
    for(int i = 1 ; i< n ; i++){
        for(int j =0 ; j< n-i ; j++ ){
           dp[{j,i+j}]=dp[{j+1,i+j}]+dp[{j,i+j-1}]-(i!=1?dp[{j+1,i+j-1}]:0)+(str[j]==str[i+j]);
        }
    }
    int q ;
    cin>>q ;
    while (q--){
        int l , r;
        cin>>l>>r;
        cout<<dp[{l-1,r-1}]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
map<pair<ll , ll > , ll >dp;
map<int , int> nums;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    IO;
    string str;
    cin>>str;
    int n =str.size();
    map<pair<int, int>, int > dp;
    for(int i =0 ; i< n; i++)dp[{i,0}]=1;
    for(int i = 1 ; i< n ; i++){
        for(int j =0 ; j< n-i ; j++ ){
            dp[{j,i}]=dp[{j+1,i-1}]+dp[{j,i-1}]-(i!=1?dp[{j+1,i-2}]:0)+(str[j]==str[i+j]);
        }
    }
    int q ;
    cin>>q ;
    while (q--){
        int l , r;
        cin>>l>>r;
        cout<<dp[{l-1,r-l}]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    IO;
    string str;
    cin>>str;
    int n =str.size();
    map<pair<int, int>, int > dp;
    map<pair<int, int > , int >pal_even;
    map<pair<int, int > , int >pal_odd;

    for(int i =0 ; i< n; i++){
        dp[{i,0}]=1;
        pal_odd.insert({{i,i},1});
    }
    for(int i=0;i<n-1;i++){
        if(str[i]==str[i+1]){
            pal_even.insert({{i,i+1},1});
            dp[{i,1}]=3;
        } else{
            dp[{i,1}]=2;

        }
    }

    for(int i = 2 ; i< n ; i++){
        map<pair<int , int > , int >temp;
        for(int j =0 ; j< n-i ; j++ ){

            dp[{j,i}]=dp[{j+1,i-1}]+dp[{j,i-1}]-dp[{j+1,i-2}];
            if(i%2==0){ // work on pal_odd

                if(pal_odd.count({j+1,j+i-1})){

                    if(str[j]==str[i+j]){
                        temp.insert({ {j,i+j},1});
                        dp[{j,i}]++;
                    }
                }
                continue;
            }
            if(pal_even.count({j+1,j+i-1})){
                if(str[j]==str[i+j]){
                    temp.insert({ {j,i+j},1});
                    dp[{j,i}]++;
                }
            }
        }
        if(i%2==0)pal_odd=temp;
        else pal_even=temp;
    }

    int q ;
    cin>>q ;
    while (q--){
        int l , r;
        cin>>l>>r;
        cout<<dp[{l-1,r-l}]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    IO;
    string str;
    cin>>str;
    int n =str.size();
    int dp[n+2][n+2];
    bool pal[n+2][n+2];
    memset(pal,0,sizeof(pal));
    for(int i =0 ; i< n; i++){
        dp[i][0]=1;
        pal[i][i]=1;
    }
    for(int i = 1 ; i< n ; i++){
        for(int j =0 ; j< n-i ; j++ ){
            dp[j][i]=dp[j+1][i-1]+dp[j][i-1]-(i!=1?dp[j+1][i-2]:0);
            if(pal[j+1][j+i-1]||i==1){

                if(str[j]==str[i+j]){
                    pal [j][i+j]=true;
                    dp[j][i]++;
                }
            }
        }
    }
    int q ;
    cin>>q ;
    while (q--){
        int l , r;
        cin>>l>>r;
        cout<<dp[l-1][r-l]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    char str[5002];
    scanf("%s", str);

    int n =strlen(str);
    int dp[n+2][n+2];
    bool pal[n+2][n+2];
    memset(pal,0,sizeof(pal));
    for(int i =0 ; i< n; i++){
        dp[i][0]=1;
        pal[i][i]=1;
    }
    for(int i = 1 ; i< n ; i++){
        for(int j =0 ; j< n-i ; j++ ){
            dp[j][i]=dp[j+1][i-1]+dp[j][i-1]-(i!=1?dp[j+1][i-2]:0);
            if(pal[j+1][j+i-1]||i==1){
                if(str[j]==str[i+j]){
                    pal [j][i+j]=true;
                    dp[j][i]++;
                }
            }
        }
    }
    int q ;
    scanf("%d", &q);
    while (q--){
        int l , r;
        scanf("%d %d", &l, &r);
        cout<<dp[l-1][r-l]<<endl;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
const ll INF=1e9+5;
int main() {
#ifdef _DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    IO;
    string str;
    cin>>str;
    int n =str.size();
    map<pair<int, int>, int > dp;
    map<pair<int, int > , int >pal;

    for(int i =0 ; i< n; i++){
        dp[{i,0}]=1;
        pal[{i,i}]=1;
    }
    for(int i = 1 ; i< n ; i++){
        for(int j =0 ; j< n-i ; j++ ){
            dp[{j,i}]=dp[{j+1,i-1}]+dp[{j,i-1}]-(i!=1?dp[{j+1,i-2}]:0);
            if(pal.count({j+1,j+i-1})||i==1){

                if(str[j]==str[i+j]){
                    pal.insert({ {j,i+j},1});
                    dp[{j,i}]++;
                }
            }
        }
    }

    int q ;
    cin>>q ;
    while (q--){
        int l , r;
        cin>>l>>r;
        cout<<dp[{l-1,r-l}]<<endl;
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;

const int maxin = 2e5 + 10;
const int inf = INT_MAX - 10;
const ll llinf = LLONG_MAX - 10;

int pref[5555][5555];
int dp[5555][5555];

main ()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   if (fopen("in.in", "r"))
   {
      freopen("in.in", "r", stdin);
      freopen("out.out", "w", stdout);
   }
   string s;
   cin >> s;
   int n = s.length();
   s = "#" + s;
   for (int i = 1;i <= n;++i)
      dp[i][i] = 1;
   for (int i = 1;i <= n;++i)
      dp[i][i + 1] = (s[i] == s[i + 1]);
   for (int ln = 0;ln < n - 2;++ln)
      for (int i = 1;i < n - ln;++i)
         if (dp[i][i + ln] && s[i - 1] == s[i + ln + 1])
            dp[i - 1][i + ln + 1] = 1;
   for (int i = 1;i <= n;++i)
      for (int j = 1;j <= n;++j)
         pref[i][j] = dp[i][j] + pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1];
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << dp[i][j] << " ";
//   cout << "\n\n";
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << pref[i][j] << " ";
//   cout << "\n\n";
   int q;
   cin >> q;
   for (int i = 1, a, b;i <= q && cin >> a >> b;++i)
      cout << pref[b][b] + pref[a - 1][a - 1] - pref[a - 1][b] - pref[b][a - 1] << "\n";
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;

const int maxin = 2e5 + 10;
const int inf = INT_MAX - 10;
const ll llinf = LLONG_MAX - 10;

int pref[5555][5555];
int dp[5555][5555];

main ()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   if (fopen("in.in", "r"))
   {
      freopen("in.in", "r", stdin);
      freopen("out.out", "w", stdout);
   }
   string s;
   cin >> s;
   int n = s.length();
   s = "#" + s;
   for (int i = 1;i <= n;++i)
      dp[i][i] = 1;
   for (int i = 1;i <= n;++i)
      dp[i][i + 1] = (s[i] == s[i + 1]);
   for (int ln = 0;ln < n - 2;++ln)
      for (int i = 1;i < n - ln;++i)
         if (dp[i][i + ln] && s[i - 1] == s[i + ln + 1])
            dp[i - 1][i + ln + 1] = 1;
   for (int i = 1;i <= n;++i)
      for (int j = 1;j <= n;++j)
         pref[i][j] = dp[i][j] + pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1];
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << dp[i][j] << " ";
//   cout << "\n\n";
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << pref[i][j] << " ";
//   cout << "\n\n";
   int q;
   cin >> q;
   for (int i = 1, a, b;i <= q && cin >> a >> b;++i)
      cout << pref[b][b] + pref[a - 1][a - 1] - pref[a - 1][b] - pref[b][a - 1] << "\n";
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;

const int maxin = 2e5 + 10;
const int inf = INT_MAX - 10;
const ll llinf = LLONG_MAX - 10;

int pref[5555][5555];
int dp[5555][5555];

main ()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   if (fopen("in.in", "r"))
   {
      freopen("in.in", "r", stdin);
      freopen("out.out", "w", stdout);
   }
   string s;
   cin >> s;
   int n = s.length();
   s = "#" + s;
   for (int i = 1;i <= n;++i)
      dp[i][i] = 1;
   for (int i = 1;i <= n;++i)
      dp[i][i + 1] = (s[i] == s[i + 1]);
   for (int ln = 0;ln < n - 2;++ln)
      for (int i = 1;i < n - ln;++i)
         if (dp[i][i + ln] && s[i - 1] == s[i + ln + 1])
            dp[i - 1][i + ln + 1] = 1;
   for (int i = 1;i <= n;++i)
      for (int j = 1;j <= n;++j)
         pref[i][j] = dp[i][j] + pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1];
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << dp[i][j] << " ";
//   cout << "\n\n";
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << pref[i][j] << " ";
//   cout << "\n\n";
   int q;
   cin >> q;
   for (int i = 1, a, b;i <= q && cin >> a >> b;++i)
      cout << pref[b][b] - pref[a][b - 1] - pref[b - 1][a] + pref[a - 1][a - 1] << '\n';
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;

const int maxin = 2e5 + 10;
const int inf = INT_MAX - 10;
const ll llinf = LLONG_MAX - 10;

int pref[5555][5555];
int dp[5555][5555];

main ()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   if (fopen("in.in", "r"))
   {
      freopen("in.in", "r", stdin);
      freopen("out.out", "w", stdout);
   }
   string s;
   cin >> s;
   int n = s.length();
   s = "#" + s;
   for (int i = 1;i <= n;++i)
      dp[i][i] = 1;
   for (int i = 1;i <= n;++i)
      dp[i][i + 1] = (s[i] == s[i + 1]);
   for (int ln = 0;ln < n - 2;++ln)
      for (int i = 1;i < n - ln;++i)
         if (dp[i][i + ln] && s[i - 1] == s[i + ln + 1])
            dp[i - 1][i + ln + 1] = 1;
   for (int i = 1;i <= n;++i)
      for (int j = 1;j <= n;++j)
         pref[i][j] = dp[i][j] + pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1];
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << dp[i][j] << " ";
//   cout << "\n\n";
//   for (int i = 1;i <= n && cout << "\n";++i)
//      for (int j = 1;j <= n;++j)
//         cout << pref[i][j] << " ";
//   cout << "\n\n";
   int q;
   cin >> q;
   for (int i = 1, a, b;i <= q && cin >> a >> b;++i)
      cout << pref[b][b] + pref[a - 1][a - 1] - pref[a - 1][b] - pref[b][a - 1] << "\n";
}


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

const ll N = 5005;

bool pal[N][N];
ll dp[N][N];

char s[N];
int main(){
    ios_base::sync_with_stdio(false);
    scanf("%s",s);
    ll n = strlen(s);
    for(long i = 1; i <= n; i++){
        pal[i][i] = 1;
        dp[i][i] = 1;
    }
    for(long i = 0; i < n-1; i++){
        if(s[i] == s[i+1]){
            pal[i+1][i+2] = 1;
            dp[i+1][i+2] = 3;
        }
        else{
            dp[i+1][i+2] = 2;
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            pal[start][start+len-1] = pal[start+1][start+len-2] && (s[start-1] == s[start+len-2]);
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            dp[start][start+len-1] = dp[start+1][start+len-1] + dp[start][start+len-2] - dp[start+1][start+len-2] + pal[start][start+len-1];
        }
    }
    ll q;
    scanf("%lld",&q);
    while(q--){
        ll l,r;
        scanf("%lld%lld",&l,&r);
        printf("%lld\n",dp[l][r]);
    }
}


#include <iostream>

using namespace std;

const int MAXN = 5000 + 10;

string s;
int n, q, dp[MAXN][MAXN];
bool isPal[MAXN][MAXN];

int main()
{
    ios::sync_with_stdio(0);
    cin >> s;
    n = (int)s.size();
    for (int i = 0 ; i < n ; i++)
    {
        isPal[i][i] = 1;
        dp[i][i] = 1;
        isPal[i+1][i] = 1;
    }
    for (int len = 2 ; len <= n ; len++)
        for (int start = 0 ; start <= n-len ; start++)
        {
            isPal[start][start+len-1] = isPal[start+1][start+len-2] & s[start] == s[start+len-1];
            dp[start][start+len-1] = dp[start][start+len-2] + dp[start+1][start+len-1] - dp[start+1][start+len-2] + isPal[start][start+len-1];
        }
    cin >> q;
    for (int i = 1 ; i <= q ; i++)
    {
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        cout << dp[l][r] << endl;
    }
    return 0;
}


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

const ll N = 5005;

bool pal[N][N];
ll dp[N][N];

string s;
int main(){
    ios_base::sync_with_stdio(false);
    cin >> s;
    ll n = s.size();
    for(long i = 1; i <= n; i++){
        pal[i][i] = 1;
        dp[i][i] = 1;
    }
    for(long i = 0; i < n-1; i++){
        if(s[i] == s[i+1]){
            pal[i+1][i+2] = 1;
            dp[i+1][i+2] = 3;
        }
        else{
            dp[i+1][i+2] = 2;
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            pal[start][start+len-1] = pal[start+1][start+len-2] && (s[start-1] == s[start+len-2]);
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            dp[start][start+len-1] = dp[start+1][start+len-1] + dp[start][start+len-2] - dp[start+1][start+len-2] + pal[start][start+len-1];
        }
    }
    ll q;
    cin >> q;
    while(q--){
        ll l,r;
        cin >> l >> r;
        cout << dp[l][r] << endl;
    }
}


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

const ll N = 5005;

bool pal[N][N];
ll dp[N][N];

string s;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> s;
    ll n = s.size();
    for(long i = 1; i <= n; i++){
        pal[i][i] = 1;
        dp[i][i] = 1;
    }
    for(long i = 0; i < n-1; i++){
        if(s[i] == s[i+1]){
            pal[i+1][i+2] = 1;
            dp[i+1][i+2] = 3;
        }
        else{
            dp[i+1][i+2] = 2;
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            pal[start][start+len-1] = pal[start+1][start+len-2] && (s[start-1] == s[start+len-2]);
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            dp[start][start+len-1] = dp[start+1][start+len-1] + dp[start][start+len-2] - dp[start+1][start+len-2] + pal[start][start+len-1];
        }
    }
    ll q;
    cin >> q;
    while(q--){
        ll l,r;
        cin >> l >> r;
        cout << dp[l][r] << "\n";
    }
}


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

const ll N = 5005;

bool pal[N][N];
ll dp[N][N];

string s;
int main(){
    ios_base::sync_with_stdio(false);
    cin >> s;
    ll n = s.size();
    for(long i = 1; i <= n; i++){
        pal[i][i] = 1;
        dp[i][i] = 1;
    }
    for(long i = 0; i < n-1; i++){
        if(s[i] == s[i+1]){
            pal[i+1][i+2] = 1;
            dp[i+1][i+2] = 3;
        }
        else{
            dp[i+1][i+2] = 2;
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            pal[start][start+len-1] = pal[start+1][start+len-2] && (s[start-1] == s[start+len-2]);
        }
    }
    for(long len = 3; len <= n; len++){
        for(long start = 1; start+len-1 <= n; start++){
            dp[start][start+len-1] = dp[start+1][start+len-1] + dp[start][start+len-2] - dp[start+1][start+len-2] + pal[start][start+len-1];
        }
    }
    ll q;
    cin >> q;
    while(q--){
        ll l,r;
        cin >> l >> r;
        cout << dp[l][r] << "\n";
    }
}


// In The Name Of Allah
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
const ll mod = 998244353;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const ll M = 26 + 10;
const ll N = 5000 + 10;

ll dp[N][N] , a[N][N];

int main()
{
	faster();
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		a[i][i] = 1;
		dp[i][i] = 1;
		a[i + 1][i] = 1;
	}
	for (int len = 2; len <= n; len++)
	{
		for (int i = 0; i <= n - len; i++)
		{
			a[i][i + len - 1] = a[i + 1][i + len - 2] & (s[i] == s[i + len - 1]);
			dp[i][i + len - 1] = dp[i][i + len - 2] + dp[i + 1][i + len - 1] - dp[i + 1][i + len - 2] + a[i][i + len - 1];
		}
	}
	int q;
	cin >> q;
	while(q--)
	{
		int l, r;
		cin >> l >> r;
		cout << dp[l - 1][r - 1] << endl;
	}

}


// In The Name Of Allah
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
const ll mod = 998244353;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll M = 26 + 10;
const ll N = 5000 + 10;

int dp[N][N];
bool a[N][N];

int main()
{
	faster();
	string s;
	int q;
	cin >> s >> q;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		a[i][i] = 1;
		dp[i][i] = 1;
		a[i + 1][i] = 1;
	}
	for (int len = 2; len <= n; len++)
	{
		for (int i = 0; i <= n - len; i++)
		{
			a[i][i + len - 1] = a[i + 1][i + len - 2] & (s[i] == s[i + len - 1]);
			dp[i][i + len - 1] = dp[i][i + len - 2] + dp[i + 1][i + len - 1] - dp[i + 1][i + len - 2] + a[i][i + len - 1];
		}
	}
	for (int i = 0; i < q; i++)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		cout << dp[l][r] << endl;
	}
}


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

const int N = 5000 + 7;

char str[N];

bool dp[N][N]; int f[N][N], cnt[N];

int main() {
       // ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
        scanf("%s", str); int n = strlen(str);
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dp[i][j] = 1;
            }
        }

        for(int i = 2; i <= n; i++) {
            for(int j = 1; j + i - 1 <= n; j++) {
                dp[j][j + i - 1] = (str[j - 1] == str[j + i - 2]) * dp[j + 1][j + i - 2];
            }
        }

        for(int i = 1; i <= n; i++) {
            for(int j = i; j >= 1; j--) {
                cnt[j] += dp[j][i];
                f[j][i] = cnt[j] + f[j + 1][i];
            }
        }

        int q; scanf("%d", &q);
        while(q--) {

            int l, r; scanf("%d %d", &l, &r);
            printf("%d\n", f[l][r]);
        }
}


// In The Name Of Allah
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
const ll mod = 998244353;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const ll M = 26 + 10;
const ll N = 5000 + 10;

ll dp[N][N] , a[N][N];

int main()
{
	faster();
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		a[i][i] = 1;
		dp[i][i] = 1;
		a[i + 1][i] = 1;
	}
	for (int len = 2; len <= n; len++)
	{
		for (int i = 0; i <= n - len; i++)
		{
			a[i][i + len - 1] = a[i + 1][i + len - 2] & (s[i] == s[i + len - 1]);
			dp[i][i + len - 1] = dp[i][i + len - 2] + dp[i + 1][i + len - 1] - dp[i + 1][i + len - 2] + a[i][i + len - 1];
		}
	}
/*
	for (int i = 0; i < n; i++)
	{
		for (int j = i; j < n; j++)
		{
			for (int k = i; k <= j; k++)
				cout << s[k];
			cout << " " << a[i][j] << endl;
		}
	}
	cout << endl << endl;
 */
/*
	for (int i = 0; i < n; i++)
	{
		for (int j = i; j < n; j++)
		{
			for (int k = i; k <= j; k++)
			{
				cout << s[k];
			}
			cout << " " << dp[i][j] << endl;
		}
	}
*/
	int q;
	cin >> q;
	while(q--)
	{
		int l, r;
		cin >> l >> r;
		cout << dp[l - 1][r - 1] << endl;
	}

}


// In The Name Of Allah
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
const ll mod = 998244353;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const ll M = 26 + 10;
const ll N = 5000 + 10;

int dp[N][N];
bool a[N][N];

int main()
{
	faster();
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		a[i][i] = 1;
		dp[i][i] = 1;
		a[i + 1][i] = 1;
	}
	for (int len = 2; len <= n; len++)
	{
		for (int i = 0; i <= n - len; i++)
		{
			a[i][i + len - 1] = a[i + 1][i + len - 2] & (s[i] == s[i + len - 1]);
			dp[i][i + len - 1] = dp[i][i + len - 2] + dp[i + 1][i + len - 1] - dp[i + 1][i + len - 2] + a[i][i + len - 1];
		}
	}
	int q;
	cin >> q;
	while(q--)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		cout << dp[l][r] << endl;
	}

}


// In The Name Of Allah
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
const ll mod = 998244353;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const ll M = 26 + 10;
const ll N = 5000 + 10;

int dp[N][N] , a[N][N];

int main()
{
	faster();
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		a[i][i] = 1;
		dp[i][i] = 1;
		a[i + 1][i] = 1;
	}
	for (int len = 2; len <= n; len++)
	{
		for (int i = 0; i <= n - len; i++)
		{
			a[i][i + len - 1] = a[i + 1][i + len - 2] & (s[i] == s[i + len - 1]);
			dp[i][i + len - 1] = dp[i][i + len - 2] + dp[i + 1][i + len - 1] - dp[i + 1][i + len - 2] + a[i][i + len - 1];
		}
	}
	int q;
	cin >> q;
	while(q--)
	{
		int l, r;
		cin >> l >> r;
		cout << dp[l - 1][r - 1] << endl;
	}

}


#include<bits/stdc++.h>
//#define nguyendz the_best

#define ll int
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
/*#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second*/

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+3;
const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;
//priority_queue<pll,vector<pll>,greater<pll>> q;
vector <ll> adj[maxn];
ll kc,t,n,m,k;

ll  nextInt() {
    ll x = 0, p = 1;
    char c;
    do {
        c = getchar();
    } while (c <= 32);
    if (c == '-') {
        p = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * p;
}
bool kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
    n=s.length();

   for (int i=0;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
       kt[i+1][i]=1;
   }

  for (int len=2;len<=n;len++)
  {
      for (int start=0;start<n-len+1;start++)
      {

               kt[start][start+len-1]=kt[start+1][start+len-2]&(s[start]==s[start+len-1]);

          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;

       cin>>x>>y;
       x--;
       y--;
       cout <<dp[x][y]<<endl;
   }

}


#include<bits/stdc++.h>
#define nguyendz the_best

#define ll long long
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+3;
const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;

vector <ll> adj[maxn];
ll kc,t,n,m,k;

bool kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
    n=s.length();

   for (int i=0;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
       kt[i+1][i]=1;
   }

  for (int len=2;len<=n;len++)
  {
      for (int start=0;start<n-len+1;start++)
      {

               kt[start][start+len-1]=kt[start+1][start+len-2]&(s[start]==s[start+len-1]);

          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;

       cin>>x>>y;
       x--;
       y--;
       cout <<dp[x][y]<<endl;
   }

}


#include<bits/stdc++.h>
//#define nguyendz the_best

#define ll int
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
/*#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second*/

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+3;
const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;
//priority_queue<pll,vector<pll>,greater<pll>> q;
vector <ll> adj[maxn];
ll kc,t,n,m,k;

ll  nextInt() {
    ll x = 0, p = 1;
    char c;
    do {
        c = getchar();
    } while (c <= 32);
    if (c == '-') {
        p = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * p;
}
bool kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
   ll n=s.length();
   s=" "+s;
   for (int i=1;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
   }
   for (int i=1;i<n;i++)
   {
       if (s[i]==s[i+1])
       {
           kt[i][i+1]=1;
       }
   }
   //cout <<kt[4][6];
  for (int len=2;len<=n;len++)
  {
      for (int start=1;start<=n-len+1;start++)
      {
           if (kt[start+1][start+len-2]&&s[start]==s[start+len-1])
           {
               kt[start][start+len-1]=1;
           }
          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;
       cin>>x>>y;
       cout <<dp[x][y]<<endl;
   }

}


#include<bits/stdc++.h>
//#define nguyendz the_best

#define ll int
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
/*#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second*/

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+500;
/*const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;*/
//priority_queue<pll,vector<pll>,greater<pll>> q;
vector <ll> adj[maxn];
ll kc,t,n,m,k;

ll par[maxn];
ll sz[maxn];
ll ans;
ll pos;
bool dd[maxn];
ll bk[maxn];
ll  nextInt() {
    ll x = 0, p = 1;
    char c;
    do {
        c = getchar();
    } while (c <= 32);
    if (c == '-') {
        p = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * p;
}
ll kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
   ll n=s.length();
   s=" "+s;
   for (int i=1;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
   }
   for (int i=1;i<n;i++)
   {
       if (s[i]==s[i+1])
       {
           kt[i][i+1]=1;
       }
   }
   //cout <<kt[4][6];
  for (int len=2;len<=n;len++)
  {
      for (int start=1;start<=n-len+1;start++)
      {
           if (kt[start+1][start+len-2]&&s[start]==s[start+len-1])
           {
               kt[start][start+len-1]=1;
           }
          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;
       cin>>x>>y;
       cout <<dp[x][y]<<endl;
   }

}