/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,mn=INT_MAX,ans=0,ck;
    string s;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        cin>>s;
        ans=0;
        int len=(int)s.length();
        mn=INT_MAX;
        if(len==1) {
            cout<<s[0]-'0'<<endl;
        } else {
            ans+=(len-2)*9;
            ck=1;
            for(int i=1; i<len; i++) {
                //mn=min(mn,s[i]-'0');
                if(s[i]>s[0]) {
                    break;
                } else if(s[i]<s[0]) {
                    ck=0;
                    break;
                }
            }
            if(ck==0) {
                if((s[0]-'0')!=1) {
                    ans+=((s[0]-'0')-1);
                }
            } else {
                if(ck==1)
                    ans+=1;
                if((s[0]-'0')!=1) {
                    ans+=((s[0]-'0')-1);
                }
            }
            cout<<ans+9<<endl;
        }
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test=1,mn=INT_MAX,ans=0;
    string s;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        cin>>s;
        ans=0;
        mn=INT_MAX;
        if(s.length()==1) {
            cout<<s<<endl;
        } else {
            ans+=((int)s.length()-2)*9;
            for(int i=1; i<(int)s.length(); i++) {
                mn=min(mn,s[i]-'0');
            }
            if(mn==0&&s[1]<=s[0]) {
                if((s[0]-'0')!=1) {
                    ans+=((s[0]-'0')-1);
                }
            } else {
                if(mn>=s[0]-'0'||s[1]>s[0])
                    ans+=1;
                if(s[0]-'0'!=1) {
                    ans+=(s[0]-'0')-1;
                }
            }
            cout<<ans+9<<endl;
        }
    }
    return 0;
}


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

#define ull unsigned long long int

int main(){
    int t;
    cin >> t;
    for(int a = 0; a < t; a++){
        int n;
        cin >> n;
        string s = std::to_string(n);
        ull sum = 9 * (s.size() - 1);
        char min = s[0];
        for(int i = 1; i < s.size(); i++) {
            if(s[i] < min) min = s[i];
        }
        int k = min - '0';
        sum += k;
        cout << sum << endl;
    }
}


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

#define ull unsigned long long int

 bool notBeautiful(string s){
    char ch = s[0];
    for(int i = 0; i < s.size(); i++){
        if(s[i] < ch) return true;
    }
    return false;
 }

int main(){
    int t;
    cin >> t;
    for(int a = 0; a < t; a++){
        int n;
        cin >> n;
        string s = std::to_string(n);
        ull sum = 9 * (s.size() - 1);
        char min = s[0];
        int k = min - '0';
        sum += k;
        if(notBeautiful(s)) sum--;
        cout << sum << endl;
    }
}


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

#define ull unsigned long long int

 bool notBeautiful(string s){
    char ch = s[0];
    for(int i = 0; i < s.size(); i++){
        if(s[i] != ch) return true;
    }
    return false;
 }

int main(){
    int t;
    cin >> t;
    for(int a = 0; a < t; a++){
        int n;
        cin >> n;
        string s = std::to_string(n);
        ull sum = 9 * (s.size() - 1);
        char min = s[0];
        int k = min - '0';
        sum += k;
        if(notBeautiful(s)) sum--;
        cout << sum << endl;
    }
}


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

#define ull unsigned long long int

 bool notBeautiful(string s){
    char ch = s[0];
    for(int i = 0; i < s.size(); i++){
        if(s[i] < ch) return true;
    }
    return false;
 }

int main(){
    int t;
    cin >> t;
    for(int a = 0; a < t; a++){
        int n;
        cin >> n;
        string s = std::to_string(n);
        ull sum = 9 * (s.size() - 1);
        int k = 0;
        for(int i = 0; i < s.size(); i++){
            k += pow(10, i);
        }
        int tmp = k;
        while(true){
            if(k > n) break;
            sum++;
            k += tmp;
        }
        cout << sum << endl;
    }
}


//Halesh kon pesar hamishe behtarin boodi va khahi bood
//One Human
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <numeric>
using namespace std;
#define vi vector<int>
#define v_r(temp , first , second) replace (temp.begin(), temp.end(), first, second);
#define v_max(temp) *max_element(temp.begin(),temp.end())
#define v_min(temp) *min_element(temp.begin(),temp.end())
#define v_c(temp , element) count(temp.begin() , temp.end() , element)
#define v_av(temp) accumulate( temp.begin(), temp.end(), 0.0)/temp.size()
#define v_er1(temp,element) temp.erase(std::remove(temp.begin(), temp.end(), element), temp.end());
#define v_er2(temp,firstNumber) temp.erase(temp.begin() + firstNumber)
#define v_er3(temp,firstNumber,secondNumber) temp.erase(temp.begin() + firstNumber , temp.begin() + secondNumber)
#define s_t_i(string,number) stringstream geek(string);geek >> number
#define i_t_s(number , string) ostringstream str1;str1 << number; string = str1.str();
#define v_cin(_vector , number) int temp;for(int i=0;i<number;i++){cin>>temp;_vector.push_back(temp);}
#define v_cout(_vector) for(int i=0;i<_vector.size();i++){cout<<_vector[i]<<endl;}
#define f(number) for(int i=0;i<number;i++)

int v_f(vector<int>& vect, int element)
{
	int final;
	std::vector<int>::iterator it;
	it = std::find(vect.begin(), vect.end(), element);
	if (it != vect.end())
	{
		final = it - vect.begin();
		return final;
	}
	else
		return -1;
}
int s_f(string wholeString, string str) {
	size_t found = wholeString.find(str);
	if (found != string::npos)
		return found;
	else
		return -1;
}
int s_f_n(string wholeString, string str, int next) {
	size_t found = wholeString.find(str);
	if (found != string::npos) {
		found = wholeString.find(str, found + next);
		if (found != string::npos)
			return found;
		else
			return -1;
	}

}
int ch_c(string wholeString, char c) {
	size_t n = std::count(wholeString.begin(), wholeString.end(), c);
	return n;

}
int findRepeatFirstN2(string s)
{
	// this is O(N^2) method
	int p = -1, i, j;
	for (i = 0; i < s.size(); i++)
	{
		for (j = i + 1; j < s.size(); j++)
		{
			if (s[i] == s[j])
			{
				p = i;
				break;
			}
		}
		if (p != -1)
			break;
	}

	return p;
}
int main()
{
	int n, temp, tempResult = 0, count;
	string a = "", b;
	cin >> n;
	vi result, input;
	f(n) {
		cin >> temp;
		input.push_back(temp);
	}
	for (int i = 0; i < n; i++) {
		if (input[i] < 10) {
			result.push_back(input[i]);
			a="";
		}
		else if (input[i] >= 10) {
			string s;
			i_t_s(input[i], s);
			for (int h = 0; h < s.size(); h++) {
				a += "1";
			}
			int m;
			s_t_i(a, m);
			result.push_back((9 * (s.size() - 1)) + input[i] / m);
			a = "";
		}
	}
	v_cout(result);

}


//Halesh kon pesar hamishe behtarin boodi va khahi bood
//One Human
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <numeric>
using namespace std;
#define vi vector<int>
#define v_r(temp , first , second) replace (temp.begin(), temp.end(), first, second);
#define v_max(temp) *max_element(temp.begin(),temp.end())
#define v_min(temp) *min_element(temp.begin(),temp.end())
#define v_c(temp , element) count(temp.begin() , temp.end() , element)
#define v_av(temp) accumulate( temp.begin(), temp.end(), 0.0)/temp.size()
#define v_er1(temp,element) temp.erase(std::remove(temp.begin(), temp.end(), element), temp.end());
#define v_er2(temp,firstNumber) temp.erase(temp.begin() + firstNumber)
#define v_er3(temp,firstNumber,secondNumber) temp.erase(temp.begin() + firstNumber , temp.begin() + secondNumber)
#define s_t_i(string,number) stringstream geek(string);geek >> number
#define i_t_s(number , string) ostringstream str1;str1 << number; string = str1.str();
#define v_cin(_vector , number) int temp;for(int i=0;i<number;i++){cin>>temp;_vector.push_back(temp);}
#define v_cout(_vector) for(int i=0;i<_vector.size();i++){cout<<_vector[i]<<endl;}
#define f(number) for(int i=0;i<number;i++)

int v_f(vector<int>& vect, int element)
{
	int final;
	std::vector<int>::iterator it;
	it = std::find(vect.begin(), vect.end(), element);
	if (it != vect.end())
	{
		final = it - vect.begin();
		return final;
	}
	else
		return -1;
}
int s_f(string wholeString, string str) {
	size_t found = wholeString.find(str);
	if (found != string::npos)
		return found;
	else
		return -1;
}
int s_f_n(string wholeString, string str, int next) {
	size_t found = wholeString.find(str);
	if (found != string::npos) {
		found = wholeString.find(str, found + next);
		if (found != string::npos)
			return found;
		else
			return -1;
	}

}
int ch_c(string wholeString, char c) {
	size_t n = std::count(wholeString.begin(), wholeString.end(), c);
	return n;

}
int findRepeatFirstN2(string s)
{
	// this is O(N^2) method
	int p = -1, i, j;
	for (i = 0; i < s.size(); i++)
	{
		for (j = i + 1; j < s.size(); j++)
		{
			if (s[i] == s[j])
			{
				p = i;
				break;
			}
		}
		if (p != -1)
			break;
	}

	return p;
}
int main()
{
	int n, temp, tempResult = 0, count;
	string a = "", b;
	cin >> n;
	vi result, input;
	f(n) {
		cin >> temp;
		input.push_back(temp);
	}
	for (int i = 0; i < n; i++) {
		if (input[i] < 10) {
			result.push_back(input[i]);
			a="";
		}
		else if (input[i] > 10) {
			string s;
			i_t_s(input[i], s);
			for (int h = 0; h < s.size(); h++) {
				a += "1";
			}
			int m;
			s_t_i(a, m);
			result.push_back((9 * (s.size() - 1)) + input[i] / m);
			a = "";
		}
	}
	v_cout(result);

}


//Halesh kon pesar hamishe behtarin boodi va khahi bood
//One Human
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <numeric>
using namespace std;
#define vi vector<int>
#define v_r(temp , first , second) replace (temp.begin(), temp.end(), first, second);
#define v_max(temp) *max_element(temp.begin(),temp.end())
#define v_min(temp) *min_element(temp.begin(),temp.end())
#define v_c(temp , element) count(temp.begin() , temp.end() , element)
#define v_av(temp) accumulate( temp.begin(), temp.end(), 0.0)/temp.size()
#define v_er1(temp,element) temp.erase(std::remove(temp.begin(), temp.end(), element), temp.end());
#define v_er2(temp,firstNumber) temp.erase(temp.begin() + firstNumber)
#define v_er3(temp,firstNumber,secondNumber) temp.erase(temp.begin() + firstNumber , temp.begin() + secondNumber)
#define s_t_i(string,number) stringstream geek(string);geek >> number
#define i_t_s(number , string) ostringstream str1;str1 << number; string = str1.str();
#define v_cin(_vector , number) int temp;for(int i=0;i<number;i++){cin>>temp;_vector.push_back(temp);}
#define v_cout(_vector) for(int i=0;i<_vector.size();i++){cout<<_vector[i]<<endl;}
#define f(number) for(int i=0;i<number;i++)

