#include <iostream>
#include <iomanip>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e15;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
typedef pair<ll,ll> P;
typedef pair<double,double> point;
const ll MAX = 2000005;
constexpr ll nx[8] = {1,0,-1,0,1,-1,1,-1};
constexpr ll ny[8] = {0,1,0,-1,1,1,-1,-1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h,w;
    cin >> h >> w;
    vector<string> grid(h+2);
    string s = "";
    rep(i,w+2) s += '.';
    grid[0] = s;
    grid[h+1] = s;
    rep(i,h){
        cin >> grid[i+1];
        grid[i+1] = '.'+grid[i+1]+'.';
    }
    rep(i,1,h+1){
        rep(j,1,w+1){
            bool out = 1;
            if (grid[i][j] == '#'){
                rep(k,4){
                    ll xn = i+nx[k];
                    ll yn = j+ny[k];
                    if (grid[xn][yn] == '#') out = 0;
                }
                if (out){
                    PRINT("No");
                    return 0;
                }
            }
        }
    }
    PRINT("Yes");
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main(){
  ll n,k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i,n){
    cin >> a.at(i);
  }
  ll i,sum=0,mini=1e9+7;
  for(i=0;i<=n-k;i++){
    sum=a.at(i+k-1)-a.at(i);
    if(a.at(i+k-1)>0 && a.at(i)<0){
      ll p=min(a.at(i+k-1),abs(a.at(i)));
      sum+=p;
    }else{
      sum=max(abs(a.at(i+k-1)),abs(a.at(i)));
    }
    mini=min(mini,sum);
  }
  cout << mini;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
    int a , b;
    cin >> a >> b;
    int c = 1 , ans = 0;
    while(c<b){
        c--;
        c += a;
        ans++;
    }
    cout<<ans;
}

#include<bits/stdc++.h>
#define ll long long int
#define vl vector<ll>
#define pl pair<ll,ll>
#define pb push_back
#define F first
#define S second
using namespace std;
ll tako(ll a,ll b)
{
	if(a>=b)
      return 1;
  return 1+tako(a,b-a+1);
}
int main()
{
	ll a,b;
	cin >> a >> b;
	if(b==1)
	cout << "0";
	else
	cout << tako(a,b);

	return 0;
}


//Jai Bhole Ki
#include <bits/stdc++.h>
using namespace std;
#define fast static int fastline = []() { std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }();
#define ll long long
#define LL unsigned ll
#define pb push_back
#define pi pair<ll,ll>
#define debug(x) cout<<x<<"\n";
#define debugg cout<<"debugged\n";
#define minval -2e9
#define maxval 2e9
long double PI =3.14159265358979323846;

const ll M=1e9+7;
const ll inf=1e18;
const ll ms=2e5+5;
fast;
ll n,m,x,y,ans;
void solve()
{
    cin>>n>>m>>x>>y;vector<ll> ar(n),arr(m);
    for(int i=0;i<n;i++)
        cin>>ar[i];
    sort(ar.begin(),ar.end());
    for(int i=0;i<m;i++)
        cin>>arr[i];
    sort(arr.begin(),arr.end());
    bool flag=0;
    if(x>ar[n-1]||y<arr[0])
        {cout<<"War"<<endl;return;}
    for(int i=x;i<=y;i++)
    {
        if(i>ar[n-1]&&i<=arr[0])
            {flag=1;break;}
    }
    if(flag)
        cout<<"No War"<<endl;
    else
        cout<<"War"<<endl;
}
void solve2()
{
    cin>>n;ans=0;map<ll,ll>mp;
    for(int i=0;i<n;i++)
    {
        ll a;cin>>a;mp[a]++;
    }
    for(auto it:mp)
    {
       ans=max(ans,it.second);

    }
    cout<<ans<<endl;
}
int main()
{
 // freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    //int t;cin>>t;while(t--)
    {
        solve();
    }
    return 0;
}

// J'aime
// Chemise Blanche

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

#define int long long

#define ii pair<int,int>
#define fi first
#define sc second

#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr << __LINE__ << " > " << #x << " = " << (x) << endl

void MAIN() {
  int n, m, x, y;
  cin >> n >> m >> x >> y;
  vector<int> a(n), b(m);
  for (auto &i : a) cin >> i;
  for (auto &i : b) cin >> i;
  sort(all(a));
  sort(all(b));
  if (a.back() < b.front() && a.back() < y && b.front() > x) cout << "No War" << '\n';
  else cout << "War" << '\n';
}

signed main() {
#ifdef _DEBUG
  // freopen("in" , "r", stdin );
  // freopen("out", "w", stdout);
#endif
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) MAIN();
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<int, ii>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fi first
#define se second
// #define multicase
const int MOD = 1e9 + 7;
const int INF = 2e9;
// const int N = ;

void solve() {
    int T = 1;
  #ifdef multicase
    cin >> T;
  #endif
    for(int tc = 1, n; tc <= T; tc++) {
        cin >> n;
        cout << n + n * n + n * n * n << "\n";
    }
}

int main() {

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

    solve();

}

# include<iostream>
# include<math.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a;
    b=pow(a,1)+pow(a,2)+pow(a,3);
    cout<<b<<"\n";

}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< int, int >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)

using namespace std;
string repeat(string s, int n) {
    string s1 = "";
    for (int i=0; i<n;i++)
        s1+=s;
    return s1;
}
string getString(char x) {
    string s(1, x);
    return s;
}

void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int gcd(int a, int b){
    if (a == 0)  return b;
    return gcd(b % a, a);
}
void  solve(){
  int n,m;
  cin>>n>>m;
  set<int> s1;
  rep(i,2,sqrt(m)+1){
    if(m%i==0) s1.insert(i);
    if(m%i==0 && m%(m/i)==0) s1.insert(m/i);
  }
  s1.insert(m);
  s1.insert(1);
  for(auto i:s1){
    if(i>=n){
      cout<<m/i<<endl;
      return;
    }
  }
}
int main(){
    optimizeIO();
    // tc
    { solve();
    }
}


#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";};
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