int v_f(vector<int>& vect, int element)
{
	int final;
	std::vector<int>::iterator it;
	it = std::find(vect.begin(), vect.end(), element);
	if (it != vect.end())
	{
		final = it - vect.begin();
		return final;
	}
	else
		return -1;
}
int s_f(string wholeString, string str) {
	size_t found = wholeString.find(str);
	if (found != string::npos)
		return found;
	else
		return -1;
}
int s_f_n(string wholeString, string str, int next) {
	size_t found = wholeString.find(str);
	if (found != string::npos) {
		found = wholeString.find(str, found + next);
		if (found != string::npos)
			return found;
		else
			return -1;
	}

}
int ch_c(string wholeString, char c) {
	size_t n = std::count(wholeString.begin(), wholeString.end(), c);
	return n;

}
int findRepeatFirstN2(string s)
{
	// this is O(N^2) method
	int p = -1, i, j;
	for (i = 0; i < s.size(); i++)
	{
		for (j = i + 1; j < s.size(); j++)
		{
			if (s[i] == s[j])
			{
				p = i;
				break;
			}
		}
		if (p != -1)
			break;
	}

	return p;
}
int main()
{
	int n,temp,tempResult=0,count;
	string a;
	cin >> n;
	vi result,input;
	f(n) {
		cin >> temp;
		input.push_back(temp);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 1; j < input[i]; j++) {
			i_t_s(j, a);
			if (a.size() == 1) {
				tempResult += 1;
			}
			else
			{
				count = findRepeatFirstN2(a);
				if (count != -1) {
					tempResult += 1;
				}
			}
		}
		result.push_back(tempResult);
		tempResult = 0;
	}
	v_cout(result);

}


//Halesh kon pesar hamishe behtarin boodi va khahi bood
//One Human
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <numeric>
using namespace std;
#define vi vector<int>
#define v_r(temp , first , second) replace (temp.begin(), temp.end(), first, second);
#define v_max(temp) *max_element(temp.begin(),temp.end())
#define v_min(temp) *min_element(temp.begin(),temp.end())
#define v_c(temp , element) count(temp.begin() , temp.end() , element)
#define v_av(temp) accumulate( temp.begin(), temp.end(), 0.0)/temp.size()
#define v_er1(temp,element) temp.erase(std::remove(temp.begin(), temp.end(), element), temp.end());
#define v_er2(temp,firstNumber) temp.erase(temp.begin() + firstNumber)
#define v_er3(temp,firstNumber,secondNumber) temp.erase(temp.begin() + firstNumber , temp.begin() + secondNumber)
#define s_t_i(string,number) stringstream geek(string);geek >> number
#define i_t_s(number , string) ostringstream str1;str1 << number; string = str1.str();
#define v_cin(_vector , number) int temp;for(int i=0;i<number;i++){cin>>temp;_vector.push_back(temp);}
#define v_cout(_vector) for(int i=0;i<_vector.size();i++){cout<<_vector[i]<<endl;}
#define f(number) for(int i=0;i<number;i++)

int v_f(vector<int>& vect, int element)
{
	int final;
	std::vector<int>::iterator it;
	it = std::find(vect.begin(), vect.end(), element);
	if (it != vect.end())
	{
		final = it - vect.begin();
		return final;
	}
	else
		return -1;
}
int s_f(string wholeString, string str) {
	size_t found = wholeString.find(str);
	if (found != string::npos)
		return found;
	else
		return -1;
}
int s_f_n(string wholeString, string str, int next) {
	size_t found = wholeString.find(str);
	if (found != string::npos) {
		found = wholeString.find(str, found + next);
		if (found != string::npos)
			return found;
		else
			return -1;
	}

}
int ch_c(string wholeString, char c) {
	size_t n = std::count(wholeString.begin(), wholeString.end(), c);
	return n;

}
int findRepeatFirstN2(string s)
{
	// this is O(N^2) method
	int p = -1, i, j;
	for (i = 0; i < s.size(); i++)
	{
		for (j = i + 1; j < s.size(); j++)
		{
			if (s[i] == s[j])
			{
				p = i;
				break;
			}
		}
		if (p != -1)
			break;
	}

	return p;
}
int main()
{
	int n, temp, tempResult = 0, count;
	string a;
	cin >> n;
	vi result, input;
	f(n) {
		cin >> temp;
		input.push_back(temp);
	}
	for (int i = 0; i < n; i++) {
		if (input[i] < 10) {
			result.push_back(input[i]);
		}
		else if (input[i] > 10) {
			string s;
			i_t_s(input[i], s);
			int a = (int)s[0] - 48;
			if (((int)s[1] - 48) > 0) {
				result.push_back((9*(s.size()-1)) + a);
			}
			else {
				result.push_back((9 * (s.size() - 1)));
			}
		}
	}
	v_cout(result);

}


//Halesh kon pesar hamishe behtarin boodi va khahi bood
//One Human
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <numeric>
using namespace std;
#define vi vector<int>
#define v_r(temp , first , second) replace (temp.begin(), temp.end(), first, second);
#define v_max(temp) *max_element(temp.begin(),temp.end())
#define v_min(temp) *min_element(temp.begin(),temp.end())
#define v_c(temp , element) count(temp.begin() , temp.end() , element)
#define v_av(temp) accumulate( temp.begin(), temp.end(), 0.0)/temp.size()
#define v_er1(temp,element) temp.erase(std::remove(temp.begin(), temp.end(), element), temp.end());
#define v_er2(temp,firstNumber) temp.erase(temp.begin() + firstNumber)
#define v_er3(temp,firstNumber,secondNumber) temp.erase(temp.begin() + firstNumber , temp.begin() + secondNumber)
#define s_t_i(string,number) stringstream geek(string);geek >> number
#define i_t_s(number , string) ostringstream str1;str1 << number; string = str1.str();
#define v_cin(_vector , number) int temp;for(int i=0;i<number;i++){cin>>temp;_vector.push_back(temp);}
#define v_cout(_vector) for(int i=0;i<_vector.size();i++){cout<<_vector[i]<<endl;}
#define f(number) for(int i=0;i<number;i++)

int v_f(vector<int>& vect, int element)
{
	int final;
	std::vector<int>::iterator it;
	it = std::find(vect.begin(), vect.end(), element);
	if (it != vect.end())
	{
		final = it - vect.begin();
		return final;
	}
	else
		return -1;
}
int s_f(string wholeString, string str) {
	size_t found = wholeString.find(str);
	if (found != string::npos)
		return found;
	else
		return -1;
}
int s_f_n(string wholeString, string str, int next) {
	size_t found = wholeString.find(str);
	if (found != string::npos) {
		found = wholeString.find(str, found + next);
		if (found != string::npos)
			return found;
		else
			return -1;
	}

}
int ch_c(string wholeString, char c) {
	size_t n = std::count(wholeString.begin(), wholeString.end(), c);
	return n;

}
int findRepeatFirstN2(string s)
{
	// this is O(N^2) method
	int p = -1, i, j;
	for (i = 0; i < s.size(); i++)
	{
		for (j = i + 1; j < s.size(); j++)
		{
			if (s[i] == s[j])
			{
				p = i;
				break;
			}
		}
		if (p != -1)
			break;
	}

	return p;
}
int main()
{
	int n, temp, tempResult = 0, count;
	string a = "", b;
	cin >> n;
	vi result, input;
	f(n) {
		cin >> temp;
		input.push_back(temp);
	}
	for (int i = 0; i < n; i++) {
		if (input[i] < 10) {
			result.push_back(input[i]);
		}
		else if (input[i] > 10) {
			string s;
			i_t_s(input[i], s);
			for (int h = 0; h < s.size(); h++) {
				a += "1";
			}
			int m;
			s_t_i(a, m);
			result.push_back((9 * (s.size() - 1)) + input[i] / m);
		}
	}
	v_cout(result);

}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;
ll arr[200005] ;

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans, p = 0 ;
    string st, sp;
    cin >> n ;
    ll len = to_string(n).size() ;
    for(int i = 1; i < 10; i++) {
      string ss = "" ;
      for(int j = 0; j < len; j++) {
        ss += to_string(i) ;
      }
      if(to_string(n) < ss) {
        stringstream geek(ss);
        geek >> p;
        break ;
      }
    }
    p-- ;
    if(n > 9) {
      y = p / 10 ;
      x = p % 10 ;
      len = to_string(y).size() ;
      ans = (len * 9) ;

      st = to_string(y) ;
      sp = to_string(x) ;
      if(x != 0) {
        if(sp[0] >= st[st.size() - 1]) {
          ans += (st[st.size() - 1] - '0') ;
        }
        else {
          ans += (st[st.size() - 1] - '0') - 1 ;
        }
      }
      else if(x == 0 and st[st.size() - 1] != '1'){
        if(st[st.size() - 1] == '0')  ans += (st[st.size() - 1] - '0') ;
        else ans += (st[st.size() - 1] - '0') - 1 ;
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }
  }
}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;
ll arr[200005] ;

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans ;
    string st, sp ;
    cin >> n ;
    if(n > 10) {
      y = n / 10 ;
      x = n % 10 ;
      ll len = to_string(y).size() ;
      ans = (len * 9) ;

      st = to_string(y) ;
      sp = to_string(x) ;
      if(x != 0) {
        if(sp[0] >= st[0]) {
          ans += (st[0] - '0') ;
        }
        else {
          ans += (st[0] - '0') - 1 ;
        }
      }
      else if(x == 0 and st[st.size() - 1] != '1'){
        ans += (st[st.size() - 1] - '0') - 1 ;
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }

  }
}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;
ll arr[200005] ;

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans ;
    string st, sp ;
    cin >> n ;
    if(n > 10) {
      y = n / 10 ;
      x = n % 10 ;
      ll len = to_string(y).size() ;
      ans = (len * 9) ;
      if(x != 0) {
        st = to_string(y) ;
        sp = to_string(x) ;
        if(sp[0] >= st[0]) {
          ans += (st[0] - '0') ;
        }
        else {
          ans += (st[0] - '0') - 1 ;
        }
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }

  }
}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;
ll arr[200005] ;

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans ;
    string st, sp ;
    cin >> n ;
    if(n > 9) {
      y = n / 10 ;
      x = n % 10 ;
      ll len = to_string(y).size() ;
      ans = (len * 9) ;

      st = to_string(y) ;
      sp = to_string(x) ;
      if(x != 0) {
        if(sp[0] >= st[0]) {
          ans += (st[0] - '0') ;
        }
        else {
          ans += (st[0] - '0') - 1 ;
        }
      }
      else if(x == 0 and st[st.size() - 1] != '1'){
        if(st[st.size() - 1] == '0')  ans += (st[st.size() - 1] - '0') ;
        else ans += (st[st.size() - 1] - '0') - 1 ;
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }

  }
}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;
ll arr[200005] ;

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans ;
    string st, sp ;
    cin >> n ;
    if(n > 10) {
      y = n / 10 ;
      x = n % 10 ;
      ll len = to_string(y).size() ;
      ans = (len * 9) ;

      st = to_string(y) ;
      sp = to_string(x) ;
      if(x != 0) {
        if(sp[0] >= st[0]) {
          ans += (st[0] - '0') ;
        }
        else {
          ans += (st[0] - '0') - 1 ;
        }
      }
      else if(x == 0 and st[st.size() - 1] != '1'){
        if(st[st.size() - 1] == '0')  ans += (st[st.size() - 1] - '0') ;
        else ans += (st[st.size() - 1] - '0') - 1 ;
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }

  }
}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;

int main()
{
  ll t = 10000 ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans, p = 0 ;
    string st, sp;
    cin >> n ;

    ll len = to_string(n).size() ;
    for(int i = 1; i < 10; i++) {
      string ss = "" ;
      for(int j = 0; j < len; j++) {
        ss += to_string(i) ;
      }
      if(to_string(n) < ss) {
        stringstream geek(ss);
        geek >> p;
        break ;
      }
    }
    if(p == 0) {
      string ss = "" ;
      for(int j = 0; j < len; j++) {
        ss += to_string(9) ;
      }
      stringstream geek(ss);
      geek >> p;
    }
    else  p-- ;

    if(n > 9) {
      y = p / 10 ;
      x = p % 10 ;
      len = to_string(y).size() ;
      ans = (len * 9) ;

      st = to_string(y) ;
      sp = to_string(x) ;
      if(x != 0) {
        if(sp[0] >= st[st.size() - 1]) {
          ans += (st[st.size() - 1] - '0') ;
        }
        else {
          ans += (st[st.size() - 1] - '0') - 1 ;
        }
      }
      else if(x == 0 and st[st.size() - 1] != '1'){
        if(st[st.size() - 1] == '0')  ans += (st[st.size() - 1] - '0') ;
        else ans += (st[st.size() - 1] - '0') - 1 ;
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }
  }
}


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

typedef long long ll;
#define PI 2 * acos (0.0)

ll MOD = 1e9 + 7 ;
ll arr[200005] ;

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n, x, y, ans ;
    string st, sp ;
    cin >> n ;
    if(n > 9) {
      y = n / 10 ;
      x = n % 10 ;
      ll len = to_string(y).size() ;
      ans = (len * 9) ;

      st = to_string(y) ;
      sp = to_string(x) ;
      if(x != 0) {
        if(sp[0] >= st[st.size() - 1]) {
          ans += (st[st.size() - 1] - '0') ;
        }
        else {
          ans += (st[st.size() - 1] - '0') - 1 ;
        }
      }
      else if(x == 0 and st[st.size() - 1] != '1'){
        if(st[st.size() - 1] == '0')  ans += (st[st.size() - 1] - '0') ;
        else ans += (st[st.size() - 1] - '0') - 1 ;
      }
      cout << ans << endl ;
    }
    else {
      cout << n << endl ;
    }
  }
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int  i=(a); i<(b);++i)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //cin.ignore();
    while(t--){
        int n,s=0;
        cin>>n;
        if(n<9) s=n;
        else{
             while(n>11){
            n/=10;
            s+=9;
        }
        if(n>9) n=9;
        s+=n;
        }

        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int  i=(a); i<(b);++i)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //cin.ignore();
    while(t--){
        int n,s=0;
        cin>>n;
        if(n<9) s=n;
        else{
             while(n>=11){
            n/=11;
            s+=9;
        }
        if(n>9) n=9;
        s+=n;
        }

        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int  i=(a); i<(b);++i)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //cin.ignore();
    while(t--){
        int n,s=0;
        cin>>n;
        while(n>99){
            n/=10;
            s+=9;
        }
        n/=11;
        s+=n;
        s+=9;
        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int  i=(a); i<(b);++i)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //cin.ignore();
    while(t--){
        int n,ans=0,b=0;
        cin>>n;
        f(i,1,10){
            b=b*10 + 1;
            f(j,1,10){
                if(b*j<=n) ans++;
                }
            }
            cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int  i=(a); i<(b);++i)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //cin.ignore();
    while(t--){
        int n,s=0;
        cin>>n;
        if(n<9) s=n;
        else{
             while(n>99){
            n/=10;
            s+=9;
        }
        n/=11;
        s+=n;
        s+=9;
        }

        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int  i=(a); i<(b);++i)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //cin.ignore();
    while(t--){
        int n,s=0;
        cin>>n;
        while(n>=11){
            n/=10;
            s+=9;
        }
        //cout<<n;
        if(n>9) n=9;
        s+=n;
        cout<<s<<endl;
    }
    return 0;
}


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

        int main() {
        	int t;
        	cin>>t;
        	while(t--){
        		long long n;
        		cin>>n;
        		int sum=0;
        		if(n<9)cout<<n<<endl;
        		else{
        			sum=0;
        			int s=100,q=0;
        			long long v=n;
        			while(n>9){
        				q=v%10;
        				while(q>10)q%=10;
        				v/=10;
        				s=min(s,q);
        				n/=10;
        				sum+=9;
        			}
        			//s=min(s,(int)v);
        			if(s<v)v-=1;
        			sum+=v;
        			cout<<sum<<endl;
        		}
        	}

        	return 0;
        }


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

int main() {
	int t;
	cin>>t;
	while(t--){
		long long n;
		cin>>n;
		int sum=0;
		sum+=min(9LL,n);
		if(n<9)cout<<sum<<endl;
		else{
			sum=0;
			int s=100,q=0;
			long long v=n;
			while(n>9){
				q=v%10;
				while(q>10)q%=10;
				v/=10;
				s=min(s,q);
				n/=10;
				sum+=9;
			}
			s=min(s,(int)v);
			sum+=s;
			cout<<sum<<endl;
		}
	}

	return 0;
}


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

    int main() {
    	int t;
    	cin>>t;
    	while(t--){
    		long long n;
    		cin>>n;
    		int sum=0;
    		if(n<9)cout<<n<<endl;
    		else{
    			sum=0;
    			int s=100,q=0;
    			long long v=n;
    			while(n>9){
    				q=v%10;
    				while(q>10)q%=10;
    				v/=10;
    				s=min(s,q);
    				n/=10;
    				sum+=9;
    			}
    			s=min(s,(int)v);
    			sum+=s;
    			cout<<sum<<endl;
    		}
    	}

    	return 0;
    }


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

    int main() {
    	int t;
    	cin>>t;
    	while(t--){
    		long long n;
    		cin>>n;
    		int sum=0;
    		if(n<=9)cout<<n<<endl;
    		else{
    			int s=100000000,q=0;
    			long long v=n;
    			while(n>9){
    				q=v%10;
    				while(q>10)q%=10;
    				v/=10;
    				s=min(s,q);
    				n/=10;
    				sum+=9;
    			}
    			s=min(s,(int)v);
    			sum+=s;
    			cout<<sum<<endl;
    		}
    	}

    	return 0;
    }


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

        int main() {
        	int t;
        	cin>>t;
        	while(t--){
        		long long n;
        		cin>>n;
        		long long vv=n;
        		int sum=0;
        		if(n<9)cout<<n<<endl;
        		else{
        			sum=0;
        			int s=100,q=0;
        			long long v=n;
        			int c=0;
        			while(n>9){
        				q=(v%10);
        				while(q>10)q%=10;
        				v/=10;
        				s=min(s,q);
        				n/=10;
        				sum+=9;
        				c++;
        			}c++;
        			int y=0;
        			while(c--){
        				y+=pow(10,c)*v;
        			}

        			if(vv<y)v--;
        			sum+=v;
        			cout<<sum<<endl;
        		}
        	}

        	return 0;
        }


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define forde(i,a, b) for(ll i=a;i>=b;--i)
#define forin(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const ll N = (int)1e5 + 1;