int digit[50];
void solve(){
    int n;
    cin >> n;
    if(n==0){
        print(0);
        return;
    }
    bool plus=true;
    if(n<0) {
        plus=false;
        n=-n;
    }
    int ctr=0;
    while(n>0){
        if(n&1){
            digit[ctr]=1;
        }
        ctr++;
        n/=2;
    }
    if(plus){
        for(ll i=ctr-1; i>0; i--){
            if(i&1){
                if(digit[i]==1) digit[i+1]++;
            }
        }
        repi(i,1,45){
            while(digit[i]>1){
                digit[i+1]++;
                digit[i+2]++;
                digit[i]-=2;
            }
        }
    }
    else{
        for(ll i=ctr-1; i>=0; i--){
            if(i%2==0){
                if(digit[i]==1) digit[i+1]++;
            }
        }
        repi(i,1,45){
            while(digit[i]>1){
                digit[i+1]++;
                digit[i+2]++;
                digit[i]-=2;
            }
        }
    }
    bool start=false;
    for(ll i=38;i>=0;i--){
        if(!start&&digit[i]==0) continue;
        if(!start&&digit[i]==1){
            start=true;
            cout << digit[i];
            continue;
        }
        cout << digit[i];
    }
    cout << endl;
    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[505][505];
bool chk[23333333];
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
int nxtPrime(int&x){
    auto chk=[&](int x){
        if(x<2)return false;
        for(int i=2;i*i<=x;i++)
            if(x%i==0)
                return false;
        return true;
    };
    do x++;while(!chk(x));
    ::chk[x]=true;
    return x;
}
void CCC(int n){
    map<int,pair<int,int>>s;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            if(s.count(a[i][j])){
                cout<<s[a[i][j]].first<<" "<<s[a[i][j]].second<<" "<<i<<" "<<j<<"\n";
                break;
            }
            s[a[i][j]]=make_pair(i,j);
        }
}
void solve(int n){
    int p=0;
    for(int i=1,j=n-(~n&1);i<=n;i++,j--)
        if(!a[i][j]&&1<=j&&j<=n)
            a[i][j]=nxtPrime(p);
    for(int i=n,j=n;i>=1;i--,j--)
        if(!a[i][j])
            a[i][j]=nxtPrime(p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if((i+j)%2==0){
                if(a[i][j])continue;
                auto get=[&](int dx,int dy)->ll{
                    int x=i,y=j;
                    while(1<=x&&x<=n&&1<=y&&y<=n){
                        if(chk[a[x][y]])return a[x][y];
                        x+=dx,y+=dy;
                    }
                    return 1;
                };
                a[i][j]=get(+1,+1)*get(+1,-1)*get(-1,+1)*get(-1,-1);
            }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if((i+j)%2==1){
                auto upd=[&](int dx,int dy){
                    if(a[i+dx][j+dy])
                        a[i][j]=lcm(a[i][j],a[i+dx][j+dy]);
                };
                a[i][j]=1;
                upd(+1,0);
                upd(-1,0);
                upd(0,+1);
                upd(0,-1);
                a[i][j]++;
            }
}
int main(){
    solve(500);
    int n;cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cout<<a[i][j]<<" \n"[j==n];
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <cstring>
#include <climits>

#define MAX
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

using namespace std;

bool primearray[10000];

void erathos()
{
	int i;
	memset(primearray, 0, sizeof(primearray));

	REP( i, 2, 10000)
	{
		if( primearray[i] == 0 )
		{
			for( int j = i*2; j < 10000; j+=i)
			{
				primearray[j] = 1;
			}
		}
	}

	return;
}

int main()
{
	int in;

	erathos();

	while( cin >> in, in)
	{
		for( int i = in ; i > 0; i--)
		{
			if( primearray[i] == 0 && primearray[i-2] == 0 )
			{
				cout << i-2 << " " << i << endl;
				break;
			}
		}
	}

	return 0;

}


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

int a[111];
int n;

void bs(){
  bool flag = true;
  int cnt = 0;

  while(flag){
    flag = false;
    for(int j = n-1; j > 0; j--){
      if(a[j] < a[j-1]){
        cnt++;
        swap(a[j],a[j-1]);
        flag = true;
      }
    }
  }
  for(int i = 0; i < n; i++){
    if(i != n-1){
      cout << a[i] << " ";
    }else cout << a[i];
  }
  cout << endl;
  cout << cnt << endl;

}

int main(){

  cin >> n;

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

  bs();

}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 1000005
queue<int> q;
char a[N],b[N];
int main()
{
	int n,i,j,ans=0;
	scanf("%d%s%s",&n,a+1,b+1);
	if(strcmp(a+1,b+1)==0){printf("0");return 0;}
	i=j=n;
	while(j){
		while(j&&b[j]==b[j-1])j--;
		while(i&&(i>j||a[i]!=b[j]))i--;
		if(i==0){printf("-1");return 0;}
		while(!q.empty()&&q.front()-(int)q.size()>=j)q.pop();
		if(i!=j)q.push(i);
		ans=max(ans,(int)q.size()+1);
		j--;
	}
	printf("%d",ans);
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
using namespace std;
int main(){
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int a,b;
  cin>>a>>b;
  if(a<9 && b<9){
    cout<<"Yay!"<<"\n";
  }
  else{
    cout<<":("<<"\n";
  }
  return 0;
}

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

int main(void)
{
	int N, K, i, min;
	cin >> N >> K;
	min = 1;
	for (i = 0; i < N; ++i)
    {
      if (min * 2 < min + K)
      {
        min *= 2;
      }
      else
      {
        min += K;
      }
    }
	cout << min << endl;
	return (0);
}


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

typedef long long ll;
typedef long double ld;
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353
#define Graph vector<vector<int>>

int main() {
    int A, B; cin >> A >> B;
    string ans = "Yay!";
    if (A > 8 || B > 8) {
        ans = ":(";
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{  int a,b;
   cin>>a>>b;
   int r=abs(a-b);
  int k=0;
 if(a<b)
     k=2*a+2*r;
 else
     k=2*b+2*r;
if(k<=16)
  cout<<"Yay!";
else
  cout<<":(";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(n) begin(n),end(n)
using ll = long long;
using P = pair<int,int>;

int main() {
    int k, t;
    cin >> k >> t;
    vector<int> a(t);
    rep(i,t) cin >> a[i];
    sort(all(a));
    int mx = a[t-1];
    int e = k-mx;
    if(mx-1 <= e) cout << 0 << endl;
    else cout << mx-1-e << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <numeric>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rep1(i,n) for (int i = 1; i <= n; ++i)
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline T intceil(T a, T b) { return (a + (b - 1)) / b; }
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
const long long MOD = 1e9+7;
#define precout(val) cout << std::fixed << std::setprecision(20) << val;
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const int dy[4] = { 0, 1, 0, -1 };
const int dx[4] = { 1, 0, -1, 0 };

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

  priority_queue<pair<int, int>> pq;
  int k,t; cin >> k >> t;
  rep(i, t) {
    int a; cin >> a;
    pq.push(make_pair(a, i));
  }

  int ans = 0;
  int beforeT = -1;
  while(!pq.empty()) {
    auto top = pq.top(); pq.pop();

    if(beforeT == top.second) {
      if(pq.size() == 0) {
        ++ans;
        --top.first;
        if(top.first == 0) {
          break;
        } else {
          pq.push(top);
          continue;
        }
      }

      auto prev = pq.top(); pq.pop();
      --prev.first;
      beforeT = prev.second;
      if(prev.first > 0) {
        pq.push(prev);
      }
      pq.push(top);
      continue;
    }

    beforeT = top.second;
    --top.first;
    if(top.first != 0) {
      pq.push(top);
    }
  }

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


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
using namespace std;

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

	int ans=0;
	for(int i=3;i<N;i++){
		ans=max(ans, N/i);
	}
	cout << ans << endl;
}

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <cstring>
#include<cmath>
using namespace std;
int main() {
	int x;
	cin >> x;
	cout << x / 3;

	return 0;

}


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

int main() {
  int a;
  cin >> a;
  if(a == 2){cout << -1 << endl;}
  else if(a == 3){cout << "aa." <<endl;
                 cout << "..a" <<endl;
                 cout << "..a" <<endl;}
  else{
    int c;
    c = (a/4-1)*4;
    for(int i = 0;i < c/2;i++){
      for(int j = 0;j < c;j++){
      if(j/2==i){cout << 'a';}
      else if(j==c-2*i-2){cout << 'c';}
      else if(j==c-2*i-1){cout << 'd';}
        else{cout << '.';}
      }
      for(int j = 0;j < a-c;j++){
        cout << '.';}
      cout << endl;
      for(int j = 0;j < c;j++){
      if(j/2==i){cout << 'b';}
      else if(j==c-2*i-2){cout << 'c';}
      else if(j==c-2*i-1){cout << 'd';}
        else{cout << '.';}
      }
      for(int j = 0;j < a-c;j++){
        cout << '.';}
       cout << endl;
    }
    int p = a-c;
    if(p == 4){
      for(int i = 0;i < c;i++){cout << '.';}
      cout << "aabc" << endl;
      for(int i = 0;i < c;i++){cout << '.';}
      cout << "ddbc" << endl;
      for(int i = 0;i < c;i++){cout << '.';}
      cout << "bcaa" << endl;
       for(int i = 0;i < c;i++){cout << '.';}
      cout << "bcdd" << endl;}
    else if(p == 5){
      for(int i = 0;i < c;i++){cout << '.';}cout << "aabbc" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "de..c" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "de..f" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "h.ggf" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "hiijj" << endl;}
    else if(p==6){
      for(int i = 0;i < c;i++){cout << '.';}cout << "aabxx." << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "c.b..z" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "cdd..z" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "xx.aab" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "..zc.b" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "..zcdd" << endl;}
    else{
      for(int i = 0;i < c;i++){cout << '.';}cout << "aa.bb.c" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << ".d..eec" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "fdg...." << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "f.g...h" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "...iijh" << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "zxx..j." << endl;
      for(int i = 0;i < c;i++){cout << '.';}cout << "z.mm.nn" << endl;}
  }
}

#include<stdio.h>
#include<algorithm>
#include<vector>
#define fst first
#define snd second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

char three[3][4] = {
    "xx.",
    "..y",
    "..y"
};

char four[4][5] = {
    "aacd",
    "bbcd",
    "cdaa",
    "cdbb"
};

char five[5][6] = {
    "aabbc",
    "dx..c",
    "dx..b",
    "a.yyb",
    "accdd"
};

char six[6][7] = {
    "aacd..",
    "bbcd..",
    "..aacd",
    "..bbcd",
    "cd..aa",
    "cd..bb"
};

char seven[7][8] = {
    "aabbcc.",
    "xx.xx.a",
    "..y..ya",
    "..y..yb",
    "xx.xx.b",
    "..y..yc",
    "..y..yc"
};

char ans[1000][1000 + 1];

int main() {
    int n;
    scanf("%d", &n);
    if (n == 2) return !puts("-1");
    if (n == 3) {
        for(int i = 0; i < 3; i++)
            puts(three[i]);
    }
    else {
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans[i][j] = '.';

        for(int st = 0; st < n;) {
            if (n - st == 5) {
                for (int i = 0; i < 5; i++) for (int j =0; j < 5; j++) {
                    ans[st + i][st + j] = five[i][j];
                }
                st += 5;
            }
            else if (n - st == 6) {
                for (int i = 0; i < 6; i++) for (int j = 0; j < 6; j++) {
                    ans[st + i][st + j] = six[i][j];
                }
                st += 6;
            }
            else if (n - st == 7) {
                for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) {
                    ans[st + i][st + j] = seven[i][j];
                }
                st += 7;
            }
            else {
                for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) {
                    ans[st + i][st + j] = four[i][j];
                }
                st += 4;
            }
        }

        for (int i = 0; i < n; i++)
            puts(ans[i]);
    }
    return 0;
}


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

int main(){
    ll a,b, k; cin >> a >> b >> k;
    for(int i = 0; i < k; i++){
        if(i %2 == 0){
            a/=2; b += a;
        }else{
            b/=2; a += b;
        }
    }
    cout << a << " " << b << endl;
    return 0;
}

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

int main() {
  int n,h,w;
  cin >>n>>h>>w;
  long long kotae=(n-h+1)*(n-w+1);
  cout<<kotae<<endl;
  return 0;
}


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

int main()
{
  int sum = 0;
  int a, b, m = 101;

  for (int i = 0; i < 4; i++){
    cin >> a;
    m = min(a, m);
    sum += a;
  }

  sum -= m;

  cin >> a >> b;

  sum += max(a, b);

  cout << sum << endl;
}

#include <iostream>
using namespace std;

int main(){
    int a[6];
    int sum=0;
    for(int i=0;i<6;i++){
        cin>>a[i];
        sum+=a[i];
    }

    int min=100;
    for(int j=0;j<4;j++){
        if(min>a[j])min=a[j];
    }

    sum=sum-min;

    if(a[4]>a[5])sum=sum-a[5];
    else sum=sum-a[4];

    cout<<sum<<endl;
}


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

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int maxn = 319;
const int MOD = 998244353;
ll dp[maxn][maxn][maxn];
string s;
int k, ones, zeros;
vi numo, mino;

ll solve(int a, int b, int c){
	if(dp[a][b][c] != -1){
		return dp[a][b][c];
	}
	if(a == 0){
		return (b - c == numo[a]);
	}
	ll ans = 0;
	for(int i = 0; i <= b; i++){
		int nh = b - i; // i is in this column, nh in previous
		if(i - numo[a] > c) break;
		if(nh < mino[a-1]) continue;
		int nm = max(i - numo[a], 0);
		ans = (ans + solve(a-1,nh,c-nm))%MOD;
	}
	dp[a][b][c] = ans;
	//cout << a << " " << b << " " << c << " "  << dp[a][b][c] << endl;
	return ans;
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	cin >> s;
	cin >> k;
	int n = sz(s);
	for(char c: s) if(c == '1') ones++;
	zeros = n - ones;
	rep(i,maxn) rep(j,maxn) rep(ii,maxn) dp[i][j][ii] = -1;
	numo.pb(0);
	for(char c: s){
		if(c == '1') numo.back()++;
		else numo.pb(0);
	}
	mino.resize(sz(numo));
	mino[0] = numo[0];
	for(int i = 1; i < sz(numo); i++){
		mino[i] = mino[i-1] + numo[i];
	}
	ll ans = 0;
	k = min(k, ones);
	rep(i,k+1){
		ans = (ans + solve(zeros,ones,i))%MOD;
	}
	cout << ans << endl;
	return 0;
}

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

const int MAX_N = 2e3 + 6, MOD = 1e9 + 7;
int n, k;
int F[MAX_N * MAX_N];
int D[MAX_N * MAX_N];
int dp[MAX_N][MAX_N];

int _sum(int a, int b) {
	int c = a + b;
	if (MOD <= c)
		c -= MOD;

	return c;
}

int _mul(int a, int b) {
	int res = 1LL * a * b % MOD;
	return res;
}

int _pow(int a, int b) {
	if (!b)
		return 1;

	int res = _pow(a, b >> 1);
	res = _mul(res, res);

	if (b & 1)
		res = _mul(res, a);

	return res;
}

int choose(int a, int b) {
	if (b < a || min(a, b) < 0)
		return 0;

	return _mul(F[b], _mul(D[a], D[b - a]));
}

void pre_process() {
	F[0] = 1;
	for (int i = 1; i < MAX_N * MAX_N; i++)
		F[i] = _mul(F[i - 1], i);

	D[0] = 1;
	for (int i = 1; i < MAX_N * MAX_N; i++)
		D[i] = _pow(F[i], MOD - 2);
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	pre_process();
	cin >> n >> k;

	if (k == 1) {
		cout << "1\n";
		return 0;
	}

	dp[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		dp[i][0] = 1;
		for (int j = 1; j <= i; j++)
			dp[i][j] = _sum(_mul(dp[i][j - 1], choose(k - 2, n * k - i - (j - 1) * (k - 1) - 1)), dp[i - 1][j]);
	}

	cout << _mul(dp[n][n], F[n]) << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct Union_Find_Tree{
    vector<int> par, rank, num;

    Union_Find_Tree(int N){
        par.assign(N, -1);
        rank.assign(N, 0);
        num.assign(N, 1);
    }

    int root(int x){
        if(par[x] < 0 || par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y){
        x = root(x), y = root(y);
        if(x == y) return;
        elif(rank[x] < rank[y]) par[x] = y, num[y] += num[x];
        else{
            par[y] = x, num[x] += num[y];
            if(rank[x] == rank[y]) rank[x]++;
        }
    }

    int size(int x) {return num[root(x)];}

    bool same(int x, int y){
        return root(x) == root(y);
    }

    void clear(){
        fill(par.begin(), par.end(), -1);
        fill(rank.begin(), rank.end(), 0);
        fill(num.begin(), num.end(), 1);
    }
};

template<typename Idempotent_Semigroup>
struct Sparse_Table{
    Idempotent_Semigroup ope(Idempotent_Semigroup a, Idempotent_Semigroup b){
        return min(a, b);
    }
    const int n;
    vector<vector<Idempotent_Semigroup>> st;
    vector<int> lookup;

    Sparse_Table(const vector<Idempotent_Semigroup> &table) : n(sz(table)){
        st.resize(n);
        rep(i, n) st[i].pb(table[i]);
        for(int k = 0; (1<<k) < n; k++){
            rep(i, n){
                if(i+(1<<k) < n) st[i].pb(ope(st[i][k], st[i+(1<<k)][k]));
                else st[i].pb(st[i][k]);
            }
        }
        lookup.resize(n+1);
        lookup[0] = -1;
        rep2(i, 1, n) lookup[i] = lookup[i/2]+1;
    }

    Idempotent_Semigroup query(int l, int r){
        if(l == r) return Idempotent_Semigroup(INF, -1);
        int k = lookup[r-l];
        return ope(st[l][k], st[r-(1<<k)][k]);
    }

    Idempotent_Semigroup operator [] (int i) {return st[i][0];}
};

int main(){
    int N; ll D;
    cin >> N >> D;
    ll A[N];
    rep(i, N) cin >> A[i];
    vector<pli> table1, table2;
    rep(i, N){
        table1.pb(pli(A[i]+D*i, i));
        table2.pb(pli(A[i]-D*i, i));
    }
    Sparse_Table<pli> st1(table1), st2(table2);
    Union_Find_Tree uft(N);
    vector<int> l(N), r(N);
    iota(all(l), 0), iota(all(r), 1);
    ll ans = 0;
    while(sz(l) > 1){
        int n = sz(l);
        rep(i, n){
            ll vl = st2.query(0, l[i]).first + st1.query(l[i], r[i]).first;
            ll vr = st1.query(r[i], N).first + st2.query(l[i], r[i]).first;
            if(vl < vr){
                int idl = st2.query(0, l[i]).second, idr = st1.query(l[i], r[i]).second;
                if(!uft.same(idl, idr)){
                    ans += A[idl]+A[idr]+D*(idr-idl);
                    uft.unite(idl, idr);
                }
            }
            else{
                int idl = st2.query(l[i], r[i]).second ,idr = st1.query(r[i], N).second;
                if(!uft.same(idl, idr)){
                    ans += A[idl]+A[idr]+D*(idr-idl);
                    uft.unite(idl, idr);
                }
            }
        }
        l.clear(), r.clear();
        int now = 0;
        while(now < N){
            l.pb(now), r.pb(now += uft.size(now));
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200010],d,n,ans;
int dpl[200010],dpr[200010];
signed main(){
	cin>>n>>d;
	for(int i=1 ;i<=n;i++)cin>>a[i],ans+=a[i];
	ans+=(n-1)*d;
	dpr[1]=a[1];
	for(int i=2;i<=n;i++)dpr[i]=min(a[i],dpr[i-1]+d);
	dpl[n]=a[n];
	for(int i=n-1;i>=1;i--)dpl[i]=min(a[i],dpl[i+1]+d);
	for(int i=2;i<n;i++)ans+=min(dpl[i],dpr[i]);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

const double pi = 3.141592653589793238462643383279;

using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;

#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)

// ここから編集

void solve(){
  int N;cin >> N;
  vector<ll>a(N);
  REP(i,N)cin >> a[i];

  string s; cin >> s;
  vector<ll> b;
  for(int i=N-1; i>=0; i--){
    ll v = a[i];
    for(auto x:b) chmin(v, v^x);
    if(v){
      if(s[i] == '1'){
        cout << 1 << endl;
        return;
      }
      b.push_back(v);
    }
  }
  cout << 0 << endl;
  return;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);

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

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

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int ans = 0;
  for (int i = 1; i < N; i++) {
    int d = A[i - 1] - A[i];
    if (d > 0) {
      ans += d;
      A[i] = A[i - 1];
    }
  }

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


#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include <iomanip>

std::vector<int> parse_line(std::string const& line) {
    using namespace std;
    vector<int> result;
    istringstream s(line);
    string element;
    while (getline(s, element, ' ')) {
        stringstream buf;
        int value = 0;
        buf << element;
        buf >> value;
        result.push_back(value);
    }
    return std::move(result);
}
int main(int argc, char* argv[])
{
    using namespace std;
    while (true) {
        string line;
        getline(std::cin, line);
        auto elements = parse_line(line);
        auto length = elements[0];
        if (length == 0) break;
        vector<int> total(length + 1);
        size_t width = length + 1;
        for (size_t i = 0; i < length; ++i) {
            getline(cin, line);
            elements = parse_line(line);
            int sum = 0;
            for (size_t n = 0; n < length; ++n) {
                int val = elements[n];
                sum += val;
                total[n] += val;
                cout << setw(5) << val;
            }
            cout << setw(5) << sum << endl;
            total[length] += sum;
        }
        for (size_t i = 0; i < width; ++i) {
            cout << setw(5) << total[i];
        }
        cout << endl;
    }

    return 0;
}


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

using namespace std;

vector<int> p, r;

void makeSet(int x){
  p[x] = x;
  r[x] = 0;
}

int findSet(int x){
  if(x != p[x])p[x] = findSet(p[x]);
  return p[x];
}

void link(int x, int y){
  if(r[x] > r[y]) p[y] = x;
  else {
    p[x] = y;
    if(r[x] == r[y]) r[y] = r[y] + 1;
  }
}

void unite(int x, int y){
  link(findSet(x), findSet(y));
}

struct Edge{
  int a, b, cost;

  bool operator<(const Edge& o) const{
    return cost < o.cost;
  }
};

struct Graph{
  int n;
  vector<Edge> es;

  int kruskal(){
    sort(es.begin(), es.end());

    int min_cost = 0;

    for(int i=0; i<es.size(); i++){
      Edge& e = es[i];
      if(!(findSet(e.a)==findSet(e.b))){
	min_cost += e.cost;
	unite(e.a, e.b);
      }
    }

    return min_cost;
  }
};

Edge e;

int main(){

  int V, E, i;
  Graph g;

  cin >> V >> E;

  p.resize(V);
  r.resize(V);

  for(i=0; i<V; i++) makeSet(i);

  for(i=0; i<E; i++){
    cin >> e.a >> e.b >>e.cost;
    g.es.push_back(e);
  }

  cout << g.kruskal() << endl;

  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

vector<int>E[200000];
int id[200000];
int col[200000];
bool b[200000];

bool dfs(int v,int k,int c){
	id[v]=k;
	col[v]=c;
	bool flag=true;
	for(int u:E[v]){
		if(id[u]==-1){
			flag&=dfs(u,k,!c);
		}
		else{
			if(col[u]==col[v])flag=false;
		}
	}
	return flag;
}
vector<int>v[200000];

int main(){
	int n,m;cin>>n>>m;
	rep(i,m){
		int a,b;scanf("%d%d",&a,&b);a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	memset(id,-1,sizeof(id));
	int cnt=0;
	rep(i,n){
		if(id[i]==-1){
			b[cnt]=dfs(i,cnt,0);
			cnt++;
		}
		v[id[i]].push_back(i);
	}
	ll c1=0,c2=0,c3=0;
	rep(i,cnt){
		if(v[i].size()==1){
			c1++;
			continue;
		}
		c2++;
		if(b[i])c3++;
	}
	cout<<c1*(n-c1)*2+c1*c1+c2*c2+c3*c3<<endl;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = (1LL<<31)-1;
const int mod = 1e9 + 7;

struct SegmentTree {
  vector<int> data, lazy;
  int sz;
  SegmentTree(int n) {
    sz = 1; while(sz < n) sz <<= 1;
    data.resize(2*sz-1, inf);
    lazy.resize(2*sz-1, -1);
  }
  void lazy_evaluate(int k, int l, int r) {
    if(~lazy[k]) {
      data[k] = lazy[k];
      if(r - l > 1) {
	lazy[2*k+1] = lazy[k];
	lazy[2*k+2] = lazy[k];
      }
      lazy[k] = -1;
    }
  }
  void update(int a, int b, int x, int k, int l, int r) {
    lazy_evaluate(k, l, r);
    if(r <= a || b <= l) return;
    if(a <= l && r <= b) {
      lazy[k] = x;
      lazy_evaluate(k, l, r);
      return;
    }
    update(a, b, x, 2*k+1, l, (l+r)/2);
    update(a, b, x, 2*k+2, (l+r)/2, r);
  }
  void update(int a, int b, int x) {
    update(a, b, x, 0, 0, sz);
  }
  int query(int i, int k, int l, int r) {
    lazy_evaluate(k, l, r);
    if(r - l == 1) return data[k];
    if(l <= i && i <= r) {
      if(i < (l+r)/2) return query(i, 2*k+1, l, (l+r)/2);
      else return query(i, 2*k+2, (l+r)/2, r);
    }
    return inf;
  }
  int query(int i) {
    return query(i, 0, 0, sz);
  }
};

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n, q;
  cin >> n >> q;
  SegmentTree seg(n);
  rep(i, q) {
    int com, s, t, x;
    cin >> com >> s;
    if(com == 0) cin >> t >> x, seg.update(s, t+1, x);
    else cout << seg.query(s) << endl;
  }

  return 0;
}

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

typedef pair<int, int> Pair;

vector<Pair> rects;

bool Compare(const Pair &a, const Pair &b) {
  int h1 = a.first;
  int h2 = b.first;
  int w1 = a.second;
  int w2 = b.second;
  int t1 = h1*h1+w1*w1;
  int t2 = h2*h2+w2*w2;
  if (t1 != t2) return t1 < t2;
  return h1 < h2;
}

int main() {
  for (int i=1; i<200; i++) {
    for (int j=i+1; j<200; j++) {
      rects.push_back(Pair(i, j));
    }
  }
  sort(rects.begin(), rects.end(), Compare);

  while (1) {
    int h, w;
    scanf("%d%d", &h, &w);
    if (h == 0 && w == 0) return 0;

    Pair ans = *upper_bound(rects.begin(), rects.end(), Pair(h, w), Compare);
    printf("%d %d\n", ans.first, ans.second);
  }
}


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

int main(){

  long long a,b,c,k,sum=0;
  cin >> a >> b >> c >> k;
  sum+=(a+b+c);
  long long x=max(a,max(b,c));
  sum-=x;
  for(long long i=0; i<k; i++){
    x*=2;
  }
  cout << x+sum << endl;
}

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<locale.h>
#include<set>
#include<stack>
#include<queue>
using namespace std;

int s[5];

int ans=-1;
string x;
int flg=0;
void wf(int i,int w ,int z) //i文字目までの部分和w zで＋と－の状況を管理
{
  if(flg==1){return;}
  int g,h;
  //+を挿入した場合の処理
 g=w+s[i]; h=z*10+1; if(i!=4){wf(i+1,g,h);} if(i==4&&g==7){ans=h; flg=1; return;}
  //-を挿入した場合の処理
 g=w-s[i]; h=z*10; if(i!=4){wf(i+1,g,h);} if(i==4&&g==7){ans=h;flg=1; return;}
}
int main()
{
 cin>>x;
 for(int i=1;i<=4;i++)
 {
	s[i]=x[i-1]-'0';
 }
 wf(2,s[1],0);
 //ansを解読する作業
 int dx=100;
 for(int i=1;i<4;i++)
 {
	printf("%d",s[i]);
	if(ans>=dx){printf("+");ans=ans-dx;}
	else {printf("-");}
	dx=dx/10;
 }
 printf("%d=7",s[4]);
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main() {
	vector<int> W, K;
	int dommy;
	for (int i = 0; i < 10; ++i) {
		cin >> dommy;
		W.push_back(dommy);
	}
	sort(W.begin(), W.end(), greater<int>());
	for (int i = 0; i < 10; ++i) {
		cin >> dommy;
		K.push_back(dommy);
	}
	sort(K.begin(), K.end(), greater<int>());
	int Wpoint = 0, Kpoint = 0;
	for (int i = 0; i < 3; ++i) {
		Wpoint += W[i];
	}
	for (int i = 0; i < 3; ++i) {
		Kpoint += K[i];
	}
	cout << Wpoint << ' ' << Kpoint << endl;
	return 0;
}

//32
#include<iostream>
#include<algorithm>
#include<numeric>
#include<functional>

using namespace std;

int main(){
  int p[2][10];
  for(int i=0;i<2;i++){
    for(int j=0;j<10;j++){
      cin>>p[i][j];
    }
  }
  for(int i=0;i<2;i++){
    sort(p[i],p[i+1],greater<int>());
    cout<<accumulate(p[i],p[i]+3,0)<<((i==0)?' ':'\n');
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define all(v) v.begin(), v.end()

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

char toLowerCase(char c){
  if(isLowerCase(c)){
    return c;
  }else{
    return c+'a'-'A';
  }
}
char toUpperCase(char c){
  if(isLowerCase(c)){
    return c-('a'-'A');
  }else{
    return c;
  }
}
ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
int main() {
  bool ans=false;
  int a,b,c;cin>>a>>b>>c;
  if(a<=c&&c<=b){
    ans=true;
  }else if(b<=c&&c<=a){
    ans=true;
  }
  cout<<yesno(ans)<<endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin >> a >> b >> c;

    if(min(a,b)<=c && max(a,b)>=c) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


#include<bits/stdc++.h>
#include<cctype>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define reps(i,n) for (int i=1;i<=(n);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rreps(i,n) for (int i=(n);i>0;i--)
#define all(v) (v).begin(),(v).end()
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int,int>
template<class T> inline bool chmin(T &a, T b) {if (a>b) {a = b; return 1;} return 0;};
template<class T> inline bool chmax(T &a, T b) {if (a<b) {a = b; return 1;} return 0;};
typedef long long int ll;
const long long INF = 1LL << 60;

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

  vector<string> rec(N);
  int iter = 0;
  int ok = 0, ng = N, mid = 0;
  while (iter<=20){
    cout << mid << endl;
    string s; cin >> s;
    cout.flush();
    if (s=="Vacant") break;
    iter++;
    rec[mid] = s;

    if ((mid-ok)%2 == (rec[ok]!=rec[mid])) ok = mid;
    else ng = mid;

    mid = (ok+ng)/2;
  }

  // cout << fixed << setprecision(10);

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main()
{
  int i = 0, n = 0;
  fscanf(stdin, "%d", &n);
  while (++i <= n) {
    int x = i;
    if (x % 3 == 0 || x % 10 == 3) {
      fprintf(stdout, " %d", i);
    }
    else {
      for (x /= 10; x > 0; x /= 10) {
        if (x % 10 == 3) {
          fprintf(stdout, " %d", i);
          break;
        }
      }
    }
  }
  fprintf(stdout, "\n");
  return 0;
}


// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(ll __x = __start; __x < __end; __x++)

int main() {
  ll n, m; cin >> n >> m;
  vector<ll> D;
  for (ll i=1; i*i<=m; i++) {
    if (m%i == 0) {
      D.push_back(i);
      if (i*i<=m) D.push_back(m/i);
    }
  }

  sort(D.rbegin(), D.rend());

  for (auto &&d: D) {
    if (n*d <= m) {
      cout << d << endl;
      return 0;
    }
  }

  cout << -1 << endl;
  return 0;
}

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define rep(pp,nn) for (int i = pp; i < nn; ++i)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define endl "\n"

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

string int_to_string(int x) {
    stringstream ss;
	ss << x;
	string ni = ss.str();
	return ni;
}
int string_to_int(string x) {
	int n;
	stringstream s(x);
	s >> n;
	return n;
}

int64_t power_mod(ll a, ll b, ll mo) {
	ll ans = 1;
	while (b) {
		if(b & 1) {
			ans = (ans % mo * a % mo) % mo;
		}
		a = (a % mo * a % mo) % mo;
		b /= 2;
	}
	return ans;
}
int64_t POW(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if(b & 1) {
			ans = ans * a;
		}
		a = a * a;
		b /= 2;
	}
	return ans;
}

vector<int64_t> INVERSE(int64_t nn, int64_t mo) {
	vector<int64_t> inv(nn + 1);
	vector<int64_t> mul(nn + 1);
	inv[0] = 1;
    inv[1] = 1;
	for (ll i = 2; i <= nn; ++i) {
		inv[i] = (mo - (mo / i) * inv[mo % i] % mo) % mo;
	}
	mul[0] = 1;
	for (int i = 1; i <= nn; ++i) {
		mul[i] = (mul[i - 1] * inv[i]) % mo;
	}
	return mul;
}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int mod = 1e9 + 7;

void add_self(int &a , int b) {
	a += b;
	if (a >= mod) {
		a -= mod;
	}
}

int main() {
	IOS; //READ THE QUESTION PROPERLY BSDK
	string s;
	int d;
	cin >> s >> d;
	int n = (int)s.size();
	vector<vector<int>> dp(d,vector<int> (2));
	//int m = 1e9 + 7;
	dp[0][0] = 1;
	for (int i = 0; i < n; ++i) {
		vector<vector<int>> new_dp(d, vector<int> (2));
		for (int j = 0; j < d; ++j) {
			for (bool sm_already : {false, true}) {
				for (int digit = 0; digit < 10; ++digit) {
					if (digit > s[i] - '0' && !sm_already) {
						break;
					}
				//debug(j,dp[j][0], s[i] - '0',digit,i);
					add_self(new_dp[(j + digit) % d][sm_already || (digit < s[i] - '0')],dp[j][sm_already]);
			    }
			}
		}
		//adebug(new_dp);
		dp = new_dp;
	}
	int answer = (dp[0][0] + dp[0][1]) % mod;
	answer--;
	if (answer == -1) {
		answer = mod - 1;
	}
	cout << answer;
}


#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int x;
    int y;
    int z;
    cin >> x;
    cin >> y;
    cin >> z;
    cout << z << " " << x << " " << y << endl;
}

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

int main(){
    int o,u,c;
    cin >> o >> u >> c;
    if(o<c&&c<u){
        cout << "Yes" << endl;
    }
    else if(o>c&&c>u){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}


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

using namespace std;

int main()
{
	int N, Y;
	cin >> N >> Y;

	int set[3] = {-1,-1,-1};

	for (int i = 0; i <= N; i++)
	{
		for (int j = 0; i + j <= N; j++)
		{
			int k = N - i - j;
			int z = 10000 * i + 5000 * j + 1000 * k;
			if (z == Y)
			{
				set[0] = i;
				set[1] = j;
				set[2] = k;
				goto loopend;
			}
			else if(z > Y)
			{
				break;
			}
		}
	}

loopend:
	cout << set[0] << ' ' << set[1] << ' ' << set[2];

}

#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
#define i(x) cin>>x
#define fr(n,x) for(ll n=0;n<x;n++)
#define pf(x) cout<<x
const ll mod=1e9+7 ;

int main()
{
    ios_base::sync_with_stdio(false),cin.tie(NULL);
    ll x,y;
    cin>>x>>y;
    if (y-x>=2019)
    {
        cout<<0;
        return 0;
    }
    ll res=INT_MAX;
    for(ll n=x;n<=y;n++)
    {
        for(ll k=n+1;k<=y;k++)
        {
            res=min(res,((n%2019)*(k%2019))%2019);
        }
    }
    cout<<res;
}


#include <bits/stdc++.h>
//#include <tr1/unordered_map>
//#include"Bignum/bignum.h"
//#define big bignum
#define lowbit(x) (x & -x)
#define debug(x) (cout << "#x = " << (x) << endl)
#define Set(x, i) memset (x, i, sizeof(x))
#define R register
#define For(i, j, k) for(R int i = (j); i <= (int) (k); ++i)
#define Rep(i, j, k) for(R int i = (j); i >= (int) (k); --i)
#define Cross(i, j, k) for(R int i = (j); i; i = (k))
using namespace std;
typedef long long ll;
const ll N = 21;
const ll INF = 5e16;

namespace IO {

	inline char gc() {
		static char buf[100000], *p1 = buf, *p2 = buf;
		return (p1 == p2) && (p2 = (p1 = buf) +
			fread(buf, 1, 100000, stdin), p1 == p2)? EOF: *p1++;
	}

	#define dd ch = getchar()
	inline ll read() {
		R ll x = 0; R int f = 0; R char dd;
		for (; !isdigit(ch); dd) f ^= (ch == '-');
		for (; isdigit(ch); dd)  x = x * 10 + (ch ^ 48);
		return f? -x: x;
	}
	#undef dd

	inline void write ( ll x ) {
		if (x < 0) x = -x, putchar('-');
		if (x > 9) write(x / 10); putchar(x % 10 | 48);
	}

	inline void wrn ( ll x ) { write(x), putchar(' '); }

	inline void wln ( ll x ) { write(x), putchar('\n'); }

	inline void wlnn ( ll x, ll y ) { wrn(x), wln(y); }

}

using IO::wln;
using IO::read;

namespace Cesare {

	/*
		f[i][S] 表示当前走到 i ，走过的路径
		状态为 S 最大可以保留的权值和。
		每次转移枚举加入一个点集还是一个点。
		这基于一个性质：每次加入的一个点或点集必定
		只与一条简单路径上的一个点相连，所以不会影
		响简单路径数量。

		f[j][S | (1 << j - 1)] = max f[i][S] + w[i][j])

		now <- 包含 j 但不包含 S 的所有状态
		f[j][S | now] = max f[i][S] + g[now] + d[i][now]
	*/

	ll n, m, Max, w[N][N];
	ll g[(1 << 16) + 3], d[N][(1 << 16) + 3], f[N][(1 << 16) + 3];

	inline void Cmax ( ll &a, ll b ) { (a < b) && (a = b); }

	int main() {
		n = read(), m = read();
		for (int i = 1, u, v; i <= m; ++i)
			u = read(), v = read(), w[u][v] = w[v][u] = read();

		Set (f, -1);
		f[1][1] = 0, Max = (1 << n) - 1;
		For ( S, 0, Max ) For ( i, 1, n )
			if (S & (1 << i - 1)) For ( j, 1, i )
				if ((i ^ j) && (S & (1 << j - 1))) g[S] += w[i][j];
		For ( S, 0, Max ) For ( i, 1, n )
			if (!(S & (1 << i - 1))) For ( j, 1, n )
				if ((S & (1 << j - 1))) d[i][S] += w[i][j];

		For ( S, 0, Max ) For ( i, 1, n )
			if (S & (1 << i - 1) && f[i][S] >= 0) {
				ll nowS = Max ^ S;
				for (int now = nowS; now; now = nowS & (now - 1))
					Cmax(f[i][S | now], f[i][S] + g[now] + d[i][now]);
				// 转移点集
				for (int j = 1; j <= n; ++j)
					if (!(S & (1 << j - 1)) && w[i][j])
						Cmax(f[j][S | (1 << j - 1)], f[i][S] + w[i][j]);
				// 转移点
			}
		return wln(g[Max] - f[n][Max]), 0;
	}

}

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

	return Cesare :: main();
}

/*

*/


#include <iostream>

int main()
{
    int64_t x;
    std::cin >> x;
    // 5と6を交互に出す
    // どちらから始めてもいいので、最後を6にできる
    const auto r = x % 11;
    std::cout << x / 11 * 2 + (r + 5) / 6 << std::endl;
    return 0;
}

#include<stdio.h>
#include<iostream>
#include <algorithm>
#include <functional>
#include <array>
using namespace std;
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
//void margesort(int a[], int a_len, int left, int right);
//void margesortD(int a[], int b[], int a_len, int left, int right);
//int temp[100000];
//int tempb[100000];
//int b[100000];
//int c[100000];
int main() {
	/*
		int a, b;
		cin >> a;
		cin >> b;
		if (a <= 5) {
			cout << 0;
		}
		else if (a <= 12) {
			cout << b / 2;
		}else{
			cout << b;
		}

		return 0;
		*/
		//B
		/*long long int r, d, x;
		cin >> r;
		cin >> d;
		cin >> x;

		int i;
		for (i = 0; i < 10; i++) {
			x = r * x - d;
			cout << x;
			cout <<'\n';
		}

		return 0;*/
		//C
		/*int n, m;
		int l, r;
		cin >> n;
		cin >> m;

		int lmax, rmin;
		lmax = 1;
		rmin = n;
		int i;
		for (i = 0; i < m; i++) {
			cin >> l;
			cin >> r;
			if (l > lmax) {
				lmax = l;
			}
			if (r < rmin) {
				rmin = r;
			}
		}
		if (lmax <= rmin) {
			cout << (rmin - lmax + 1);
		}
		else {
			cout << 0;
		}

		return 0;*/

		//D
	/*int n, m;
	cin >> n;
	cin >> m;
	int i;
	int a[100000];
	for (i = 0; i < n; i++) {
		cin >> a[i];
	}
	margesort(a, n, 0, n - 1);

	for (i = 0; i < m; i++) {
		cin >> b[i];
		cin >> c[i];

	}
	margesortD(c, b, m, 0, m - 1);
	//sort(b.begin(), b.end());

	long long int d;
	d = 0;
	int j, k, flag;
	i = 0;
	j = m - 1;
	flag = 0;
	while (flag == 0) {
		for (k = i; k < i + b[j]; k++) {
			if (a[k] < c[j]) {
				a[k] = c[j];

			}
			else {
				flag = 1;
			}
			if (k == n) {
				flag = 1;
				break;
			}
		}
		i = i + b[j];
		j--;
		if (j == -1) { flag = -1; }
	}
	for (i = 0; i < n; i++) {
		d = d + a[i];
	}
	cout << d;

	return 0;
	*/

	//E
	long long int n, m, k;
	cin >> n;
	cin >> m;
	cin >> k;
	long long int p;
	p = 1000000007;
	long long int c;
	c = 0;
	//2駒総コストを求める.
	int i;
	int j;
	c = (((((((n * m) % p) * (n + m)) % p) * (n * m - 1)) % p) * modinv(6, p)) % p;
	//それを(nm-2)C(k-2)倍する.
	j = 1;
	for (i = n*m-2; i > (n * m - 2)-(k-2); i--) {
		c = (c * i) % p;
		c = (c * modinv(j, p)) % p;
		j++;
	}
	cout << c;
	return 0;

}
/*void margesort(int a[], int a_len, int left, int right)
{
	int i, j, mid, L, R;

	if (right <= left)
		return;

	mid = (left + right) / 2;
	margesort(a, mid, left, mid);
	margesort(a, a_len - mid, mid+1, right);

	for (i = left; i <= mid; i++)
		temp[i] = a[i];
	for (i = mid+1, j = right; i <= right; i++, j--)
		temp[i] = a[j];

	L = left;
	R = right;

	for (i = left; i <= right; i++)
	{
		if (temp[L] <= temp[R])
		{
			a[i] = temp[L];
			L++;
		}
		else
		{
			a[i] = temp[R];
			R--;
		}
	}
}

void margesortD(int a[], int b[], int a_len, int left, int right) {
	int i, j, mid, L, R;

	if (right <= left)
		return;

	mid = (left + right) / 2;
	margesortD(a, b, mid, left, mid);
	margesortD(a, b, a_len - mid, mid+1, right);

	for (i = left; i <= mid; i++) {
		temp[i] = a[i];
		tempb[i] = b[i];
	}
	for (i = mid+1, j = right; i <= right; i++, j--) {
		temp[i] = a[j];
		tempb[i] = b[j];
	}

	L = left;
	R = right;

	for (i = left; i <= right; i++)
	{
		if (temp[L] <= temp[R])
		{
			a[i] = temp[L];
			b[i] = tempb[L];
			L++;
		}
		else
		{
			a[i] = temp[R];
			b[i] = tempb[R];
			R--;
		}
	}
}*/

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
	return modpow(a, mod - 2, mod);
}


#include <iostream>
#include <string>
using namespace std;

enum eKey{
	ENCYPTION,
	CONJUGATED,
};

void conv( string& s, int i, eKey e ){
	if ( e == CONJUGATED ){
		i = 26 - i;
	}
	for( unsigned j = 0; j < s.size(); ++j ){
		if ( s[ j ] >= 'a' && s[ j ] <= 'z' ){
			s[ j ] = ( s[ j ] - 'a' + i ) % 26 + 'a';
		}
	}
}

int main(){
	string s, sThe, sThis, sThat;
	while ( getline ( cin, s ) ){

		for( int i = 0; i < 26; ++i ){
			sThe = "the";
			sThis = "this";
			sThat = "that";
			conv( sThe, i , ENCYPTION );
			conv( sThis, i , ENCYPTION );
			conv( sThat, i , ENCYPTION );
			if ( s.find( sThe ) != string::npos ||
				 s.find( sThis ) != string::npos ||
				 s.find( sThat ) != string::npos ){
					conv( s, i, CONJUGATED );
					break;
			}
		}
			cout << s << endl;
	}

	return 0;
}

#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;
int key;

int dist(char s1,char s2){

  if(s1==s2){
    return 0;
  }else if(s1<s2){
    return s2-s1;
  }else{
    return 'z'-s1+s2-'a'+1;
  }
}

bool checkThe(char s[]){
  if(strlen(s)!=3){
    return false;
  }else{
    if(dist('t',s[0])==dist('h',s[1]) && dist('h',s[1])==dist('e',s[2])){
      key=dist('t',s[0]);
      return true;
    }
  }
  return false;
}
bool checkThis(char s[]){
  if(strlen(s)!=4){
    return false;
  }else{
    if(dist('t',s[0])==dist('h',s[1]) && dist('h',s[1])==dist('i',s[2]) && dist('i',s[2])==dist('s',s[3])){
      key=dist('t',s[0]);
      return true;
    }
  }
  return false;
}
bool checkThat(char s[]){
  if(strlen(s)!=4){
    return false;
  }else{
    if(dist('t',s[0])==dist('h',s[1]) && dist('h',s[1])==dist('a',s[2]) && dist('a',s[2])==dist('t',s[3])){
      key=dist('t',s[0]);
      return true;
    }
  }
  return false;

}
int main(){

  char str[100];
  char temp[100];

  char *tok;
  bool f;
  while(fgets(str,sizeof(str),stdin)!=NULL){
    key=0;
    f=false;
    for(int i=0;i<strlen(str);i++)
      temp[i]=str[i];
    temp[strlen(str)]='\0';

    str[strlen(str)]='\0';
    str[strlen(str)-1]='\0';
    tok=strtok(str," ");

    if(strlen(tok)==3){
      if(checkThe(tok)==true){
	f=true;
      }
    }else if(strlen(tok)==4){
      if(checkThat(tok)==true){
	f=true;
      }else if(checkThis(tok)==true){
	f=true;
      }
    }
    while(f==false && tok!=NULL){
      tok=strtok(NULL," ");
      if(tok!=NULL){
	if(strlen(tok)==3){
	  if(checkThe(tok)==true){
	    f=true;
	  }
	}else if(strlen(tok)==4){
	  if(checkThat(tok)==true){
	    f=true;
	  }else if(checkThis(tok)==true){
	    f=true;
	  }
	}
      }
    }

    for(int i=0;i<strlen(temp);i++){
      if('a'<=temp[i] && temp[i]<='z'){
	if(temp[i]-key>'z'){
	  printf("%c",(temp[i]-key));
	}else if(temp[i]-key<'a'){
	  printf("%c",(temp[i]-key)+26);
	}else{
	  printf("%c",(temp[i]-key));
	}
      }else{
	printf("%c",temp[i]);
      }
    }

  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define pb push_back
#define mod 998244353
const ll N =30005 ;
ll dp[2*N] ;

int main ()
{
    ll n , t ;
    cin>> n >> t ;
    ll i ,j,x,y;
    dp[0] = 1 ;
    vector<pair<ll,ll> > v(n) ;
    for(i = 0 ; i< n ;i++) cin >> v[i].first >> v[i].second ;
    sort(v.begin(),v.end()) ;
    for(i = 1 ; i<= n ; i++) {
        x = v[i-1].first , y = v[i-1].second ;
        for (j = t-1 ; j>=0 ; j--) if (dp[j]) dp[j+x] = max(dp[j+x],dp[j]+y) ;

    }
    ll ans = 0 ;
    for (i = 0 ; i<2*N ; i++) ans = max(ans,dp[i]) ;
    cout << ans -1 << endl ;
    return 0 ;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) for(int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define I(x) cin >> x;
#define D(x) cerr << (x) << " ";
#define BR cerr << "\n";
#define P(x) cout << (x) << endl;
#define FIX cout << fixed << setprecision(10);

/* const */
const int ARRAY = 100005;
const int INF = 1001001001; // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll Q;
ll ret = 0;

struct Event {
  ll t, type, X;
  bool operator<(const Event& a) {
    return t < a.t;
  }
};

vector<Event> eves;
multiset<ll> ms;

int main(void){

  I(N);
  I(Q);
  ll S, T, X;
  REP(i, N) {
    I(S);
    I(T);
    I(X);
    Event a = {S-X, 1, X};
    Event b = {T-X, -1, X};
    eves.pb(a);
    eves.pb(b);
  }
  sort(ALL(eves));

  ll d;
  ll k = 0;
  REP(i, Q) {
    I(d);
    while (k < eves.size() && eves[k].t <= d) {
      Event e = eves[k];
      if (e.type == 1) {
        ms.insert(e.X);
      } else {
        ms.erase(ms.find(e.X));
      }
      k++;
    }

    if (ms.empty()) {
      P(-1);
    } else {
      P(*ms.begin());
    }

  }
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vl vector
#define ff first
#define ss second
using namespace std;
int main()
{
    ll a,b,i,j,n,t,c,m;
    ll arr[3];
    for(i=0;i<3;i++) cin>>arr[i];
    sort(arr,arr+3);
    cout<<arr[0]+arr[1];
}

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

int main() {
	int A,B;
	cin >> A>>B;
	int ans = 0;
	for (int n = 0;n<2; ++n) {
		if (A > B) {
			ans += A;
			--A;
		}
		else {
			ans += B;
			--B;
		}
	}
	cout << ans<<endl;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;

int main () {
	double a,b,c,d;
	cin >> a >> b >> c >> d;
	cout << setprecision(20) <<sqrt((a-c)*(a-c)+(b-d)*(b-d));
	return 0;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<string>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define reps(i,n) for(int i=1;i<=(int)n;i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a,x) lb(all(a), x) - a.begin()
#define UB(a,x) ub(all(a), x) - a.begin()
#define MOD 1000000007
#define itn int
#define enld endl
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b){if(b<a){a=b; return 1;} return 0;}
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main(){
    int N; cin>> N;
    string A,B,C; cin >> A >> B >> C;
    int ans  =0;
    rep(i,N){
        int cnt = 0;
        if((A[i] != B[i]) && (B[i] != C[i]) && (C[i] != A[i])) ans += 2;
        else if((A[i] == B[i]) && (B[i] == C[i]) && (C[i] == A[i])) continue;
        else ans += 1;
    }
    cout << ans << enld;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
int main(void){
    string s;
    cin>>s;
    int n=s.size();
    bool ok=false;
    int L=-1,R=-1;
    if(n==2&&s[0]==s[1]){
        L=1,R=2;
    }
    for(int i=0;i<n-2;i++){
        if(s[i]==s[i+1]||s[i+1]==s[i+2]||s[i+2]==s[i]){
            L=i+1,R=i+3;
        }
    }
    cout<<L<<" "<<R<<endl;
}

#include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<(int)(n);i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14;
constexpr double eps = 1e-10;
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int main(){
    ll K; cin >> K;
    const ll N = 50;
    vector<ll> a(N,N-1+K/N);
    ll rem = K%N;
    for(int i=0;i<rem;i++){
        a[i] += N - (rem-1);
    }
    for(int i=rem;i<N;i++){
        a[i] -= rem;
    }

    cout << N << endl;
    for(int i=0;i<N;i++){
        if(i) cout << " ";
        cout << a[i];
    }
    cout << endl;
}


#include <bits/stdc++.h>

using namespace std;
using ll =long long;
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//Write From this Line

int main()
{
	ll a, b, k;
	cin >> a >> b >> k;
	int count = 1;
	while(k--){
		if(count%2){
			// a をそうさ
			if(a%2) a--;
			b += a/2;
			a /= 2;
		} else{
			if(b%2) b--;
			a += b/2;
			b /= 2;
		}
		count ++ ;
	}
	cout << a << " " << b << endl;
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;n>i;++i)
using namespace std;
using ll=int64_t;
using vi=vector<int>;
using VI=vector<ll>;
using vis=vector<string>;
using vvi=vector<vi>;
int main(){
    int h,w,n;
    cin>>h>>w>>n;
    if(n%max(h,w)==0){
        cout<<n/max(h,w)<<endl;
        return 0;
    }
    else{
        cout<<n/max(h,w)+1<<endl;
        return 0;
    }
}


#include<bits/stdc++.h>
#define mp make_pair
#define endl "\n"
#define v vector
#define b begin()
#define e end()
using namespace std;
typedef long long ll;
typedef long l;
typedef long long unsigned ull;

int main(){

    /*int t;
    while(t--){

    }*/
    ll n;
    cin>>n;
    ll m = n/500;
    ll q = m*1000;
    n -= (m*500);
    ll p = (n/5)*5;
    cout<<q+p;

    return 0;

}

#include<iostream>
using namespace std ;

int main()
{
  string S ;
  cin>>S ;

  int SIZE=S.size() ;
  if(SIZE==2) cout<<S<<endl ;
  else for(int i=SIZE-1;i>=0;i--) cout<<S.at(i) ;

  return 0 ;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <utility>
#include <complex>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <tuple>
#include <bitset>
#include <limits>
#include <algorithm>
#include <array>
#include <random>
#include <complex>
#include <regex>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define quickIO()	{cin.tie(0);	cout.sync_with_stdio(false);}
#define reach(i,a)	for(auto i:a)
#define rep(i,n)	for(int i=0;i<(int)n;i++)
#define REP(i,n)	for(int i=0;i<=(int)n;i++)
#define srep(i,a,n)	for(int i=a;i<(int)n;i++)
#define SREP(i,a,n)	for(int i=a;i<=(int)n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define RREP(i,n)	for(int i=n;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define mt	make_tuple
#define pb	push_back
#define fst	first
#define scn second
int bitcnt(ll x) {
	x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) + (x & 0x5555555555555555);
	x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) + (x & 0x3333333333333333);
	x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) + (x & 0x0F0F0F0F0F0F0F0F);
	x = ((x & 0xFF00FF00FF00FF00) >> 8) + (x & 0x00FF00FF00FF00FF);
	x = ((x & 0xFFFF0000FFFF0000) >> 16) + (x & 0x0000FFFF0000FFFF);
	x = ((x & 0xFFFFFFFF00000000) >> 32) + (x & 0x00000000FFFFFFFF);
	return x;
}
int bitcnt(int x) {
	x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555);
	x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333);
	x = ((x & 0xF0F0F0F0) >> 4) + (x & 0x0F0F0F0F);
	x = ((x & 0xFF00FF00) >> 8) + (x & 0x00FF00FF);
	x = ((x & 0xFFFF0000) >> 16) + (x & 0x0000FFFF);
	return x;
}
ll sqrt(ll x) {
	ll left = 0, right = x;
	rep(i, 100) {
		ll mid = (left + right) >> 1;
		if (mid*mid <= x)	left = mid;
		else	right = mid;
	}
	return left;
}
ll gcd(ll a, ll b) {
	return a%b == 0 ? b : gcd(b, a%b);
}
#define debug(x)	printf("Case #%d: ", x)
#define DEBUG 0
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
const int dx[] = { 1,0,-1,0,0 };
const int dy[] = { 0,1,0,-1,0 };

int main() {
	ll n, start;	cin >> n >> start;
	vector<ll> x(n);
	vector<ll> p(n);
	rep(i, n)	cin >> x[i] >> p[i];
	vector<ll> pos;
	int s = 0, t = n - 1;
	while (x[s]<start && start<x[t]) {
		if (p[s] < p[t]) {
			pos.push_back(x[s]);
			p[t] += p[s];
			s++;
		}
		else {
			pos.push_back(x[t]);
			p[s] += p[t];
			t--;
		}
	}
	while (s != t) {
		if (x[s] < start)	pos.push_back(x[s++]);
		else	pos.push_back(x[t--]);
	}
	pos.push_back(x[s]);
	ll cur = start, ret = 0;
	rrep(i, pos.size()) {
		ret += labs(pos[i] - cur);
		cur = pos[i];
	}
	cout << ret << endl;
	return 0;
}

// This amazing code is by Eric Sunli Chen.
#include<bits/stdc++.h>
using namespace std;
template<typename T> bool get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar());
	if(t=='-')neg=true,t=getchar();if(t==EOF)return false;
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;return true;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) (get1(a)&&get1(b))
#define get3(a,b,c) (get1(a)&&get2(b,c))
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

const int maxn=100111;
int n,s,x[maxn];
LL p[maxn],ans;

int calc(int l,int r)
{
	if(x[r]<s)
	{
		ans+=s-x[l];
		return l;
	}
	if(x[l]>s)
	{
		ans+=x[r]-s;
		return r;
	}
	if(p[l]>=p[r])
	{
		p[l]+=p[r];
		ans+=x[r]-x[calc(l,r-1)];
		return r;
	}
	else
	{
		p[r]+=p[l];
		ans+=x[calc(l+1,r)]-x[l];
		return l;
	}
}

int main()
{
	get2(n,s);
	for(int i=1;i<=n;i++)get2(x[i],p[i]);
	calc(1,n);
	printendl(ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};

int main() {
  st s;
  cin >> s;
  st t="";
  rep(i,s.size()-8){
    t+=s[i];
  }
  cout << t << endl;
}

#include <stdio.h>
#include <string.h>
int main()
{
int i;
char str[21];
scanf("%s",str);
for(i=strlen(str)-1; i>=0; i--)
printf("%c",str[i]);
printf("\n");
return 0;
}


#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;

typedef pair<string, int> P;

int main() {
	int n;
	while(cin >> n && n){
		int x[20], y[20];
		for(int i = 0; i < n; ++i)
			cin >> x[i] >> y[i];
		int m;
		cin >> m;
		queue<P> q;
		string s;
		int d;
		for(int i = 0; i < m; ++i){
			cin >> s >> d;
			q.push(P(s, d));
		}
		int c = n, nowx = 10, nowy = 10;
		bool ca[20] = {false};
		while(!q.empty()){
			P p = q.front(); q.pop();
			for(int i = 0; i < p.second; ++i){
				string str = p.first;
				if(str == "N") nowy++;
				else if(str == "E") nowx++;
				else if(str == "S") nowy--;
				else if(str == "W") nowx--;

				for(int j = 0; j < n; ++j){
					if(nowx == x[j] && nowy == y[j] && !ca[j]){
						ca[j] = true;
						c--;
					}
				}
				if(c == 0)
					break;

			}
		}
		if(c == 0)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}

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

int main(){
    int a,b,c;
    while(cin>>a>>b>>c,a||b||c){
        int n,parts[500],input[10000][4],bd;
        for(int i=0;i<500;i++) parts[i]=2;
        cin>>n;
        for(int _i=0;_i<n;_i++){
            for(int _j=0;_j<4;_j++){
                cin>>input[_i][_j];
            }
            if(input[_i][3]==1){
                for(int i=0;i<3;i++){
                    parts[input[_i][i]]=1;
                }
            }
            for(int i=0;i<=_i;i++){
                if(input[i][3]==1) continue;
                bd=-1;
                for(int j=0;j<3;j++){
                    if(parts[input[i][j]]==2 && bd==-1) bd=input[i][j];
                    else if(parts[input[i][j]]!=1){
                        bd=-1;
                        break;
                    }
                }
                if(bd!=-1) parts[bd]=0;
            }
        }
        for(int i=1;i<=a+b+c;i++){
            printf("%d\n",parts[i]);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define booga cout << "booga" << endl
#define ll long long int
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,a,b;
	cin >> n >> a >> b;
	if(n == 1){
		cout << "Hello World ";
	}
	else{
		cout << a+b;
	}
	return 0;
}

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

int main()
{
	double ax, ay, bx, by;
	cin >> ax >> ay >> bx >> by;
	printf("%.6f\n",sqrt(pow(bx-ax,2)+pow(by-ay,2)));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long int power(int a,int b)
{
    if(b==0)
        return 1;
    long long int k=power(a,b/2);
    if(b%2==0)
        return ((k%M)*(k%M))%M;
    else
        return ((k%M)*(k%M)*(a%M))%M;
}
long long int fact(long long int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return ((n%M)*(fact(n-1)%M))%M;

}
int div(int n)
{  int c=0;
    for(int i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
          if(n/i==i)
            c++;
          else
            c+=2;
        }

    }

    return  c;
}
int func(int n)
{
    return (n*(n+1))/2;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     #ifndef ONLINE_JUDGE

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

   #endif

   int n,m;
   cin>>n;
   map<string,int> mp;
   while(n--)
   {
      string str;
      cin>>str;
      mp[str]++;
   }
   cin>>m;
   while(m--)
   {
      string str;
      cin>>str;
      mp[str]--;
   }
   map<string,int> :: iterator itr;
   int cmax=0;
   for(itr=mp.begin();itr!=mp.end();itr++)
   {
     if(itr->second > cmax)
     {
        cmax= itr-> second;
     }
   }
   cout<<cmax;

 return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	vector<int> a(N);
	bool ok=true;
	int ans=1e9;
	//O(N * LOG(MAX(a)))
	for(int& x : a) {
		cin >> x;
		if(x&1) ok=false;
		int cnt=0;
		while(x) {
			if(x&1) {
				ans=min(ans,cnt);
				break;
			}
			cnt++;
			x/=2;
		}
	}
	if(!ok) {
		puts("0");return 0;
	}else {
		cout << ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main(){cout<<fixed<<setprecision(10);
		   string s[3];
           rep(i,0,3)cin>>s[i];
           YES(s[0][s[0].size()-1]==s[1][0]&&s[1][s[1].size()-1]==s[2][0]);
          }

#include<bits/stdc++.h>
#include<string.h>

using namespace std;

int main()
{
    char line[50];
    cin.getline(line,50);
    char a,b,c,d;
    int count=0;
    for(int i=0;i<strlen(line);i++)
    {
        if(line[i]==' ')
        {
            if(count==0)
            {
            a=line[i-1];
            b=line[i+1];
            count=1;
            }
            else
            {
              c=line[i-1];
              d=line[i+1];
            }
        }
    }
    if(a==b && c==d)
    {
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
    return 0;
}


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

using namespace std;

void solve()
{
	int N, M;
	while (cin >> N >> M, N || M)
	{
		vector<int> point(N);
		for (int i = 0; i < N; ++i)
		{
			cin >> point[i];
		}
		point.push_back(0);
		vector<int> sum;
		for (int i = 0; i <= N; ++i)
		{
			for (int j = 0; j <= N; ++j)
			{
				sum.push_back(point[i] + point[j]);
			}
		}
		int ans = 0;
		sort(sum.begin(), sum.end());
		for (int i = 0; i < sum.size(); ++i)
		{
			if (M - sum[i] < 0)
			{
				continue;
			}
			int left = 0;
			int right = sum.size();
			while (left < right)
			{
				int middle = (left + right) / 2;
				if (sum[i] + sum[middle] > M)
				{
					right = middle;
				}
				else if (sum[i] + sum[middle] < M)
				{
					left = middle + 1;
				}
				else
				{
					break;
				}
			}
			ans = max(ans, sum[i] + sum[right - 1]);
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

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

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define lc                          id << 1
#define rc                          lc | 1

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}

const ll MAXN = 2e3 + 20;
const ll INF  = 1e18;
const ll MOD  = 1e9 + 7;
ll L[MAXN], R[MAXN], A[MAXN][MAXN], ps[MAXN][MAXN], n, m, ans;
stack<ll> st;

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1; i <= n; i++) {
		string s;
		cin >> s;
		for (ll j = 1; j <= m; j++) {
			A[i][j] = (s[j - 1] == '#');
		}
	}
	for (ll i = 1; i < n; i++) {
		for (ll j = 1; j < m; j++) {
			ll cnt = A[i][j] + A[i + 1][j] + A[i][j + 1] + A[i + 1][j + 1];
			if (cnt & 1) ps[i][j] = 0;
			else ps[i][j] = ps[i - 1][j] + 1;
		}
	}
	ans = max(n, m);
	for (ll i = 1; i < n; i++) {
		while (sz(st)) st.pop();
		st.push(0);
		for (ll j = 1; j < m; j++) {
			while (sz(st) && ps[i][j] <= ps[i][st.top()]) st.pop();
			if (sz(st))
				L[j] = st.top();
			st.push(j);
		}
		while (sz(st)) st.pop();
		st.push(m);
		for (ll j = m - 1; j >= 1; j--) {
			while (sz(st) && ps[i][j] <= ps[i][st.top()]) st.pop();
			if (sz(st)) R[j] = st.top();
			st.push(j);
		}
		for (ll j = 1; j < m; j++) {
			ans = max(ans, (ps[i][j] + 1) * (R[j] - L[j]));
		}
	}
	printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define inputv(v, n)                                                           \
    vl v;                                                                      \
    rep(i, n) {                                                                \
        ll x;                                                                  \
        cin >> x;                                                              \
        v.push_back(x);                                                        \
    }
bool chmin(ll& a, ll  b) { if (b < a) { a = b; return 1; } return 0; }
bool chmax(ll& a, ll b) { if (b > a) { a = b; return 1; } return 0; }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
struct Gragh {
    ll N;
    vector<vl> G;
    vl visited;

    Gragh(ll n) {
        N = n;
        G.resize(N);
        resetv();
    }

    void add(ll a, ll b) { G[a].push_back(b); }

    void resetv(void) { visited = vl(N, 0); }

    //重さ無し
    void dfs(ll x) {
        visited[x] = 1;
        for (ll i : G[x]) {
            if (visited[i] == 0) {
                dfs(i);
            }
        }
    }
};
int main() {
    cin >> n >> m;
    Gragh G(2 * n);
    rep(i, m) {
        cin >> a >> b;
        a--;
        b--;
        G.add(a * 2, b * 2 + 1);
        G.add(b * 2, a * 2 + 1);
        G.add(a * 2+1, b * 2);
        G.add(b * 2+1, a * 2);
    }

    G.dfs(0);
    vl dp(2, 0);
    rep(i, n * 2) {
        if (G.visited[i] == 1)dp[i % 2]++;
    }

    x=dp[0];
    y=dp[1];
    k=x+y-n;
    ans=k*(n-k)+k*(k-1)/2+(x-k)*(y-k)-m;

    cout << ans << endl;
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

void solve(int n,int m){
    int fr[501][501],a,b,ans=0;

    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            fr[i][j]=100;

    for(int i=0;i<m;i++){
        scanf("%d%d",&a,&b);
        fr[a][b]=1;
        fr[b][a]=1;
    }

    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                fr[i][j]=min(fr[i][j],fr[i][k]+fr[k][j]);

    for(int i=2;i<=n;i++)
        ans+=(fr[1][i]==2 || fr[1][i]==1);

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

    return;
}

int main(){
    int n,m;

    while(1){
        scanf("%d%d",&n,&m);
        if(n+m==0) break;
        solve(n,m);
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main(){
	int n;
	cin>>n;
	vi d(n);
	int f,l,sf,sl;
	rep(i,0,n*(n-1)/2){
		cin>>f>>l>>sf>>sl;
		f--;
		l--;
		if(sf<sl){
			d[l]+=3;
		}else if(sf>sl){
			d[f]+=3;
		}else{
			d[l]++;
			d[f]++;
		}
	}
	vi e=d;
	sort(all(e));
	rep(i,0,n){
		int res=upper_bound(all(e),d[i])-e.begin();
		o(n+1-res);
	}
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 1e9+7;;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
    int N;
    cin >> N;
    vector
ab(N);
    vector
cd(N);
    for(int i = 0; i < N; i++) {
        int a,b;
        cin >> a >> b;
        ab[i].first = a;
        ab[i].second = b;
    }
    for(int i = 0; i < N; i++) {
        int c,d;
        cin >> c >> d;
        cd[i].first = c;
        cd[i].second = d;
    }
    vector<bool>ok(N,false);
    sort(ab.begin(),ab.end());
    reverse(ab.begin(),ab.end());
    int ans = 0;
    for(int i = 0; i < N; i++) {
        int X = INF;
        int Y = INF;
        for(int j = 0; j < N; j++) {
            if(ab[i].first < cd[j].first && ab[i].second < cd[j].second) {
                if((!ok[j]) && X > cd[j].second) {
                    X = cd[j].second;
                    Y = j;
                }

            }
        }
        if(Y != INF) {
            ok[Y] = true;
        }
    }
    for(int i = 0; i < N; i++) {
        if(ok[i]) {
            ans++;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <vector>
typedef long long ll;
const int maxn = 1e5 + 5;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
ll a[maxn];

int main (int argc, char **argv) {
  int n;
  std::cin >> n;
  int cnt = 0;
  for (int i=1; i <= n; i++) {
    std::cin >> a[i];
    if (a[i] < 0) {
      cnt++;
    }
  }

  ll ans = 0;
  if (cnt&1) {
    ll min_val = a[1] > 0 ? a[1]:-a[1];
    for (int i=2; i <= n; i++) {
      min_val = std::min(min_val, a[i] > 0 ? a[i]:-a[i]);
    }
    ans -= 2*min_val;
  }

  for (int i=1; i <= n; i++) {
    if (a[i] < 0) {
      ans -= a[i];
    } else {
      ans += a[i];
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
    ll N;
    cin >> N;
    vl A(N);
    rep(i, N) cin >> A[i];

    vvc<ll> dp(N + 1, vl(2, -INF));
    dp[0][0] = 0;

    rep(i, N){
        dp[i + 1][0] = max(dp[i][0] + A[i], dp[i][1] - A[i]);
        dp[i + 1][1] = max(dp[i][0] - A[i], dp[i][1] + A[i]);
    }

    cout << dp[N][0] << endl;
}


#include<iostream>
#include<string>

signed main() {
  int h,w,a,b;
    scanf("%d%d%d%d",&h,&w,&a,&b);
    	std::string s(a,'0');
    	std::string t(w-a,'1');
        std::string ans=s+t;
  		std::string S(a,'1');
  		std::string T(w-a,'0');
  		std::string Ans=S+T;
  for(int i=0;i<h;++i){
    if(i<b){
    	printf("%s\n",ans.c_str());
    }
    else{
    	printf("%s\n",Ans.c_str());
    }
  }
}

#include <bits/stdc++.h>
#define pb push_back
#define pll pair <ll, ll>
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair <ll, pair <ll, ll>>
#define pllll pair <pair <ll, ll>, pair <ll, ll> >
#define psl pair <string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector <vector <ll> >

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 1e3 + 10;

ll n, m, k, t;

ll x, y;

ll ANS[maxn][maxn];

int main() {
    pyshnapyshnakaa;
    ll q, w, e, a, b, c;
    cin >> n >> m >> x >> y;
    for (q = 0; q < n; q++) {
        for (w = 0; w < x; w++) {
            ANS[q][w] = 0;
        }
        for (w = x; w < m; w++) {
            ANS[q][w] = 1;
        }
    }
    for (q = 0; q < y; q++) {
        for (w = 0; w < m; w++) {
            ANS[q][w] ^= 1;
        }
    }
    for (q = 0; q < n; q++) {
        for (w = 0; w < m; w++) {
            cout << ANS[q][w];
        }
        cout << endl;
    }
    return 0;
}


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

using namespace std;

int gcd(int x, int y){
  return y ? gcd(y, x % y) : x;
}

void divisor(int n, vector<int>& vec){
  for(int i=1;i*i<=n;i++){
    if(n % i == 0){
      vec.push_back(i);
      if(i != n / i) vec.push_back(n / i);
    }
  }
}

main(){
  int n, in[3];
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> in[i];
  }
  vector<int> ans;
  if(n == 2) divisor(gcd(in[0], in[1]), ans);
  else divisor(gcd(in[0], gcd(in[1], in[2])), ans);
  sort(ans.begin(), ans.end());
  for(int i=0;i<ans.size();i++) cout << ans[i] << endl;
}

#include<iostream>
#include<map>
using namespace std;

int main()
{
  int n;
  cin >> n;
  map<int,int> ans;
  for(int i=0;i<n;i++)
    {
      int num;
      cin >> num;
      for(int j=1;j<=num/2;j++)
	{
	  if(num%j == 0)
	    ans[j]++;
	}
      ans[num]++;
    }

  for(map<int,int>::iterator it = ans.begin();it != ans.end();it++)
    if((*it).second >= n)
      cout << (*it).first << endl;

  return 0;
}


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

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll n, q;
  string s;
  cin >> n >> s >> q;
  vll vq(q);
  rep(i, q) cin >> vq[i];

  vll nc(n + 2);

  rep(i, n) {
    if (s[i] == 'C')
      nc[i]++;
  }

  REP(i, n) {
    nc[i] += nc[i - 1];
  }

  for (const auto k : vq) {
    ll ans = 0;

    vll imos(n + 2);
    vll dc(n + 2);

    rep(i, n) {
      if (s[i] == 'D') {
        dc[i] = nc[min(i + k - 1, n - 1)] - nc[i];
        imos[i]++;
        if (i + k < n) {
          imos[i + k]--;
        }
      }
    }

    REP(i, n) {
      imos[i] += imos[i - 1];
    }

    REP(i, n) {
      dc[i] += dc[i - 1];
      if (s[i] == 'C') {
        dc[i] -= imos[i];
      } else if (s[i] == 'M') {
        ans += dc[i];
      }
    }

    cout << ans << endl;
  }

  return 0;
}

#include<bits/stdc++.h>

int main(){
    using namespace std;
    constexpr unsigned long MOD = 998244353;

    string S;
    cin >> S;
    reverse(begin(S), end(S));
    unsigned long K;
    cin >> K;
    K = min<unsigned long>(count(begin(S), end(S), '1'), K);

    vector<vector<unsigned long>> dp(K + 1), tmp(K + 1);
    for(unsigned long i{0}; i <= K; ++i)dp[i] = vector(i + 1, 0UL);
    for(unsigned long i{0}; i <= K; ++i)tmp[i] = vector(i + 1, 0UL);
    dp[0][0] = 1;

    unsigned long cnt{0};
    for(const auto& c : S)if(c == '1')++cnt;
    else{
        for(auto&& i : tmp)fill(begin(i), end(i), 0UL);
        for(unsigned long i{0}; i < K; ++i)for(unsigned long j{0}; j + i < K; ++j){
            tmp[j + i + 1][i] = tmp[j + i][i] + dp[j + i][i];
            if(j >= cnt)tmp[j + i + 1][i] -= dp[j - cnt + i][i];
        }
        for(auto&& i : dp)partial_sum(begin(i), end(i), begin(i));
        for(unsigned long i{0}; i <= K; ++i)for(unsigned long j{0}; j <= i; ++j)(dp[i][j] += tmp[i][j]) %= MOD;
        cnt = 0;
    }
    for(auto&& i : dp)partial_sum(begin(i), end(i), begin(i));

    unsigned long ans{0};
    for(const auto& i : dp)ans += i.back();
    cout << ans % MOD << endl;
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7, maxN = 1e5 + 13;
int n, x, dp[maxN], dpw[maxN];
vector<int> vec;
int pw (int a, int b) {
    int ret = 1;
    for (; b; b >>= 1, a = 1ll * a * a % mod)
	if (b & 1)
	    ret = 1ll * ret * a % mod;
    return ret;
}

int32_t main () {
    cin >> n >> x;
    int cur = 1;
    for (int i = 0; i < n; i++) {
	int a;
	cin >> a;
	if (a <= x)
	    vec.push_back(a);
	cur = (cur * (i + 1)) % mod;
    }
    sort(vec.begin(), vec.end());

    for (int i = 0; i <= n; i++)
	dpw[i] = pw(i, mod - 2);

    for (int i = 0; i <= x; i++) {
	int pos = upper_bound(vec.begin(), vec.end(), i) - vec.begin();
	if (pos == 0)
	    dp[i] = i;
	for (int j = 0; j < pos; j++) {
	    //cout << i << " " << vec[j] << " " << dp[i % vec[j]] << endl;
	    dp[i] = (dp[i] + dpw[pos] * dp[i % vec[j]]) % mod;
	}

	//cout << i << " " << pos << " " << dp[i] << endl;
    }

    //cout << solve(x, n) << " " << cur  << endl;
    cout << 1ll * dp[x] * cur % mod << endl;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y)*y;
}
ll kai(ll x, ll y, ll m) {
	ll res = 1;
	for (ll i = x - y + 1; i <= x; i++) {
		res *= i; res %= m;
	}
	return res;
}
ll mod_pow(ll x, ll y, ll m) {
	ll res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

ll comb(ll x, ll y, ll m) {
	if (y > x)return 0;
	return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

const ll mod = 1000000007;

int n, x;
vector<int> vec[1050000];
ll d[500010];
ll ans;

signed main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int a; cin >> a;
		x ^= a;
		vec[x].push_back(i);
	}
	if (x) {
		ll cnt1 = 0, cnt2 = 1;
		for (int j = 0; j < (int)vec[x].size(); j++) {
			int p = 0;
			if (j)p = vec[x][j - 1];
			int t = lower_bound(all(vec[0]), vec[x][j]) - lower_bound(all(vec[0]), p);
			cnt2 = (cnt2 + cnt1 * t) % mod;
			cnt1 = (cnt1 + cnt2) % mod;
		}
		cout << cnt2 << endl;
		return 0;
	}
	for (int i = 1; i < (1 << 20); i++) {
		if ((int)vec[i].size() == 0)continue;
		ll cnt1 = 0, cnt2 = 1;
		for (int j = 0; j < (int)vec[i].size(); j++) {
			int p = 0;
			if (j)p = vec[i][j - 1];
			int t = lower_bound(all(vec[0]), vec[i][j]) - lower_bound(all(vec[0]), p);
			cnt2 = (cnt2 + cnt1 * t) % mod;
			cnt1 = (cnt1 + cnt2) % mod;
		}
		ans = (ans + cnt1) % mod;
	}
	ll cnt = 1;
	for (int i = 1; i < (int)vec[0].size(); i++)cnt = cnt * 2 % mod;
	cout << (ans + cnt) % mod << endl;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

	int k;
	cin >> k;
	vector<vector<int>> chess(8, vector<int>(8));
	auto ok = [&](const vector<vector<int>> &newChess) {
		REP(i, 8) {
			int cnt = 0;
			REP(j, 8) {
				cnt += newChess[i][j];
				if (cnt > 1) {
					return false;
				}
			}
		}
		REP(j, 8) {
			int cnt = 0;
			REP(i, 8) {
				cnt += newChess[i][j];
				if (cnt > 1) {
					return false;
				}
			}
		}
		REP(i, 8) {
			int cnt = 0;
			REP(j, 8) {
				if (i + j == 8) break;
				cnt += newChess[i + j][j];
				if (cnt > 1) {
					return false;
				}
			}
		}
		REP(i, 8) {
			int cnt = 0;
			REP(j, 8) {
				if (i - j < 0) break;
				cnt += newChess[i - j][j];
				if (cnt > 1) {
					return false;
				}
			}
		}
		REP(j, 8) {
			int cnt = 0;
			REP(i, 8) {
				if (j + i == 8) break;
				cnt += newChess[i][j + i];
				if (cnt > 1) {
					return false;
				}
			}
		}
		REP(j, 8) {
			int cnt = 0;
			REP(i, 8) {
				if (j + i == 8) break;
				cnt += newChess[7 - i][j + i];
				if (cnt > 1) {
					return false;
				}
			}
		}
		return true;
	};
	REP(i, k) {
		int r, c;
		cin >> r >> c;
		chess[r][c] = 1;
	}
	vector<int> v(8);
	REP(i, 8) { v[i] = i; }

	do {
		vector<vector<int>> newChess = chess;
		REP(i, 8) {
			newChess[i][v[i]] = 1;
		}
		if (ok(newChess)) {
			REP(i, 8) {
				REP(j, 8) {
					if (newChess[i][j]) {
						cout << 'Q';
					} else {
						cout << '.';
					}
				}
				cout << endl;
			}
		}
	} while (next_permutation(v.begin(), v.end()));

	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

const char Q = 'Q';
const char _ = '.';

char board[8][9] = {
    "........",
    "........",
    "........",
    "........",
    "........",
    "........",
    "........",
    "........",
};

int n;
bool pre_x[8], pre_y[8];

inline bool check(int x, int y) {
    int p = x, q = y;
    while (--p >= 0 && --q >= 0) {  // upper left
        if (board[q][p] == Q) return false;
    }

    p = x; q = y;
    while (++p < 8 && --q >= 0) {   // upper right
        if (board[q][p] == Q) return false;
    }

    p = x; q = y;
    while (++p < 8 && ++q < 8) {    // lower right
        if (board[q][p] == Q) return false;
    }

    p = x; q = y;
    while (--p >= 0 && ++q < 8) {   // lower left
        if (board[q][p] == Q) return false;
    }

    return true;
}

bool solve(int row) {
    if (row == 8)
        return true;
    if (pre_y[row] && solve(row + 1)) {
        return true;
    }
    else {
        for (int i = 0; i < 8; ++i) {
            if (!pre_x[i] && check(i, row)) {
                board[row][i] = Q;
                pre_x[i] = true;
                if (solve(row + 1)) {
                    return true;
                }
                else {
                    board[row][i] = _;
                    pre_x[i] = false;
                }
            }
        }
    }
    return false;
}

int main(void) {

    scanf("%d", &n);

    for (int i = 0, x, y; i < n; ++i) {
        scanf("%d %d", &y, &x);
        board[y][x] = Q;
        pre_y[y] = pre_x[x] = true;
    }

    solve(0);

    for (int i = 0; i < 8; ++i) {
        printf("%s\n", board[i]);
    }

    return 0;
}