void solve(){
	ll n;cin>>n;
	if (n<10){
		cout<<n<<endl;
	}
	else if(n >=11){
		int ans = 0;
		ans += min(100LL, n) / 11;

		ans += min(1000LL, n) / 111;

		ans += min(10000LL, n) / 1111;

		ans += min(100000LL, n) / 11111;

		ans += min(1000000LL, n) / 111111;

		ans += min(10000000LL, n) / 1111111;

		ans += min(100000000LL, n) / 11111111;

		ans += min(1000000000LL, n) / 111111111;
		//ans += min(10000000000LL, n) / 1111111111;
		cout<<ans + 9 + (n==(ll)1e9?9:0)<<endl;
	} else{
		cout<<9<<endl;
	}
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;cin>>t;
	while(t--){
		ll n;cin>>n;
		char s[10] = {'0'};
		ll cnt = 0, te = 1;
		for(int i = 0; i < 10; ++i){
			s[i] = '1';
			te*=10;
			cnt += min(te - 1, n)/atoll(s);
		}
		cout<<cnt<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define ford(i,n) for(ll i=n;i>=1;--i)
#define forg(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const ll N = (int)1e5 + 1;

void solve(){
	int n;cin>>n;
	int cnt = 0;
	if(n<11){
		cout<<min(9, n)<<endl;
		return;
	}
	int k = n;
	while(k){
		cnt++;
		k/=10;
	}
	int div = 0;
	for0(i,cnt){
		div*=10;
		div+=1;
	}
	cout<<(cnt - 1)*9 + n/div<<endl;
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define forde(i,a, b) for(ll i=a;i>=b;--i)
#define forin(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const ll N = (int)1e5 + 1;

void solve(){
	ll n;cin>>n;
	if (n<10){
		cout<<n<<endl;
	}
	else if(n >=11){
		int ans = 0;
		ans += min(100LL, n) / 11;
		ans += min(1000LL, n) / 111;
		ans += min(10000LL, n) / 1111;
		ans += min(100000LL, n) / 11111;
		ans += min(1000000LL, n) / 111111;
		ans += min(10000000LL, n) / 1111111;
		ans += min(10000000LL, n) / 11111111;
		ans += min(1000000000LL, n) / 111111111;
		ans += min(10000000000LL, n) / 1111111111;
		cout<<ans + 9 + (n==(ll)1e9?9:0)<<endl;
	} else{
		cout<<9<<endl;
	}
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define forde(i,a, b) for(ll i=a;i>=b;--i)
#define forin(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const ll N = (int)1e5 + 1;

void solve(){
	ll n;cin>>n;
	if (n<10){
		cout<<n<<endl;
	}
	else if(n >=11){
		int ans = 0;
		ans += min(100LL, n) / 11;

		ans += min(1000LL, n) / 111;

		ans += min(10000LL, n) / 1111;

		ans += min(100000LL, n) / 11111;

		ans += min(1000000LL, n) / 111111;

		ans += min(10000000LL, n) / 1111111;

		ans += min(100000000LL, n) / 11111111;

		ans += min(1000000000LL, n) / 111111111;
		//ans += min(10000000000LL, n) / 1111111111;
		cout<<ans + 9 <<endl;
	} else{
		cout<<9<<endl;
	}
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
unsigned long long pwr(int base, int exponent)
{
    unsigned long long v=base;
    for(int i=0; i<exponent; i++)
    {
        v*=base;
    }
    return v;
}

using namespace std;

int main()
{
    int t;
    cin>>t;
    int n[10000];
    for(int i=0; i<t; i++)
    {
        cin>>n[i];
    }
    for(int i=0; i<t; i++)
    {
        int d, r;
        d=log10(n[i]);
        r=d*9;
        r+=n[i]/((pwr(10, d)-1)/9);
        cout<<r<<"\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    cin>>k;
}


#include <bits/stdc++.h>
unsigned long long pwr(int base, int exponent)
{
    for(int i=0; i<exponent; i++)
    {
        base*=base;
    }
    return base;
}

using namespace std;

int main()
{
    int t;
    cin>>t;
    int n[10000];
    for(int i=0; i<t; i++)
    {
        cin>>n[i];
    }
    for(int i=0; i<t; i++)
    {
        int d, r;
        d=log10(n[i]);
        r=d*9;
        r+=n[i]/((pwr(10, d)-1)/9);
        cout<<r<<"\n";
    }
}


#include <bits/stdc++.h>
long long pwr(int base, int exponent)
{
    for(int i=0; i<exponent; i++)
    {
        base*=base;
    }
    return base;
}

using namespace std;

int main()
{
    int t;
    cin>>t;
    int n[10000];
    for(int i=0; i<t; i++)
    {
        cin>>n[i];
    }
    for(int i=0; i<t; i++)
    {
        int d, r;
        d=log10(n[i]);
        r=d*9;
        r+=n[i]/((pwr(10, d)-1)/9);
        cout<<r<<"\n";
    }
}


#include <bits/stdc++.h>
long long pwr(long long base, long long exponent)
{
    for(int i=0; i<exponent; i++)
    {
        base*=base;
    }
    return base;
}

using namespace std;

int main()
{
    int t;
    cin>>t;
    long long n[10000];
    for(int i=0; i<t; i++)
    {
        cin>>n[i];
    }
    for(int i=0; i<t; i++)
    {
        long long d, r;
        d=log10(n[i]);
        r=d*9;
        r+=n[i]/((pwr(10, d)-1)/9);
        cout<<r<<"\n";
    }
}


#include<iostream>
#include<algorithm>
#include<cmath>
using  namespace std;
#define ll long long
#define ld long double
const int maxn=3e5+1;

ll t,n,i,cnt;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("test.INP","r",stdin);
  	cin>>t; cnt=0;
  	while(t--){
  		cin>>n; cnt=0;
		i=1;
		while(i<=n){
			cnt++;
			i=i*10+1;
		}
		i=(i-1)/10;
		cout<<i<<"x"<<cnt<<"x";
		if (n%i!=0) cout<<n/i+(cnt-1)*9<<"\n";
		else cout<<n/i+(cnt-1)*9-1<<"\n";
	}
}


#include<iostream>
#include<algorithm>
#include<cmath>
using  namespace std;
#define ll long long
#define ld long double
const int maxn=3e5+1;

ll t,n,i,cnt;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("test.INP","r",stdin);
  	cin>>t; cnt=0;
  	while(t--){
  		cin>>n; cnt=0;
		i=1;
		while(i<=n){
			cnt++;
			i=i*10+1;
		}
		i=(i-1)/10;
		if (n%10!=0) cout<<n/i+(cnt-1)*9<<"\n";
		else cout<<n/i+(cnt-1)*9-1<<"\n";
	}
}


#include<iostream>
#include<algorithm>
#include<string.h>
using  namespace std;
#define ll long long
#define ld long double
const int maxn=3e5+1;
ll mod=1000000007;
string a;
ll t,n,x,ans=0,sr,sl;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cin>>t;
	while(t--){
		ans=0;
		cin>>n;
		x=1;
		while(x*10+1<n){
			ans+=9;
			x=x*10+1;

		}

		cout<<n/x + ans<<"\n";
	}
}


#include<iostream>
#include<algorithm>
#include<cmath>
using  namespace std;
#define ll long long
#define ld long double
const int maxn=3e5+1;

ll t,n,i,cnt;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("test.INP","r",stdin);
  	cin>>t; cnt=0;
  	while(t--){
  		cin>>n; cnt=0;
		i=1;
		while(i<=n){
			cnt++;
			i=i*10+1;
		}
		i=(i-1)/10;
		cout<<n/i+(cnt-1)*9<<"\n";
	}
}


#include<iostream>
#include<algorithm>
#include<cmath>
using  namespace std;
#define ll long long
#define ld long double
const int maxn=3e5+1;

ll t,n,i,cnt,s;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("test.INP","r",stdin);
  	cin>>t; cnt=0;
  	while(t--){
  		cin>>n; s=n; cnt=0;
		i=0;
		while(n>0){
			cnt++;
			n/=10;
		}
		for (ll j=0;j<cnt;j++) i=i*10+1;
		cout<<s/i+(cnt-1)*9<<"\n";
	}
}


#include<iostream>
#include<algorithm>
#include<cmath>
using  namespace std;
#define ll long long
#define ld long double
const int maxn=3e5+1;

ll t,n,i,cnt;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("test.INP","r",stdin);
  	cin>>t; cnt=0;
  	while(t--){
  		cin>>n; cnt=0;
		i=1;
		while(i<=n){
			cnt++;
			i=i*10+1;
		}
		i=(i-1)/10;
	//	cout<<i<<"x"<<cnt<<"x";
		if (n%i!=0) cout<<n/i+(cnt-1)*9<<"\n";
		else cout<<n/i+(cnt-1)*9-1<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define eb emplace_back
#define mp make_pair
using namespace std;
vector<int> v;
int main(){
    int _,x;
    for(cin>>_;_;_--){
        cin>>x;
        v.clear();
        int ans=0,left=10;
        while(x>=10){
            left=x%10;
            v.eb(left);
            x/=10;
            ans+=9;
        }
        reverse(v.begin(),v.end());
        for(int i=0;i<v.size();i++){
            int y=v[i];
            if(y>x){
                ans+=x;
                break;
            }else if(y<x){
                ans+=x-1;
                break;
            }else if(i==v.size()-1){
                ans+=x;
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define eb emplace_back
#define mp make_pair
using namespace std;
int main(){
    int _,x;
    for(cin>>_;_;_--){
        cin>>x;
        int ans=0,left=10;
        while(x>=10){
            left=min(left,x%10);
            x/=10;
            ans+=9;
        }
        ans+=min(x,left);
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define eb emplace_back
#define mp make_pair
using namespace std;
int main(){
    int _,x;
    for(cin>>_;_;_--){
        cin>>x;
        int ans=0,left=10;
        while(x>=10){
            left=x%10;
            x/=10;
            ans+=9;
        }
        if(left>=x)
            ans+=x;
        else
            ans+=x-1;
        cout<<ans<<endl;
    }
    return 0;
}


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

const int MAXN=1000000;
const int MAXS=2000000;
const int MAXW=20;
int N,A[MAXN+5],f[MAXS+5][2],ans;

void update(int x,int pos);
void dp();
void Input();
int main()
{
	Input();
	dp();
	printf("%d\n",ans);
	return 0;
}
void update(int S,int pos)
{
//	if(f[S][0]<pos && pos!=f[S][1]) f[S][0]=pos;
//	if(f[S][0]>f[S][1]) swap(f[S][0],f[S][1]);
	if(f[S][1]<pos)
	{
		f[S][0]=f[S][1];
		f[S][1]=pos;
	}
	else if(f[S][0]<pos && pos!=f[S][1])
		f[S][0]=pos;
	return;
}
void dp()
{
	ans=0;
	for(int i=1;i<=N;i++)//i<j<k, 所以从i开始向后
		f[A[i]][1]=i;//A[i]是A[i]的子集(边界)
	for(int S=MAXS;S>=0;S--)//倒序枚举，这样可以先更新S再更新S的子集
		for(int i=MAXW;i>=0;i--)
			if(S&(1<<i))//枚举S的元素
			{//用S的可选集合 更新与S仅差一个元素的子集的可选集合
				update(S^(1<<i),f[S][1]);
				update(S^(1<<i),f[S][0]);
			}
	for(int i=1;i<=N-2;i++)
	{
		int w=0;
		for(int j=MAXW;j>=0;j--)//找到Cu(A[i])的元素
			if((A[i]&(1<<j))==0 && f[w|(1<<j)][1]>i && f[w|(1<<j)][0]>i)//贪心地从Cu(A[i])的最高位选起
				w=w|(1<<j);
		ans=max(ans,A[i]|w);
	}
	return;
}
void Input()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
		scanf("%d",&A[i]);
	return;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001, fullVal = (1 << 21) - 1;
typedef pair<int, int> Pair;
Pair f[1 << 21];
int A[maxn];
#define getbit(x) (1 << ((x) - 1))
inline void add(Pair& des, Pair org) {
    if (des.second < org.second) {
        swap(des.second, des.first);
        des.second = org.second;
    }
    if (des.first < org.first) {
        des.first = org.first;
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i) {
	    cin >> A[i];
	    add(f[A[i]], make_pair(0, i));
    }
    for (int i = fullVal; i; --i) {
        for (int j = 1; j <= 21; ++j) {
            if (getbit(j) & i) add(f[i ^ getbit(j)], f[i]);
        }
    }
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
        int s = 0;
        for (int j = 21; j; --j) {
            if (f[s | getbit(j)].first > i) s |= getbit(j);
        }
        ans = max(ans, A[i] | s);
    }
    cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001, fullVal = (1 << 21) - 1;
typedef pair<int, int> Pair;
Pair f[1 << 21];
int A[maxn];
#define getbit(x) (1 << ((x) - 1))
inline void add(Pair& des, Pair org) {
    if (org.first == des.first || org.first == des.second) {
        org.first = 0;
    }
    if (org.second == des.first || org.second == des.second) {
        org.second = 0;
        swap(org.first, org.second);
    }
    if (des.second <= org.second) {
        swap(des.second, des.first);
        des.second = org.second;
        swap(org.first, org.second);
    }
    if (des.first < org.second) {
        des.first = org.second;
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i) {
	    cin >> A[i];
	    add(f[A[i]], make_pair(0, i));
    }
    for (int i = fullVal; i; --i) {
        for (int j = 1; j <= 21; ++j) {
            if (getbit(j) & i) add(f[i ^ getbit(j)], f[i]);
        }
        assert(f[i].first <= f[i].second);
    }
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
        int s = 0;
        for (int j = 21; j; --j) {
            if (!(getbit(j) & s) && f[s | getbit(j)].first > i) s |= getbit(j);
        }
        ans = max(ans, (A[i] | s));
    }
    cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e6+50;
const int maxa=2e6+50;
int n,a[maxn];
PII dp[maxa];
void updata(int x,int pos)
{
    if(dp[x].first<pos)
    {
        dp[x].second=dp[x].first;
        dp[x].first=pos;
    }
    else if(dp[x].second<pos&&pos!=dp[x].first)  //注意first和second的位置不能相同
        dp[x].second=pos;
}
void get_dp()
{
    for(int x=maxa-1;x>=0;x--)   //从大到小更新
    {
        for(int i=20;i>=0;i--)
        {
            if(((x>>i)&1)==1)    //枚举x为1的二进制位
            {
                updata(x^(1<<i),dp[x].first);     //更新 把x的一个为1的二进制位变为0的子集
                updata(x^(1<<i),dp[x].second);
            }
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        updata(a[i],i);
    }
    get_dp();
    int ans=0;
    for(int i=1;i<=n-2;i++)
    {
        int w=0;
        for(int j=20;j>=0;j--)
        {       //找到a[i]为0的二进制位
            if(((a[i]>>j)&1)==0&&dp[w|(1<<j)].first>i&&dp[w|(1<<j)].second>i)
                w=w|(1<<j);
        }
        ans=max(ans,a[i]|w);
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
#define LG 22
#define N (1<<LG)
#define P 1000000007
#define pb push_back
#define mp make_pair
#define re(x, n) for (int x = 0; x < (n); ++x)
#define rep(x, l, r) for (int x = (l); x <= (r); ++x)
#define repd(x, r, l) for (int x = (r); x >= (l); --x)

int getint() {
	int x = 0, s = 1; char d = getchar();
	while (d < '0' || d > '9') { if (d == '-') s = -1; d = getchar(); }
	while (d >= '0' && d <= '9') x = x * 10 + d - '0', d = getchar();
	return x * s;
}

using namespace std;
typedef pair<int, int> pii;

int n, a[N];
pii f[N];

void udt(int x, int v) {
    if (!v) return;
    pii &pi = f[x];
    if (v > pi.first) {
        pi.second = pi.first;
        pi.first = v;
    } else if (v > pi.second && v != pi.first) {
        pi.second = v;
    }
}

int main() {
    n = getint();
    re(i, n) a[i] = getint(), udt(a[i], i + 1);
    repd(i, N - 1, 0) {
        if (!f[i].first) continue;
        for (int j = 0; j < LG; ++j) {
            if (!(i&(1<<j))) continue;
            udt(i^(1<<j), f[i].first);
            udt(i^(1<<j), f[i].second);
        }
    }
    int ans = 0;
    re(i, n - 2) {
        int mx = 0;
        for (int j = LG - 1; ~j; --j) {
            if (a[i]&(1<<j)) continue;
            if (f[mx|(1<<j)].second > i + 1) mx |= 1<<j;
        }
        ans = max(ans, a[i] | mx);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e6+50;
const int maxa=2e6+50;
int n,a[maxn];
PII dp[maxa];
void updata(int x,int pos)
{
    if(dp[x].second<pos && pos!=dp[x].first) dp[x].second=pos;
	if(dp[x].second>dp[x].first) swap(dp[x].second,dp[x].first);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        dp[a[i]].first=i;
    }
    for(int x=maxa-1;x>=0;x--)   //从大到小更新
    {
        for(int i=20;i>=0;i--)
        {
            if(((x>>i)&1)==1)    //枚举x为1的二进制位
            {
                updata(x^(1<<i),dp[x].first);     //更新 把x的一个为1的二进制位变为0的子集
                updata(x^(1<<i),dp[x].second);
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n-2;i++)
    {
        int w=0;
        for(int j=20;j>=0;j--)
        {       //找到a[i]为0的二进制位
            if(((a[i]>>j)&1)==0&&dp[w|(1<<j)].first>i&&dp[w|(1<<j)].second>i)
                w=w|(1<<j);
        }
        ans=max(ans,a[i]|w);
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e6+50;
const int maxa=2e6+50;
int n,a[maxn];
PII dp[maxa];
void updata(int x,int pos)
{
    if(dp[x].second<pos && pos!=dp[x].first) dp[x].second=pos;
	if(dp[x].second>dp[x].first) swap(dp[x].second,dp[x].first);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        updata(a[i],i);
    }
    for(int x=maxa-1;x>=0;x--)   //从大到小更新
    {
        for(int i=20;i>=0;i--)
        {
            if(((x>>i)&1)==1)    //枚举x为1的二进制位
            {
                updata(x^(1<<i),dp[x].first);     //更新 把x的一个为1的二进制位变为0的子集
                updata(x^(1<<i),dp[x].second);
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n-2;i++)
    {
        int w=0;
        for(int j=20;j>=0;j--)
        {       //找到a[i]为0的二进制位
            if(((a[i]>>j)&1)==0&&dp[w|(1<<j)].first>i&&dp[w|(1<<j)].second>i)
                w=w|(1<<j);
        }
        ans=max(ans,a[i]|w);
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001, fullVal = (1 << 21) - 1;
typedef pair<int, int> Pair;
Pair f[1 << 21];
int A[maxn];
#define getbit(x) (1 << ((x) - 1))
inline void add(Pair& des, Pair org) {
    if (des.second <= org.second) {
        swap(des.second, des.first);
        des.second = org.second;
        swap(org.first, org.second);
    }
    if (des.first < org.second) {
        des.first = org.second;
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i) {
	    cin >> A[i];
	    add(f[A[i]], make_pair(0, i));
    }
    for (int i = fullVal; i; --i) {
        for (int j = 1; j <= 21; ++j) {
            if (getbit(j) & i) add(f[i ^ getbit(j)], f[i]);
        }
        assert(f[i].first <= f[i].second);
    }
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
        int s = 0;
        for (int j = 21; j; --j) {
            if (!(getbit(j) & s) && f[s | getbit(j)].first > i) s |= getbit(j);
        }
        ans = max(ans, (A[i] | s));
    }
    cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define re register
using namespace std;
const int maxn=1e6+5;
const int maxm=(1<<21)+5;
int a[maxn],n,wei,tmp[3];
pair<int,int>dp[maxm];

void Max(int &a,int b) {
	if(a<b)a=b;
}

pair<int,int> operator+(const pair<int,int> &a,const int &b) {
	if(!b)return a;
	tmp[0]=a.first,tmp[1]=a.second,tmp[2]=b;
	sort(tmp,tmp+3);
	return make_pair(tmp[1],tmp[2]);
}

pair<int,int> operator+(const pair<int,int> &a,const pair<int,int> &b) {
	a=a+b.first+b.second;
	return a;
}

bool check(pair<int,int> x,int pr) {
	return x.second&&x.first>pr;
}

int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)scanf("%d",&a[i]),dp[a[i]]=dp[a[i]]+i,Max(wei,(int)log2(a[i]));
	int w=(1<<wei+1)-1;
//	printf("wei : %d,w : %d\n",wei,w);
	for(int i=0; i<=wei; i++)
		for(re int j=0; j<=w; j++)
			if(j&(1<<i))dp[j^(1<<i)]=dp[j^(1<<i)]+dp[j];
	int ans=0;
	for(int i=1; i<=n; i++) {
		int step=w-a[i],S=0;
		for(int j=wei; j>=0; j--)if(step&(1<<j)&&check(dp[S|(1<<j)],i))S|=(1<<j);
		if(check(dp[S],i))Max(ans,S|a[i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
#define N 1048577
int n,a[N],f[20][N],ans;
void Ins(int x,int k)
{
	++f[k][x];if(f[k][x]>=2)return;if(k>=19)return;
	Ins(x,k+1);if((x>>k)&1)Ins(x^(1<<k),k+1);
}
int main(){
	scanf("%d",&n);int i,j,x,res,t;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--)
	{
		res=t=0;x=a[i];
		for(j=19;j>=0;j--)
		if((x>>j)&1)res|=(1<<j);
		else if(f[j][t|(1<<j)]>=2){res|=(1<<j);t|=(1<<j);}
		ans=max(ans,res);Ins(x,0);
	}printf("%d",ans);return 0;
}


#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<set>
#include<unordered_map>
#include<vector>
typedef long long ll;
using namespace std;
#define N 1000010
#define M 2097155
int cnt[M],a[N],vis[N];
void get(int step,int x){
	int lb,y;
	if(vis[x]==step)return;
	vis[x]=step;
	if(++cnt[x]>2)return;
	while(x){
		lb=x&(-x);
		get(step,(x-=lb)|y);
		y+=lb;
	}
}
int main(){
	int i,j,n,res=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--){
		for(j=a[i];j;j=(j-1)&a[i])cnt[j]++;
		if(n-i+1<=2)continue;
		int val=0;
		for(j=20;j>=0;j--){
			if(a[i]&(1<<j))continue;
			if(cnt[val|(1<<j)]>=2)val|=1<<j;
		}
		res=max(res,val|a[i]);
	}
	printf("%d",res);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<set>
#include<unordered_map>
#include<vector>
typedef long long ll;
using namespace std;
#define N 1000010
#define M 2097155
int cnt[M],a[N],vis[N];
void get(int step,int x){
	int lb,y;
	if(vis[x]==step)return;
	vis[x]=step;
	if(++cnt[x]>2)return;
	while(x){
		lb=x&(-x);
		get(step,(x-=lb)|y);
		y+=lb;
	}
}
int main(){
	int i,j,n,res=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--){
		get(i,a[i]);
		if(n-i+1<=2)continue;
		int val=0;
		for(j=20;j>=0;j--){
			if(a[i]&(1<<j))continue;
			if(cnt[val|(1<<j)]>=2)val|=1<<j;
		}
		res=max(res,val|a[i]);
	}
	printf("%d",res);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<set>
#include<unordered_map>
#include<vector>
typedef long long ll;
using namespace std;
#define N 1000010
#define M 2097155
int cnt[M],a[N],vis[N];
void get(int step,int x){
	int lb,y=0;
	if(vis[x]==step)return;
	vis[x]=step;
	if(++cnt[x]>2)return;
	while(x){
		lb=x&(-x);
		get(step,(x-=lb)|y);
		y+=lb;
	}
}
int main(){
	int i,j,n,res=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--){
		get(i,a[i]);
		if(n-i+1<=2)continue;
		int val=0;
		for(j=20;j>=0;j--){
			if(a[i]&(1<<j))continue;
			if(cnt[val|(1<<j)]>=2)val|=1<<j;
		}
		res=max(res,val|a[i]);
	}
	printf("%d",res);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<set>
#include<unordered_map>
#include<vector>
typedef long long ll;
using namespace std;
#define N 1000010
#define M 2097155
int cnt[M],a[N];
int main(){
	int i,j,n,res=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--){
		for(j=a[i];j;j=(j-1)&a[i])cnt[j]++;
		if(n-i+1<=2)continue;
		int val=0;
		for(j=20;j>=0;j--){
			if(a[i]&(1<<j))continue;
			if(cnt[val|(1<<j)]>=2)val|=1<<j;
		}
		res=max(res,val|a[i]);
	}
	printf("%d",res);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n,a[N],ans;
int cnt[1<<21];
void Upd(int x,int k){
    if(cnt[x]>1)return;
    if(k==21)return void(cnt[x]++);
    if(1<<k&x)Upd(1<<k^x,k+1);
    Upd(x,k+1);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
        int now=0,res=0;
        for(int j=20;j>=0;j--)
            if(1<<j&a[i])res|=1<<j;
            else if(cnt[1<<j|now]>1)res|=1<<j,now|=1<<j;
        if(i<n-1)ans=max(ans,res);
    }
    printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n,a[N],ans;
int cnt[1<<21];
void Upd(int x,int k){
    if(cnt[x]>1)return;
    if(k==21)return void(cnt[x]++);
    if(1<<k&x)Upd(1<<k^x,k+1);
    Upd(x,k+1);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
        int now=0,res=0;
        for(int j=20;j>=0;j--)
            if(1<<j&a[i])res|=1<<j;
            else if(cnt[1<<j|now]>1)res|=1<<j,now|=1<<j;
        if(i<n-1)ans=max(ans,res);
        Upd(a[i],0);
    }
    printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n,a[N],ans;
int cnt[(1<<20)+5];
void Upd(int x,int k){
    if(cnt[x]>1)return;
    if(k==20)return void(cnt[x]++);
    if(1<<k&x)Upd(1<<k^x,k+1);
    Upd(x,k+1);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
        int now=0,res=0;
        for(int j=19;j>=0;j--)
            if(1<<j&a[i])res|=1<<j;
            else if(cnt[1<<j|now]>1)res|=1<<j,now|=1<<j;
        Upd(a[i],0);
        ans=max(ans,res);
    }
    printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n,a[N],ans;
int cnt[1<<20];
void Upd(int x,int k){
    if(cnt[x]>1)return;
    if(k==20)return void(cnt[x]++);
    if(1<<k&x)Upd(1<<k^x,k+1);
    Upd(x,k+1);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
        int now=0,res=0;
        for(int j=19;j>=0;j--)
            if(1<<j&a[i])res|=1<<j;
            else if(cnt[1<<j|now]>1)res|=1<<j,now|=1<<j;
        Upd(a[i],0);
        ans=max(ans,res);
    }
    printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n,a[N],ans;
int cnt[1<<21];
void Upd(int x,int k){
    if(cnt[x]>1)return;
    if(k==21)return void(cnt[x]++);
    if(1<<k&x)Upd(1<<k^x,k+1);
    Upd(x,k+1);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
        int now=0,res=0;
        for(int j=20;j>=0;j--)
            if(1<<j&a[i])res|=1<<j;
            else if(cnt[1<<j|now]>1)res|=1<<j,now|=1<<j;
        Upd(a[i],0);
        ans=max(ans,res);
    }
    printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n,a[N],ans;
int cnt[1<<21];
void Upd(int x,int k){
    if(cnt[x]>1)return;
    if(k==20)return void(cnt[x]++);
    if(1<<k&x)Upd(1<<k^x,k+1);
    Upd(x,k+1);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
        int now=0,res=0;
        for(int j=19;j>=0;j--)
            if(1<<j&a[i])res|=1<<j;
            else if(cnt[1<<j|now]>1)res|=1<<j,now|=1<<j;
        Upd(a[i],0);
        ans=max(ans,res);
    }
    printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a[1000005],dp[10000005][2],f[10000005];
int maxn=(1<<22),ans;
inline void upd(int now,int val){
	if (val>dp[now][0])dp[now][1]=dp[now][0],dp[now][0]=val;
	else if (val>dp[now][1]&&val!=dp[now][0])dp[now][1]=val;
	return;
}
inline bool chk(int num){
	for (register int i=1;i<=maxn;i++)
		if (dp[i][1]>f[num^(num&i)])return 1;
	return 0;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++)scanf("%d",&a[i]);
	memset(f,0x7f,sizeof(f));
	for (int i=1;i<=n;i++)f[a[i]]=min(f[a[i]],i),upd(a[i],i);
	for (register int i=maxn;i>=1;i--)
		for (register int j=0;j<=22;j++)
			if ((i&(1<<j))!=0){
				upd((i^(1<<j)),dp[i][0]);
				upd((i^(1<<j)),dp[i][1]);
				f[i^(1<<j)]=min(f[i^(1<<j)],f[i]);
			}
	for (register int i=22;i>=0;i--)
		if (chk(ans+(1<<i)))ans+=(1<<i);
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a[1000005],dp[10000005][2],f[10000005];
int maxn=(1<<21),ans;
inline void upd(int now,int val){
	if (val>dp[now][0])dp[now][1]=dp[now][0],dp[now][0]=val;
	else if (val>dp[now][1]&&val!=dp[now][0])dp[now][1]=val;
	return;
}
inline bool chk(int num){
	for (register int i=0;i<=maxn;i++)
		if (dp[i][1]>f[num^(num&i)])return 1;
	return 0;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++)scanf("%d",&a[i]);
	memset(f,0x7f,sizeof(f));
	for (int i=1;i<=n;i++)f[a[i]]=min(f[a[i]],i),upd(a[i],i);
	for (register int i=maxn;i>=0;i--)
		for (register int j=0;j<=20;j++)
			if ((i&(1<<j))!=0){
				upd((i^(1<<j)),dp[i][0]);
				upd((i^(1<<j)),dp[i][1]);
				f[i^(1<<j)]=min(f[i^(1<<j)],f[i]);
			}
	for (register int i=20;i>=0;i--)
		if (chk(ans+(1<<i)))ans+=(1<<i);
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a[1000005],dp[10000005][2],f[10000005];
int maxn=(1<<21),ans;
inline void upd(int now,int val){
	if (val>dp[now][0])dp[now][1]=dp[now][0],dp[now][0]=val;
	else if (val>dp[now][1]&&val!=dp[now][0])dp[now][1]=val;
	return;
}
inline bool chk(int num){
	for (register int i=1;i<=maxn;i++)
		if (dp[i][1]>f[num^(num&i)])return 1;
	return 0;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++)scanf("%d",&a[i]);
	memset(f,0x7f,sizeof(f));
	for (int i=1;i<=n;i++)f[a[i]]=min(f[a[i]],i),upd(a[i],i);
	for (register int i=maxn;i>=1;i--)
		for (register int j=0;j<=21;j++)
			if ((i&(1<<j))!=0){
				upd((i^(1<<j)),dp[i][0]);
				upd((i^(1<<j)),dp[i][1]);
				f[i^(1<<j)]=min(f[i^(1<<j)],f[i]);
			}
	for (register int i=21;i>=0;i--)
		if (chk(ans+(1<<i)))ans+=(1<<i);
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a[1000005],dp[10000005][2],f[10000005];
int maxn=(1<<21),ans;
inline void upd(int now,int val){
	if (val>dp[now][0])dp[now][1]=dp[now][0],dp[now][0]=val;
	else if (val>dp[now][1]&&val!=dp[now][0])dp[now][1]=val;
	return;
}
inline bool chk(int num){
	for (register int i=1;i<=maxn;i++)
		if (dp[i][1]>f[num^(num&i)])return 1;
	return 0;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++)scanf("%d",&a[i]);
	memset(f,0x7f,sizeof(f));
	for (int i=1;i<=n;i++)f[a[i]]=min(f[a[i]],i),upd(a[i],i);
	for (register int i=maxn;i>=1;i--)
		for (register int j=0;j<=20;j++)
			if ((i&(1<<j))!=0){
				upd((i^(1<<j)),dp[i][0]);
				upd((i^(1<<j)),dp[i][1]);
				f[i^(1<<j)]=min(f[i^(1<<j)],f[i]);
			}
	for (register int i=20;i>=0;i--)
		if (chk(ans+(1<<i)))ans+=(1<<i);
	cout<<ans<<endl;
	return 0;
}


#pragma comment(linker, "/STACK:640000000")
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)2e6 + 10;

int a[maxn];
int t[maxn];
int res = 0;

void dfs(int val, int pos) {
	if (t[val] >= 2) return;
	t[val]++;
	for (int i = pos; i < 21; i++) if (val & (1 << i)) {
		dfs(val ^ (1 << i), i + 1);
	}
}

void solve(int val) {
	int cur = 0;
	for (int i = 20; i >= 0; i--) if ((val & (1 << i)) == 0) {
		if (t[cur ^ (1 << i)] >= 2) {
			cur ^= 1 << i;
		}
	}
	res = max(res, cur | val);
}

int main() {
	#ifdef LOCAL
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	int n; cin >> n;
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = n - 1; i >= 0; i--) {
		solve(a[i]);
		dfs(a[i], 0);
	}

	cout << res << endl;

	return 0;
}


#pragma comment(linker, "/STACK:640000000")
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)2e6 + 10;

int a[maxn];
int t[maxn];
int res = 0;

void dfs(int val, int pos) {
	if (t[val] >= 2) return;
	t[val]++;
	for (int i = pos; i < 21; i++) if (val & (1 << i)) {
		dfs(val ^ (1 << i), i + 1);
	}
}

void solve(int val) {
	if (t[0] < 2) return;
	int cur = 0;
	for (int i = 20; i >= 0; i--) if ((val & (1 << i)) == 0) {
		if (t[cur ^ (1 << i)] >= 2) {
			cur ^= 1 << i;
		}
	}
	res = max(res, cur | val);
}

int main() {
	#ifdef LOCAL
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	int n; cin >> n;
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = n - 1; i >= 0; i--) {
		solve(a[i]);
		dfs(a[i], 0);
	}

	cout << res << endl;

	return 0;
}


#pragma comment(linker, "/STACK:640000000")
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)2e6 + 10;

int a[maxn];
int t[maxn];
int res = 0;

void dfs(int val, int pos) {
	t[val]++;
	if (t[val] >= 2) return;
	for (int i = pos; i < 21; i++) if (val & (1 << i)) {
		dfs(val ^ (1 << i), pos + 1);
	}
}

void solve(int val) {
	int cur = 0;
	for (int i = 20; i >= 0; i--) if ((val & (1 << i)) == 0) {
		if (t[cur ^ (1 << i)] >= 2) {
			cur ^= 1 << i;
		}
	}
	res = max(res, cur | val);
}

int main() {
	#ifdef LOCAL
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	int n; cin >> n;
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = n - 1; i >= 0; i--) {
		solve(a[i]);
		dfs(a[i], 0);
	}

	cout << res << endl;

	return 0;
}


#pragma comment(linker, "/STACK:640000000")
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)1e6 + 10;

int a[maxn];
int t[4 * maxn];
int res = 0;

void dfs(int val) {
	t[val]++;
	if (t[val] >= 2) return;
	for (int i = 0; i < 21; i++) if (val & (1 << i)) {
		dfs(val ^ (1 << i));
	}
}

void solve(int val) {
	int cur = 0;
	for (int i = 20; i >= 0; i--) if ((val & (1 << i)) == 0) {
		if (t[cur ^ (1 << i)] >= 2) {
			cur ^= 1 << i;
		}
	}
	res = max(res, cur | val);
}

int main() {
	#ifdef LOCAL
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	int n; cin >> n;
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = n - 1; i >= 0; i--) {
		solve(a[i]);
		dfs(a[i]);
	}

	cout << res << endl;

	return 0;
}


#pragma comment(linker, "/STACK:640000000")
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)2e6 + 10;

int a[maxn];
int t[maxn];
int res = 0;

void dfs(int val, int pos) {
	t[val]++;
	if (t[val] >= 2) return;
	for (int i = pos; i < 22; i++) if (val & (1 << i)) {
		dfs(val ^ (1 << i), pos + 1);
	}
}

void solve(int val) {
	int cur = 0;
	for (int i = 21; i >= 0; i--) if ((val & (1 << i)) == 0) {
		if (t[cur ^ (1 << i)] >= 2) {
			cur ^= 1 << i;
		}
	}
	res = max(res, cur | val);
}

int main() {
	#ifdef LOCAL
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	int n; cin >> n;
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = n - 1; i >= 0; i--) {
		solve(a[i]);
		dfs(a[i], 0);
	}

	cout << res << endl;

	return 0;
}


#include <cstdio>

#include <algorithm>
using namespace std;

const int N = 2e6 + 5;
int n, ans;

int a[N];

int f[(1 << 21)][21];

void modify(int s, int k) {

	if (k > 20) return;

	//if (f[s][k] >= 2) return;

	f[s][k]++;

	modify(s, k + 1);

	if (s & (1 << k))

		modify(s ^ (1 << k), k);

}

int main() {

	scanf("%d\n", &n);

	for (int i = 1; i <= n; ++i)

		scanf("%d", a + i);

	for (int i = n; i; --i) {

		int tmp = 0, now = 0;

		if (i < n - 1) {

			for (int j = 20; ~j; --j)

				if (a[i] & (1 << j))

					tmp |= (1 << j);

				else

					if (f[now | (1 << j)][20] >= 2)

						tmp |= (1 << j), now |= (1 << j);

		}
		modify(a[i], 0);

		ans = max(ans, tmp);

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

	return 0;

}


#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker,"/STACK:102400000,102400000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#define FILENAME(x) \
freopen(x".in","r",stdin); \
freopen(x".out","w",stdout);
#define MAXIOLG 25
#define IOBUFSZ 1000000
#define IOBUFSZZ 1000005
#define LOWBIT(x) ((x)&(-(x)))
#define MD(x) (((x)>=MOD)?((x)-=MOD):(0))
#define MAXN 1000005
#define MAXNLGN 11000005
#define MXD 1048576
#define IOINIT() (io_p1=io_p2=shin_ibuf,io_p3=io_p4=shin_obuf,\
io_p4+=IOBUFSZ)
#define IOGC() ((io_p1==io_p2 &&\
((io_p2=(io_p1=shin_ibuf)+fread(shin_ibuf,1,IOBUFSZ,stdin)),\
io_p1==io_p2))?(EOF):(*io_p1++))
#define IOPC(x) ((io_p3==io_p4)?(fwrite(shin_obuf,1,IOBUFSZ,stdout),\
io_p3=shin_obuf):(0),(*io_p3++)=(x))
#define IOFL() (fwrite(shin_obuf,1,io_p3-shin_obuf,stdout),\
io_p3=shin_obuf)
using namespace std;

typedef long long ll;
typedef int io_t;
char shin_ibuf[IOBUFSZZ];
char shin_obuf[IOBUFSZZ];
char *io_p1,*io_p2,*io_p3,*io_p4;
io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x,char spliter=' ');

struct Ene{
    int lc,rc,sz,ct;
    #define LC(x) ((trie[(x)]).lc)
    #define RC(x) ((trie[(x)]).rc)
    #define SZ(x) ((trie[(x)]).sz)
    #define CNT(x) ((trie[(x)]).ct)
};

int arr[MAXN];
int pcnt=0;
int ans=0;
Ene trie[MAXNLGN];

void insert(int &root,int curd,int curx);
void erase(int &root,int curd,int curx);
void find(int root1,int root2,int curd,int ty,int tans);

int main(void){
    FILENAME("trie");
    IOINIT();
    srand(19260817);
    int n=seto();
    int troot=0;
    for (int i=1;i<=n;i++)
        arr[i]=seto(),insert(troot,MXD,arr[i]);
    int tries=1539;
    while (tries--){
        int i=rand()%(n-2)+1; //[1,n-2]
        int j=rand()%(n-1-i)+i+1; //[i+1,n-1]
        int k=rand()%(n-j)+j+1; //[j+1,n]
        ans=max(ans,(arr[i]|(arr[j]&arr[k])));
    }
    for (int i=1,ub=n-2;i<=ub;i++){
        erase(troot,MXD,arr[i]);
        find(troot,troot,MXD,arr[i],0);
    }
    ayano(ans);
    IOFL();
    return 0;
}

void find(int root1,int root2,int curd,int ty,int tans){
    //printf("find(%d,%d,%d,%d,%d),",root1,root2,curd,ty,tans);
    //printf("SZ(root1)=%d,SZ(root2)=%d\n",SZ(root1),SZ(root2));
    if (root1==root2 && SZ(root1)<2)
        return;
    if (!(SZ(root1) && SZ(root2)))
        return;
    if (!curd){
        ans=max(ans,tans);
        return;
    }
    if ((tans|ty|(CNT(root1)&CNT(root2)))<=ans)
        //CUT!
        return;
    if (ty&curd){
        //This digit is 1.
        if (SZ(LC(root1))){
            if (SZ(LC(root2)))
                find(LC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(LC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
        if (SZ(RC(root1))){
            if (SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
    }
    else {
        //This digit may be 1.
        if ((SZ(RC(root1)) && SZ(RC(root2))) &&
            (root1!=root2 || SZ(RC(root1))>=2))
            find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        else {
            if (SZ(LC(root1))){
                if (SZ(LC(root2)))
                    find(LC(root1),LC(root2),curd>>1,ty,tans);
                if (SZ(RC(root2)))
                    find(LC(root1),RC(root2),curd>>1,ty,tans);
            }
            if (SZ(RC(root1)) && SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans);
        }
    }
}

void insert(int &root,int curd,int curx){
    if (!root){
        pcnt++;
        root=pcnt;
        LC(root)=RC(root)=SZ(root)=CNT(root)=0;
    }
    SZ(root)++;
    if (!curd)
        return;
    CNT(root)|=(curx&((curd<<1)-1));
    if (curx&curd)
        insert(RC(root),curd>>1,curx);
    else
        insert(LC(root),curd>>1,curx);
}

void erase(int &root,int curd,int curx){
    if (!root)
        return;
    SZ(root)--;
    if (!SZ(root)){
        root=0;
        return;
    }
    if (!curd)
        return;
    if (curx&curd)
        erase(RC(root),curd>>1,curx);
    else
        erase(LC(root),curd>>1,curx);
}

io_t seto(void){
    io_t ioans=0;
    int symbol=0;
    char ch=IOGC();
    while (!isdigit(ch)){
        (ch=='-')?(symbol=1):(0);
        ch=IOGC();
    }
    while (isdigit(ch)){
        ioans=ioans*10+(ch-'0');
        ch=IOGC();
    }
    return (symbol)?(-ioans):(ioans);
}
void ayano(io_t x,char spliter){
    if (!x){
        IOPC('0'),IOPC(spliter);
        return;
    }
    if (x<0)
        IOPC('-'),x=-x;
    int len=0;
    while (x){
        io_t d=x/10;
        shin[len++]=x-d*10;
        x=d;
    }
    while (len--)
        IOPC(shin[len]+'0');
    IOPC(spliter);
}


#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker,"/STACK:102400000,102400000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#define FILENAME(x) \
freopen(x".in","r",stdin); \
freopen(x".out","w",stdout);
#define MAXIOLG 25
#define IOBUFSZ 1000000
#define IOBUFSZZ 1000005
#define LOWBIT(x) ((x)&(-(x)))
#define MD(x) (((x)>=MOD)?((x)-=MOD):(0))
#define MAXN 1000005
#define MAXNLGN 11000005
#define MXD 1048576
#define IOINIT() (io_p1=io_p2=shin_ibuf,io_p3=io_p4=shin_obuf,\
io_p4+=IOBUFSZ)
#define IOGC() ((io_p1==io_p2 &&\
((io_p2=(io_p1=shin_ibuf)+fread(shin_ibuf,1,IOBUFSZ,stdin)),\
io_p1==io_p2))?(EOF):(*io_p1++))
#define IOPC(x) ((io_p3==io_p4)?(fwrite(shin_obuf,1,IOBUFSZ,stdout),\
io_p3=shin_obuf):(0),(*io_p3++)=(x))
#define IOFL() (fwrite(shin_obuf,1,io_p3-shin_obuf,stdout),\
io_p3=shin_obuf)
using namespace std;

typedef long long ll;
typedef int io_t;
char shin_ibuf[IOBUFSZZ];
char shin_obuf[IOBUFSZZ];
char *io_p1,*io_p2,*io_p3,*io_p4;
io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x,char spliter=' ');

struct Ene{
    int lc,rc,sz,ct;
    #define LC(x) ((trie[(x)]).lc)
    #define RC(x) ((trie[(x)]).rc)
    #define SZ(x) ((trie[(x)]).sz)
    #define CNT(x) ((trie[(x)]).ct)
};

int arr[MAXN];
int pcnt=0;
int ans=0;
Ene trie[MAXNLGN];

void insert(int &root,int curd,int curx);
void erase(int &root,int curd,int curx);
void find(int root1,int root2,int curd,int ty,int tans);

int main(void){
    IOINIT();
    srand(19260817);
    int n=seto();
    int troot=0;
    for (int i=1;i<=n;i++)
        arr[i]=seto(),insert(troot,MXD,arr[i]);
    int tries=1539;
    while (tries--){
        int i=rand()%(n-2)+1; //[1,n-2]
        int j=rand()%(n-1-i)+i+1; //[i+1,n-1]
        int k=rand()%(n-j)+j+1; //[j+1,n]
        ans=max(ans,(arr[i]|(arr[j]&arr[k])));
    }
    for (int i=1,ub=n-2;i<=ub;i++){
        erase(troot,MXD,arr[i]);
        find(troot,troot,MXD,arr[i],0);
    }
    ayano(ans);
    IOFL();
    return 0;
}

void find(int root1,int root2,int curd,int ty,int tans){
    //printf("find(%d,%d,%d,%d,%d),",root1,root2,curd,ty,tans);
    //printf("SZ(root1)=%d,SZ(root2)=%d\n",SZ(root1),SZ(root2));
    if (root1==root2 && SZ(root1)<2)
        return;
    if (!(SZ(root1) && SZ(root2)))
        return;
    if (!curd){
        ans=max(ans,tans);
        return;
    }
    if ((tans|ty|(CNT(root1)&CNT(root2)))<=ans)
        //CUT!
        return;
    if (ty&curd){
        //This digit is 1.
        if (SZ(LC(root1))){
            if (SZ(LC(root2)))
                find(LC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(LC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
        if (SZ(RC(root1))){
            if (SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
    }
    else {
        //This digit may be 1.
        if ((SZ(RC(root1)) && SZ(RC(root2))) &&
            (root1!=root2 || SZ(RC(root1))>=2))
            find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        else {
            if (SZ(LC(root1))){
                if (SZ(LC(root2)))
                    find(LC(root1),LC(root2),curd>>1,ty,tans);
                if (SZ(RC(root2)))
                    find(LC(root1),RC(root2),curd>>1,ty,tans);
            }
            if (SZ(RC(root1)) && SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans);
        }
    }
}

void insert(int &root,int curd,int curx){
    if (!root){
        pcnt++;
        root=pcnt;
        LC(root)=RC(root)=SZ(root)=CNT(root)=0;
    }
    SZ(root)++;
    if (!curd)
        return;
    CNT(root)|=(curx&((curd<<1)-1));
    if (curx&curd)
        insert(RC(root),curd>>1,curx);
    else
        insert(LC(root),curd>>1,curx);
}

void erase(int &root,int curd,int curx){
    if (!root)
        return;
    SZ(root)--;
    if (!SZ(root)){
        root=0;
        return;
    }
    if (!curd)
        return;
    if (curx&curd)
        erase(RC(root),curd>>1,curx);
    else
        erase(LC(root),curd>>1,curx);
}

io_t seto(void){
    io_t ioans=0;
    int symbol=0;
    char ch=IOGC();
    while (!isdigit(ch)){
        (ch=='-')?(symbol=1):(0);
        ch=IOGC();
    }
    while (isdigit(ch)){
        ioans=ioans*10+(ch-'0');
        ch=IOGC();
    }
    return (symbol)?(-ioans):(ioans);
}
void ayano(io_t x,char spliter){
    if (!x){
        IOPC('0'),IOPC(spliter);
        return;
    }
    if (x<0)
        IOPC('-'),x=-x;
    int len=0;
    while (x){
        io_t d=x/10;
        shin[len++]=x-d*10;
        x=d;
    }
    while (len--)
        IOPC(shin[len]+'0');
    IOPC(spliter);
}


#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#define FILENAME(x) \
freopen(x".in","r",stdin); \
freopen(x".out","w",stdout);
#define MAXIOLG 25
#define LOWBIT(x) ((x)&(-(x)))
#define MD(x) (((x)>=MOD)?((x)-=MOD):(0))
#define MAXN 1000005
#define MAXNLGN 21000005
#define MXD 1048576
//#define MXD 8
using namespace std;

typedef long long ll;
typedef long double ld;
typedef int io_t;

io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x,char spliter='\n');

struct Ene{
    int lc,rc,sz,ct;
    #define LC(x) ((trie[(x)]).lc)
    #define RC(x) ((trie[(x)]).rc)
    #define SZ(x) ((trie[(x)]).sz)
    #define CNT(x) ((trie[(x)]).ct)
};

int arr[MAXN];
int pcnt=0;
int ans=0;
Ene trie[MAXNLGN];

void insert(int &root,int curd,int curx);
void erase(int &root,int curd,int curx);
void find(int root1,int root2,int curd,int ty,int tans);

int main(void){
    srand(19260817);
    int n=seto();
    int troot=0;
    for (int i=1;i<=n;i++)
        arr[i]=seto(),insert(troot,MXD,arr[i]);
    int tries=539;
    while (tries--){
        int i=rand()%(n-2)+1; //[1,n-2]
        int j=rand()%(n-1-i)+i+1; //[i+1,n-1]
        int k=rand()%(n-j)+j+1; //[j+1,n]
        ans=max(ans,(arr[i]|(arr[j]&arr[k])));
    }
    for (int i=1,ub=n-2;i<=ub;i++){
        erase(troot,MXD,arr[i]);
        find(troot,troot,MXD,arr[i],0);
    }
    ayano(ans);
    return 0;
}

void find(int root1,int root2,int curd,int ty,int tans){
    //printf("find(%d,%d,%d,%d,%d),",root1,root2,curd,ty,tans);
    //printf("SZ(root1)=%d,SZ(root2)=%d\n",SZ(root1),SZ(root2));
    if (root1==root2 && SZ(root1)<2)
        return;
    if (!(SZ(root1) && SZ(root2)))
        return;
    if (!curd){
        ans=max(ans,tans);
        return;
    }
    if ((tans|((curd<<1)-1))<=ans)
        //return printf("CUT!\n"),void();
        return;
    if (ty&curd){
        //This digit is 1.
        if (SZ(LC(root1))){
            if (SZ(LC(root2)))
                find(LC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(LC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
        if (SZ(RC(root1))){
            if (SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
    }
    else {
        //This digit may be 1.
        if ((SZ(RC(root1)) && SZ(RC(root2))) &&
            (root1!=root2 || SZ(RC(root1))>=2))
            find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        else {
            if (SZ(LC(root1))){
                if (SZ(LC(root2)))
                    find(LC(root1),LC(root2),curd>>1,ty,tans);
                if (SZ(RC(root2)))
                    find(LC(root1),RC(root2),curd>>1,ty,tans);
            }
            if (SZ(RC(root1)) && SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans);
        }
    }
}

void insert(int &root,int curd,int curx){
    if (!root){
        pcnt++;
        root=pcnt;
        LC(root)=RC(root)=SZ(root)=CNT(root)=0;
    }
    SZ(root)++;
    if (!curd){
        CNT(root)++;
        return;
    }
    if (curx&curd)
        insert(RC(root),curd>>1,curx);
    else
        insert(LC(root),curd>>1,curx);
}

void erase(int &root,int curd,int curx){
    if (!root)
        return;
    SZ(root)--;
    if (!SZ(root)){
        root=0;
        return;
    }
    if (!curd){
        CNT(root)--;
        return;
    }
    if (curx&curd)
        erase(RC(root),curd>>1,curx);
    else
        erase(LC(root),curd>>1,curx);
}

io_t seto(void){
    io_t x=0;
    int symbol=0;
    char ch=getchar();
    while (!isdigit(ch))
        (ch=='-')?(symbol=1):(0),ch=getchar();
    while (isdigit(ch))
        x=(x*10)+(ch-'0'),ch=getchar();
    return (symbol)?(-x):(x);
}
void ayano(io_t x,char spliter){
    if (!x){
        putchar('0'),putchar(spliter);
        return;
    }
    if (x<0)
        putchar('-'),x=-x;
    int len=0;
    while (x){
        io_t d=x/10;
        shin[len++]=x-d*10;
        x=d;
    }
    while (len--)
        putchar(shin[len]+'0');
    putchar(spliter);
}


#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker,"/STACK:102400000,102400000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#define FILENAME(x) \
freopen(x".in","r",stdin); \
freopen(x".out","w",stdout);
#define MAXIOLG 25
#define IOBUFSZ 1000000
#define IOBUFSZZ 1000005
#define LOWBIT(x) ((x)&(-(x)))
#define MD(x) (((x)>=MOD)?((x)-=MOD):(0))
#define MAXN 1000005
#define MAXNLGN 11000005
#define MXD 1048576
#define IOINIT() (io_p1=io_p2=shin_ibuf,io_p3=io_p4=shin_obuf,\
io_p4+=IOBUFSZ)
#define IOGC() ((io_p1==io_p2 &&\
((io_p2=(io_p1=shin_ibuf)+fread(shin_ibuf,1,IOBUFSZ,stdin)),\
io_p1==io_p2))?(EOF):(*io_p1++))
#define IOPC(x) ((io_p3==io_p4)?(fwrite(shin_obuf,1,IOBUFSZ,stdout),\
io_p3=shin_obuf):(0),(*io_p3++)=(x))
#define IOFL() (fwrite(shin_obuf,1,io_p3-shin_obuf,stdout),\
io_p3=shin_obuf)
using namespace std;

typedef long long ll;
typedef int io_t;
char shin_ibuf[IOBUFSZZ];
char shin_obuf[IOBUFSZZ];
char *io_p1,*io_p2,*io_p3,*io_p4;
io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x,char spliter=' ');

struct Ene{
    int lc,rc,sz,ct;
    #define LC(x) ((trie[(x)]).lc)
    #define RC(x) ((trie[(x)]).rc)
    #define SZ(x) ((trie[(x)]).sz)
    #define CNT(x) ((trie[(x)]).ct)
};

int arr[MAXN];
int pcnt=0;
int ans=0;
Ene trie[MAXNLGN];

void insert(int &root,int curd,int curx);
void erase(int &root,int curd,int curx);
void find(int root1,int root2,int curd,int ty,int tans);

int main(void){
    IOINIT();
    srand(19260817);
    int n=seto();
    int troot=0;
    for (int i=1;i<=n;i++)
        arr[i]=seto(),insert(troot,MXD,arr[i]);
    int tries=1539;
    while (tries--){
        int i=rand()%(n-2)+1; //[1,n-2]
        int j=rand()%(n-1-i)+i+1; //[i+1,n-1]
        int k=rand()%(n-j)+j+1; //[j+1,n]
        ans=max(ans,(arr[i]|(arr[j]&arr[k])));
    }
    for (int i=1,ub=n-2;i<=ub;i++){
        erase(troot,MXD,arr[i]);
        find(troot,troot,MXD,arr[i],0);
    }
    ayano(ans);
    IOFL();
    return 0;
}

void find(int root1,int root2,int curd,int ty,int tans){
    //printf("find(%d,%d,%d,%d,%d),",root1,root2,curd,ty,tans);
    //printf("SZ(root1)=%d,SZ(root2)=%d\n",SZ(root1),SZ(root2));
    if (root1==root2 && SZ(root1)<2)
        return;
    if (!(SZ(root1) && SZ(root2)))
        return;
    if (!curd){
        ans=max(ans,tans);
        return;
    }
    if ((tans|((curd<<1)-1))<=ans)
        //return printf("CUT!\n"),void();
        return;
    if (ty&curd){
        //This digit is 1.
        if (SZ(LC(root1))){
            if (SZ(LC(root2)))
                find(LC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(LC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
        if (SZ(RC(root1))){
            if (SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
    }
    else {
        //This digit may be 1.
        if ((SZ(RC(root1)) && SZ(RC(root2))) &&
            (root1!=root2 || SZ(RC(root1))>=2))
            find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        else {
            if (SZ(LC(root1))){
                if (SZ(LC(root2)))
                    find(LC(root1),LC(root2),curd>>1,ty,tans);
                if (SZ(RC(root2)))
                    find(LC(root1),RC(root2),curd>>1,ty,tans);
            }
            if (SZ(RC(root1)) && SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans);
        }
    }
}

void insert(int &root,int curd,int curx){
    if (!root){
        pcnt++;
        root=pcnt;
        LC(root)=RC(root)=SZ(root)=CNT(root)=0;
    }
    SZ(root)++;
    if (!curd){
        CNT(root)++;
        return;
    }
    if (curx&curd)
        insert(RC(root),curd>>1,curx);
    else
        insert(LC(root),curd>>1,curx);
}

void erase(int &root,int curd,int curx){
    if (!root)
        return;
    SZ(root)--;
    if (!SZ(root)){
        root=0;
        return;
    }
    if (!curd){
        CNT(root)--;
        return;
    }
    if (curx&curd)
        erase(RC(root),curd>>1,curx);
    else
        erase(LC(root),curd>>1,curx);
}

io_t seto(void){
    io_t ioans=0;
    int symbol=0;
    char ch=IOGC();
    while (!isdigit(ch)){
        (ch=='-')?(symbol=1):(0);
        ch=IOGC();
    }
    while (isdigit(ch)){
        ioans=ioans*10+(ch-'0');
        ch=IOGC();
    }
    return (symbol)?(-ioans):(ioans);
}
void ayano(io_t x,char spliter){
    if (!x){
        IOPC('0'),IOPC(spliter);
        return;
    }
    if (x<0)
        IOPC('-'),x=-x;
    int len=0;
    while (x){
        io_t d=x/10;
        shin[len++]=x-d*10;
        x=d;
    }
    while (len--)
        IOPC(shin[len]+'0');
    IOPC(spliter);
}


#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#define FILENAME(x) \
freopen(x".in","r",stdin); \
freopen(x".out","w",stdout);
#define MAXIOLG 25
#define LOWBIT(x) ((x)&(-(x)))
#define MD(x) (((x)>=MOD)?((x)-=MOD):(0))
#define MAXN 1000005
#define MAXNLGN 11000005
#define MXD 1048576
//#define MXD 8
using namespace std;

typedef long long ll;
typedef long double ld;
typedef int io_t;

io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x,char spliter='\n');

struct Ene{
    int lc,rc,sz,ct;
    #define LC(x) ((trie[(x)]).lc)
    #define RC(x) ((trie[(x)]).rc)
    #define SZ(x) ((trie[(x)]).sz)
    #define CNT(x) ((trie[(x)]).ct)
};

int arr[MAXN];
int pcnt=0;
int ans=0;
Ene trie[MAXNLGN];

void insert(int &root,int curd,int curx);
void erase(int &root,int curd,int curx);
void find(int root1,int root2,int curd,int ty,int tans);

int main(void){
    srand(19260817);
    int n=seto();
    int troot=0;
    for (int i=1;i<=n;i++)
        arr[i]=seto(),insert(troot,MXD,arr[i]);
    int tries=539;
    while (tries--){
        int i=rand()%(n-2)+1; //[1,n-2]
        int j=rand()%(n-1-i)+i+1; //[i+1,n-1]
        int k=rand()%(n-j)+j+1; //[j+1,n]
        ans=max(ans,(arr[i]|(arr[j]&arr[k])));
    }
    for (int i=1,ub=n-2;i<=ub;i++){
        erase(troot,MXD,arr[i]);
        find(troot,troot,MXD,arr[i],0);
    }
    ayano(ans);
    return 0;
}

void find(int root1,int root2,int curd,int ty,int tans){
    //printf("find(%d,%d,%d,%d,%d),",root1,root2,curd,ty,tans);
    //printf("SZ(root1)=%d,SZ(root2)=%d\n",SZ(root1),SZ(root2));
    if (root1==root2 && SZ(root1)<2)
        return;
    if (!(SZ(root1) && SZ(root2)))
        return;
    if (!curd){
        ans=max(ans,tans);
        return;
    }
    if ((tans|((curd<<1)-1))<=ans)
        //return printf("CUT!\n"),void();
        return;
    if (ty&curd){
        //This digit is 1.
        if (SZ(LC(root1))){
            if (SZ(LC(root2)))
                find(LC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(LC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
        if (SZ(RC(root1))){
            if (SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans^curd);
            if (SZ(RC(root2)))
                find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        }
    }
    else {
        //This digit may be 1.
        if ((SZ(RC(root1)) && SZ(RC(root2))) &&
            (root1!=root2 || SZ(RC(root1))>=2))
            find(RC(root1),RC(root2),curd>>1,ty,tans^curd);
        else {
            if (SZ(LC(root1))){
                if (SZ(LC(root2)))
                    find(LC(root1),LC(root2),curd>>1,ty,tans);
                if (SZ(RC(root2)))
                    find(LC(root1),RC(root2),curd>>1,ty,tans);
            }
            if (SZ(RC(root1)) && SZ(LC(root2)))
                find(RC(root1),LC(root2),curd>>1,ty,tans);
        }
    }
}

void insert(int &root,int curd,int curx){
    if (!root){
        pcnt++;
        root=pcnt;
        LC(root)=RC(root)=SZ(root)=CNT(root)=0;
    }
    SZ(root)++;
    if (!curd){
        CNT(root)++;
        return;
    }
    if (curx&curd)
        insert(RC(root),curd>>1,curx);
    else
        insert(LC(root),curd>>1,curx);
}

void erase(int &root,int curd,int curx){
    if (!root)
        return;
    SZ(root)--;
    if (!SZ(root)){
        root=0;
        return;
    }
    if (!curd){
        CNT(root)--;
        return;
    }
    if (curx&curd)
        erase(RC(root),curd>>1,curx);
    else
        erase(LC(root),curd>>1,curx);
}

io_t seto(void){
    io_t x=0;
    int symbol=0;
    char ch=getchar();
    while (!isdigit(ch))
        (ch=='-')?(symbol=1):(0),ch=getchar();
    while (isdigit(ch))
        x=(x*10)+(ch-'0'),ch=getchar();
    return (symbol)?(-x):(x);
}
void ayano(io_t x,char spliter){
    if (!x){
        putchar('0'),putchar(spliter);
        return;
    }
    if (x<0)
        putchar('-'),x=-x;
    int len=0;
    while (x){
        io_t d=x/10;
        shin[len++]=x-d*10;
        x=d;
    }
    while (len--)
        putchar(shin[len]+'0');
    putchar(spliter);
}