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

char ch[330][330];

int main()
{
    freopen("ain.txt","r",stdin);
    int i,k,j,n,m;
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
            scanf("%s",ch[i]);

        char na=ch[0][0];
        char nb=ch[1][0];
        int ok=0;
        if(na != ch[1][1])
        {
            ok=1;
        }
        if(nb != ch[0][1])
        {
            ok=1;
        }
        if(na==nb)
        {
            ok=1;
        }

        for(i=0;i<n;i++)
        {
            if(ok)
                break;
            for(j=0;j<n;j++)
            {
                if(i == j || (i+j) == (n-1))
                {
                    if(ch[i][j] != na)
                    {
                        ok=1;
                        break;
                    }
                }
                else
                {
                    if(ch[i][j] != nb)
                    {
                        ok=1;
                        break;
                    }
                }
            }
            if(ok == 1)
                break;
        }

        if(ok==1)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}


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

char ch[330][330];

int main()
{
    freopen("ain.txt","r",stdin);
    int i,k,j,n,m;
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
            gets(ch[i]);
        char na=ch[0][0];
        char nb=ch[0][1];
        if(na==nb)
        {
            puts("NO");
            continue;
        }
        int ok=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i == j || (i+j) == (n-1))
                {
                    if(ch[i][j] != na)
                    {
                        ok=1;
                        break;
                    }
                }
                else
                {
                    if(ch[i][j] != nb)
                    {
                        ok=1;
                        break;
                    }
                }
            }
            if(ok == 1)
                break;
        }

        if(ok==1)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}


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

char ch[330][330];
char ch1[90001];
char ch2[90001];

int main()
{
    freopen("ain.txt","r",stdin);
    int i,k,j,n,m;
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
            scanf("%s",ch[i]);

        char na=ch[0][0];
        char nb=ch[1][0];
        int ok=0;
        int taa1=0;
        int taa2=0;

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

            for(j=0;j<n;j++)
            {
                if(i == j || i+j == (n-1))
                    ch1[taa1++]=ch[i][j];
                else
                    ch2[taa2++]=ch[i][j];

            }

        }

        for(i=1;i<taa1;i++)
        {
            if(ch1[i-1]!=ch1[i])
            {
                ok=1;
                break;
            }
        }

        for(i=1;i<taa2;i++)
        {
            if(ch2[i-1]!=ch2[i])
            {
                ok=1;
                break;
            }
        }

        if(ok==1)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}


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

char ch[330][330];

int main()
{
    freopen("ain.txt","r",stdin);
    int i,k,j,n,m;
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
            scanf("%s",ch[i]);

        char na=ch[0][0];
        char nb=ch[1][0];
        int ok=0;
        if(na != ch[1][1])
        {
            ok=1;
        }
        if(nb != ch[0][1])
        {
            ok=1;
        }
        if(na==nb)
        {
            ok=1;
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i == j || (i+j) == (n-1))
                {
                    if(ch[i][j] != na)
                    {
                        ok=1;
                        break;
                    }
                }
                else
                {
                    if(ch[i][j] != nb)
                    {
                        ok=1;
                        break;
                    }
                }
            }
            if(ok == 1)
                break;
        }

        if(ok==1)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}


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

char ch[330][330];
char ch1[90001];
char ch2[90001];

int main()
{

    int i,k,j,n,m;
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
            scanf("%s",ch[i]);

        char na=ch[0][0];
        char nb=ch[1][0];
        int ok=0;
        int taa1=0;
        int taa2=0;
        if(na==nb)
            ok==1;

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

            for(j=0;j<n;j++)
            {
                if(i == j || i+j == (n-1))
                    ch1[taa1++]=ch[i][j];
                else
                    ch2[taa2++]=ch[i][j];

            }

        }

        for(i=1;i<taa1;i++)
        {
            if(ch1[i-1]!=ch1[i])
            {
                ok=1;
                break;
            }
        }

        for(i=1;i<taa2;i++)
        {
            if(ch2[i-1]!=ch2[i])
            {
                ok=1;
                break;
            }
        }

        if(ok==1)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}


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

char ch[330][330];

int main()
{
    freopen("ain.txt","r",stdin);
    int i,k,j,n,m;
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=0;i<n;i++)
            scanf("%s",ch[i]);
        char na=ch[0][0];
        char nb=ch[1][0];
        if(na != ch[1][1])
        {
            puts("NO");
            continue;
        }
        if(nb != ch[0][1])
        {
            puts("NO");
            continue;
        }
        if(na==nb)
        {
            puts("NO");
            continue;
        }
        int ok=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i == j || (i+j) == (n-1))
                {
                    if(ch[i][j] != na)
                    {
                        ok=1;
                        break;
                    }
                }
                else
                {
                    if(ch[i][j] != nb)
                    {
                        ok=1;
                        break;
                    }
                }
            }
            if(ok == 1)
                break;
        }

        if(ok==1)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,af[26]={0};
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>s[i][j];
			af[s[i][j]]++;
		}
	}
	/*char s1 = s[0][0];
	char s2 = s[0][1];
	int a = af[s1];
	int b = af[s2];*/
	int mx = max(af[s[0][0]],af[s[0][1]]);
	int mn = min(af[s[0][0]],af[s[0][1]]);
	if(mx-1 == mn)
		printf("YES");
	else
		printf("NO");
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,af[128]={0};
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>s[i][j];
			af[s[i][j]]++;
		}
	}
	/*char s1 = s[0][0];
	char s2 = s[0][1];
	int a = af[s1];
	int b = af[s2];*/
	int mx = max(af[s[0][0]],af[s[0][1]]);
	int mn = min(af[s[0][0]],af[s[0][1]]);
	if(mx-1 == mn*2 || mx-1 == mn)
		printf("YES");
	else
		printf("NO");
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,af[128]={0};
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>s[i][j];
			af[s[i][j]]++;
		}
	}
	/*char s1 = s[0][0];
	char s2 = s[0][1];
	int a = af[s1];
	int b = af[s2];*/
	int mx = max(af[s[0][0]],af[s[0][1]]);
	int mn = min(af[s[0][0]],af[s[0][1]]);
	if(mx-1 == pow(mn,2) || mx-1 == mn)
		printf("YES");
	else
		printf("NO");
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,af[128]={0};
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>s[i][j];
			af[s[i][j]]++;
		}
	}
	char s1 = s[0][0];
	char s2 = s[0][1];
	int a = af[s1];
	int b = af[s2];
	//cout<<a<<b;
	int mx = max(af[s[0][0]],af[s[0][1]]);
	int mn = min(af[s[0][0]],af[s[0][1]]);
//	cout<<(mn-1)*2<<mx;
	if(mx == (mn-1)*2 || mx-1 == mn)
		printf("YES");
	else
		printf("NO");
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	bool flag = false;
	int n;
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>s[i][j];
			char s1 = s[0][0];
			char s2 = s[0][1];
			if(i==j)
                {
                    if(s1!=s[i][j])
                    {
                        flag=true;
                    }
                }
                else if(i==n-1-j)
                {
                  if(s1!=s[i][j])
                  {
                      flag=true;
                  }

                }
                else
                {
                   if(s2!=s[i][j]||s1==s[i][j])
                   {
                       flag=true;
                   }
                }

        }
    }
    if(flag)
    	printf("NO");
    else
    	printf("YES");
}
/*
5
liiil
ilili
iilii
ilili
liiil

*/


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,af[128]={0};
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,af[128]={0};
	scanf("%d",&n);
	char s[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>s[i][j];
			af[s[i][j]]++;
		}
	}
	/*char s1 = s[0][0];
	char s2 = s[0][1];
	int a = af[s1];
	int b = af[s2];
	cout<<s[0][0];
	cout<<s[0][1];*/
	int mx = max(af[s[0][0]],af[s[0][1]]);
	int mn = min(af[s[0][0]],af[s[0][1]]);
	//cout<<mx<<mn;
	if(mx-1 == pow(mn,2) || mx-1 == mn)
		printf("YES");
	else
		printf("NO");
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

int main()
{
      int n,i,c=0,d=0;
      cin >> n;
      char ar[n*n];

      for(i=0;i<n*n;i++)
      cin >> ar[i];

      for(i=0;i<n*n;i++)
      {
            if(ar[i]==ar[0])
            c++;
            if(ar[i]==ar[1])
            d++;
      }

      cout << ((c==(2*n)-1) && (d==(n*n)-c)?"YES":"NO") << endl;

      return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

int main()
{
      int n,i,c=0,d=0;
      cin >> n;
      char ar[n*n];

      for(i=0;i<n*n;i++)
      cin >> ar[i];

      for(i=0;i<n*n;i++)
      {
            if(ar[i]==ar[0])
            c++;
            if(ar[i]==ar[1])
            d++;
      }

      cout << ((c==2*n) && (d==n*n-2*n)?"YES":"NO") << endl;

      return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

int main()
{
      int n,i,c=0,d=0;
      cin >> n;
      char ar[n*n];

      for(i=0;i<n*n;i++)
      cin >> ar[i];

      for(i=0;i<n*n;i++)
      {
            if(ar[i]==ar[0])
            c++;
            if(ar[i]==ar[1])
            d++;
      }

      cout << ((c==(2*n)-1) && d==(n*n)-(2*n)+1?"YES":"NO") << endl;

      return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

int main()
{
      int n,i,c=0,f;
      cin >> n;
      char ch;

      for(i=0;i<n*n;i++)
      {
            cin >> ch;
            if(i==0)
            f = int(ch);
            c = c^ch;
      }

      cout << (c==f?"YES":"NO") << endl;

      return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

int main()
{
      int n,i,e=0,d=0;
      cin >> n;
      char ar[n*n];

      for(i=0;i<n*n;i++)
      cin >> ar[i];

      for(i=0;i<n*n;i++)
      {
            if(ar[i]==ar[0])
            e++;
            if(ar[i]==ar[1])
            d++;
      }

      cout << ((e==2*n) && (d==n*n-2*n)?"YES":"NO") << endl;

      return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int n;
  cin>>n;
  char mat[n+1][n+1]={'a'};
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  long long int count=0,dount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c)count++;
          else if(mat[i][j]==d) dount++;
      }
  }
  if(count!=2*n-1)cout<<"NO"<<endl;
  else if(count==2*n-1){
      if(dount<n*n-count)cout<<"NO"<<endl;
      else if(dount==n*n-count) cout<<"YES"<<endl;
  }
 // if(count==n*n)cout<<"NO"<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int n;
  cin>>n;
  char mat[n+1][n+1]={'a'};
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  long long int count=0,dount=0,eount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c && i==j)count++;
           if(mat[i][j]==c&& i==n-j) dount++;
           if(i!=j && i!=n-j && (mat[i][j]!=d||mat[i][j]==c))eount++;
      }
  }
  if(count==n&&dount==n && eount==0)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int n;
  cin>>n;
  char mat[n+1][n+1]={'a'};
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  long long int count=0,dount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c)count++;
          else if(mat[i][j]==d) dount++;
      }
  }
  if(count<2*n-1)cout<<"NO"<<endl;
  else if(count==2*n-1){
      if(dount<n*n-count)cout<<"NO"<<endl;
      else if(dount==n*n-count) cout<<"YES"<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int n;
  cin>>n;
  char mat[n+1][n+1]={a};
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  long long int count=0,dount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c)count++;
          else if(mat[i][j]==d) dount++;
      }
  }
  if(count<2*n-1)cout<<"NO"<<endl;
  else if(count==2*n-1){
      if(dount<n*n-count)cout<<"NO"<<endl;
      else if(dount==n*n-count) cout<<"YES"<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int n;
  cin>>n;
  char mat[n+1][n+1]={'a'};
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  long long int count=0,dount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c)count++;
          else if(mat[i][j]==d) dount++;
      }
  }
  if(count<2*n-1)cout<<"NO"<<endl;
  else if(count==2*n-1){
      if(dount<n*n-count)cout<<"NO"<<endl;
      else if(dount==n*n-count) cout<<"YES"<<endl;
  }
  if(count==n*n)cout<<"NO"<<endl;
}


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

main()
{
	int n;
	cin>>n;
	char x;
	set<char> s1,s2;
	for(int i=0;i<n;i++)
	for(int j=0;j<n;j++ )
	{
	cin>>x;
	if(i==j||j==n-1-i)
	s1.insert(x);
	else s2.insert(x);}
	if(s1!=s2&&s1.size()==1&&s2.size()==1)
	cout<<"YES";
	else cout<<"NO";

}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cin>>n;
  char mat[n][n];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  int count=0,dount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c)count++;
          else if(mat[i][j]==d) dount++;
      }
  }
  if(count<2*n-1)cout<<"NO"<<endl;
  else if(count==2*n-1){
      if(dount<n*n-count)cout<<"NO"<<endl;
      else if(dount==n*n-count) cout<<"YES"<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int n;
  cin>>n;
  char mat[n][n];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          cin>>mat[i][j];

      }
  }
  long long int count=0,dount=0;
  char c=mat[1][1],d=mat[1][2];
  for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
          if(mat[i][j]==c)count++;
          else if(mat[i][j]==d) dount++;
      }
  }
  if(count<2*n-1)cout<<"NO"<<endl;
  else if(count==2*n-1){
      if(dount<n*n-count)cout<<"NO"<<endl;
      else if(dount==n*n-count) cout<<"YES"<<endl;
  }
}


#include <iostream>
using namespace std;
int z[250];
int main() {
	string h;
	int a ;
	cin >> a ;
	for(int i=0;i<a;i++){
		cin>>h;
		if(i==0){
			for(int j=0;j<a;j++){
				z[h[j]]++;
			}
		}
		else{
			for(int j=0;j<a;j++){
				if(z[h[j]]==0){
					cout<<"NO";
					return 0;
				}
			}
		}
	}
	if(z[h[0]]==a){
		cout<<"NO";
		return 0;
	}
	cout<<"YES ";
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <pair<char,int>> x;
int z[250];
string h[400];
int main() {
	int a,m=0;

	cin>>a;
	for(int i=0;i<a;i++){
		cin>>h[i];
		for(int j=0;j<i;j++){
			if(h[i]==h[j]){
				cout<<"NO";
				return 0;
			}
		}
		if(i==0){
			for(int j=0;j<a;j++){
				z[h[i][j]]++;
				if(z[h[i][j]]==1){
					x.push_back(make_pair(h[i][j],0));
				}
			}
			continue;
		}
		else{
			for(int j=0;j<a;j++){
				if(z[h[i][j]]==0){
					cout<<"NO";
					return 0;
				}
				for(int k=0;k<x.size();k++){
					if(h[i][j]==x[k].first){
						x[k].second++;
					}
					else m = j;
				}
			}
		}
		for(int j=0;j<x.size();j++){
			if(x[j].second==0){
				cout<<"NO ";
				return 0;
			}
			x[j].second=0;
		}

	}
	if(x.size()==1){
		cout<<"NO";
		return 0;
	}

	cout<<"YES";
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <pair<char,int>> x;
int z[250];
string h[400];
int main() {
	int a,m=0;

	cin>>a;
	for(int i=0;i<a;i++){
		cin>>h[i];
		for(int j=0;j<i;j++){
			if(h[i]==h[j]){

				m++;
			}
		}
		if(i==0){
			for(int j=0;j<a;j++){
				z[h[i][j]]++;
				if(z[h[i][j]]==1){
					x.push_back(make_pair(h[i][j],0));
				}
			}
			continue;
		}
		else{
			for(int j=0;j<a;j++){
				if(z[h[i][j]]==0){
					cout<<"NO";
					return 0;
				}
				for(int k=0;k<x.size();k++){
					if(h[i][j]==x[k].first){
						x[k].second++;
					}

				}
			}
		}
		for(int j=0;j<x.size();j++){
			if(x[j].second==0){
				cout<<"NO ";
				return 0;
			}
			x[j].second=0;
		}

	}
	if(x.size()==1||(m+1)==a){
		cout<<"NO";
		return 0;
	}

	cout<<"YES";
	return 0;
}


#include <iostream>
using namespace std;
int z[250];
int main() {
	string h;
	int a ;
	cin >> a ;
	for(int i=0;i<a;i++){
		cin>>h;
		if(i==0){
			for(int j=0;j<a;j++){
				z[h[j]]++;
			}
		}
		else{
			for(int j=0;j<a;j++){
				if(z[h[j]]==0){
					cout<<"NO";
					return 0;
				}
			}
		}
	}
	if(z[h[0]]==(a*a)){
		cout<<"NO";
		return 0;
	}
	cout<<"YES";
	return 0;
}


#include <iostream>
using namespace std;
int z[250];
int main() {
	string h;
	int a ;
	cin >> a ;
	for(int i=0;i<a;i++){
		cin>>h;
		if(i==0){
			for(int j=0;j<a;j++){
				z[h[j]]++;
			}
		}
		else{
			for(int j=0;j<a;j++){
				if(z[h[j]]==0){
					cout<<"NO";
					return 0;
				}
			}
		}
	}
	cout<<"YES";
	return 0;
}


#include <iostream>
using namespace std;
char g,o;
int main() {
	string h;
	int a , c = 0, l = 0;
	cin >> a ;
	l = a - 1 ;
	for (int i=0;i<a;i++) {
		cin>>h;
		if ( i == 0 ) {
			 g = h[0];
			 o = h[1];
		}
		for ( int j = 0 ; j < a ; j++ ) {
			if ( j == l || j == c ) {
				continue ;
			}
			if ( h[j] != o ){
				cout << "NO" ;
				return 0 ;
			}
		}
		if (h[c] != g || h[l] != g ) {
			cout << "NO" ;
			return 0 ;
		}
		l-- ;
		c++ ;
	}
	if ( o == g) {
		cout << "NO" ;
		return 0 ;
	}
	cout << "YES" ;
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <pair<char,int>> x;
int z[250];
int main() {
	int a,m=0;
	string h;
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>h;

		if(i==0){
			for(int j=0;j<a;j++){
				z[h[j]]++;
				if(z[h[j]]==1){
					x.push_back(make_pair(h[j],0));
				}
			}
			continue;
		}
		else{
			for(int j=0;j<a;j++){
				if(z[h[j]]==0){
					cout<<"NO";
					return 0;
				}
				for(int k=0;k<x.size();k++){
					if(h[j]==x[k].first){
						x[k].second++;
					}
					else m = j;
				}
			}
		}
		for(int j=0;j<x.size();j++){
			if(x[j].second==0){
				cout<<"NO ";
				return 0;
			}
			x[j].second=0;
		}
	}
	if(x.size()==1){
		cout<<"NO";
		return 0;
	}

	cout<<"YES";
	return 0;
}


#include <iostream>
using namespace std;
int z[250];
int main() {
	string h;
	int a ;
	cin >> a ;
	for(int i=0;i<a;i++){
		cin>>h;
		if(i==0){
			for(int j=0;j<a;j++){
				z[h[j]]++;
			}
		}
		else{
			for(int j=0;j<a;


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,k,b[26]={0},m=0;
    scanf("%d",&n);
    char a[n+1][n+1];
    for(i=0;i<n;i++)
        scanf("%s",a[i]);
   for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            b[a[i][j]-'a']++;
        }
    for(i=0;i<26;i++)
        if(b[i]!=0)
            m++;
    if(m!=2)
        return 0*printf("NO");
    for(i=0;i<n;i++)
    for(j=i,k=n-i-1;j<n&&k>=0;j++,k--){
        if(a[i][j]!=a[i][k])
            return 0*printf("NO");
        else
           break;
    }
    printf("YES");
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,k,b[26]={0},m=0;
    scanf("%d",&n);
    char a[n+1][n+1];
    for(i=0;i<n;i++)
        scanf("%s",a[i]);
   for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            b[a[i][j]-'a']++;
        }
    for(i=0;i<26;i++)
        if(b[i]!=0)
            m++;
    if(m!=2)
        return 0*printf("NO");
    for(i=0;i<n;i++)
    for(j=i,k=n-i;j<n&&k>=0;j++,k--){
        if(a[i][j]!=a[i][k])
            return 0*printf("NO");
        else
            break;
    }
    printf("YES");
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    bool three=false;
    char arr[n][n],x=NULL,y=NULL;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>arr[i][j];
            if(j==1){
                 x=arr[0][0],y=arr[0][1];
            }
            if(j>=2)
            if(arr[i][j]!=x&&arr[i][j]!=y){
            three=true;
            break;
        }

        }
    }
    int j=0,p=2;
    for(int i=0;i<n;i++)
    {
        if(arr[i][j]!=x)
            three=true;
        j++;
        if(arr[i][p]!=x)
            three=true;
        p--;
    }

    if(three)cout<<"NO";
    else cout<<"YES";
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    bool three=false;
    char arr[n][n];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        cin>>arr[i][j];
    char x=arr[0][0],y=arr[0][1];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        if(arr[i][j]!=x&&arr[i][j]!=y)
            three=true;
    int l=0,r=n-1;
    for(int i=0;i<n;i++){

        if(arr[i][l]!=x||arr[i][r]!=x)
        three=true;
    r--,l++;
    }
    if(x==y)three=true;
    if(three)cout<<"NO";
    else cout<<"YES";

    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    bool three=false;
    char arr[n][n],x=NULL,y=NULL;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>arr[i][j];
            if(j==1){
                 x=arr[0][0],y=arr[0][1];
            }
            if(j>=2)
            if(arr[i][j]!=x&&arr[i][j]!=y){
            three=true;
            break;
        }

        }
    }
    int j=0,p=n-1;
    for(int i=0;i<n;i++)
    {
        if(arr[i][j]!=x)
            three=true;
        j++;
        if(arr[i][p]!=x)
            three=true;
        p--;
    }

    if(three)cout<<"NO";
    else cout<<"YES";
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    char val;
    set<char>s,s1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
            cin>>val;
            if(i==j||j+i==n-1){
                s.insert(val);
            }else{
            s1.insert(val);
            }
        }
    }
    if(s.size()==1&&s1.size()==1&&s1!=s)cout<<"YES";
    else cout<<"NO";

    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    bool three=false;
    char arr[n][n];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        cin>>arr[i][j];
    char x=arr[0][0],y=arr[0][1];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        if(arr[i][j]!=x&&arr[i][j]!=y)
            three=true;
    int l=0,r=n-1;
    for(int i=0;i<n;i++){

        if(arr[i][l]!=x||arr[i][r]!=x)
        three=true;
    n--,l++;
    }
    if(x==y)three=true;
    if(three)cout<<"NO";
    else cout<<"YES";

    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    bool three=false;
    char arr[n][n],x=NULL,y=NULL;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>arr[i][j];
            if(j<=1&&i==0){
                 x=arr[0][0],y=arr[0][1];
            }else{
            if(arr[i][j]!=x&&arr[i][j]!=y){
            three=true;
            break;
        }
            }

        }
    }
    int j=0,p=n-1;
    for(int i=0;i<n;i++)
    {
        if(arr[i][j]!=x)
            three=true;
        j++;
        if(arr[i][p]!=x)
            three=true;
        p--;
    }

    if(three)cout<<"NO";
    else cout<<"YES";
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    bool three=false;
    char arr[n][n],x=NULL,y=NULL;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>arr[i][j];
            if(j<=1&&i==0){
                 x=arr[0][0],y=arr[0][1];
                 if(x==y)three=true;
            }else{
            if(arr[i][j]!=x&&arr[i][j]!=y){
            three=true;
            break;
        }
            }

        }
    }
    int j=0,p=n-1;
    for(int i=0;i<n;i++)
    {
        if(arr[i][j]!=x)
            three=true;
        j++;
        if(arr[i][p]!=x)
            three=true;
        p--;
    }

    if(three)cout<<"NO";
    else cout<<"YES";
    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main(){

	int Data;
	char Target[301][301];
	scanf("%d" , &Data);
	for(int i=0 ; i<Data ; i++)
	    for(int i2=0 ; i2<Data ; i2++)
	         cin >> Target[i][i2];
	for(int i=0 ; i<Data ; i++){
		int Attack=0;
		for(int i2=1 ; i2<Data-1; i2++){
		           //cout << Target[i][i2+1] << "=Target[i][i2+1]"<<  " " << Target[i][i2] << "=Target[i][i2]" << " " << Target[i][i2-1] << "=Target[i][i2-1]" << " "<< i2<< " " ;
		           //if(i+1<Data && i2+1<Data)cout << " " << Target[i+1][i2] << " " << Target[i][i2+1] << " " ;
		           //if( irget[i][i2+1]!=Target[i+1][i2] && Target[i][i2]!=Target[i+1][i2])return cout << "NO" , 0
		           if(Target[i][i2+1]!=Target[i][i2] && Target[i][i2+1]!=Target[i][i2-1] && Target[i][i2]!=Target[i][i2-1])return cout << "NO" , 0;

		}
	}
	cout <<"YES" ;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main (){

    int Search[27]={0}, Data2;
    string Data1 ;
    cin >> Data2;
    for(int i=0; i<Data2 ; i++)
    {
    	cin >> Data1;
    	for(int i2=0 ; i2<Data1.size(); i2++)
    		 Search[Data1[i2]-'a']++;
    }
    sort(Search , Search+27);
	//cout << Search[25] << " " << Search[26];
    for(int i=0 ; i<=24 ; i++)
    {
    	if(Search[i]!=Search[25] && Search[26]!=Search[i] && Search[i]>0)
    	{
    		cout << "NO";
    		return 0;
    	}
    }
    cout << "YES";
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main (){

	int Data;
	vector<string>Attack;
	scanf("%d" , &Data);
	char Target[301][301], Trap1 ,Trap2;
	for(int i=0 ; i<Data ; i++)
	{   string Okay;
	    for(int i2=0 ; i2<Data ; i2++){cin >> Target[i][i2], Okay+=Target[i][i2];}
	    Attack.push_back(Okay);
	}
	Trap1=Target[0][0]; Trap2=Target[0][1];
	for(int i=0 ; i<Attack.size() ; i++){
		int Hacked1=count(Attack[i].begin() , Attack[i].end() , Trap1) , Hacked2=count(Attack[i].begin() , Attack[i].end() , Trap2);
		if(i+1!=(Data/2)+1){
			if(Data==3 && Hacked1%2==0 && Hacked2 & 1 )continue;
			else if(Data>3 && Hacked1%2==0 && Hacked2 & 1 && Hacked1>1 && Hacked2 >1)continue;
			else return cout <<"NO" , 0;
		}
		else{
		            if(Hacked1==1 && Hacked2%2==0)continue;
		            else return cout <<"NO" , 0;
		}
	}
	cout <<"YES";
	// It will work ;

	return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main ()
{

    char Search[301][301] ;
    int Data1;
    cin >> Data1;
    for(int i=1 ; i<=Data1 ; i++)
    	for(int i2=1 ; i2<=Data1 ; i2++)
    		 cin >> Search[i][i2];
    char Target1=Search[1][1] , Target2=Search[1][2];
    int Trap1=0 , Trap2=0;
    if(Target1==Target2)return cout <<"NO" , 0;
    for(int i=1 ; i<=Data1 ; i++)
    {
    	for(int i2=1 ; i2<=Data1 ; i2++)
    	{
    		if(i2==i||i+i2==Data1+1)
    		{
    			if(Search[i][i2]!=Target1)Trap1++;
    		}
    		else if(Search[i][i2]!=Target2)Trap2++;
    	}
    }
    if(Trap1==0 && Trap2==0)cout <<"YES";
    else cout <<"NO";
    // I will be better , I won't give up
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main ()
{

    int Search[27]={0} , Attack;
    string Data;
    cin >> Attack ;
    for(int i=0 ; i<Attack ; i++)
    {
    	cin >> Data ;
    	for(int i2=0 ; i2<Data.size() ; i2++)
    		  Search[Data[i2]-'a']++;

    }
    sort(Search , Search+27);
	//for(int i=0 ; i<27 ; i++)
	    //cout << Search[i] << " "  << i << endl;
    int Hacked=((Attack*Attack)/3)+1;
	//cout << Hacked << Search[25];
	if(Search[25]!=Hacked||(Attack*Attack)-Hacked!=Search[26])cout <<"NO";
	else cout <<"YES";

	return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main(){

	int Data;
	char Target[301][301];
	scanf("%d" , &Data);
	for(int i=0 ; i<Data ; i++)
	    for(int i2=0 ; i2<Data ; i2++)
	         cin >> Target[i][i2];
	for(int i=0 ; i<Data ; i++){
		int Attack=0;
		for(int i2=0 ; i2<Data; i2++){
			if(Target[i-1][i2]==Target[i+1][i2] )Attack++;
			if(Target[i][i2-1]==Target[i][i2+1] )Attack++;
			if(Target[i][i2-1]==Target[i-1][i2] )Attack++;
			if(Target[i+1][i2]==Target[i][i2+1] )Attack++;
			if(Attack & 1)return cout <<"NO" , 0;
		}

	}
	cout <<"YES" ;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main (){

    int Search[27]={0}, Data2;
    string Data1 ;
    cin >> Data2;
    for(int i=0; i<Data2 ; i++)
    {
    	cin >> Data1;
    	for(int i2=0 ; i2<Data1.size(); i2++)
    		 Search[Data1[i2]-'a']++;
    }
    sort(Search , Search+27);
	//cout << Search[25] << " " << Search[26];
	int Hacked=(Data2*Data2)-Search[25];
	if(Hacked!=Search[26])
	{
	    cout <<"NO";
		return 0;
	}
    for(int i=0 ; i<=24 ; i++)
    {
    	if(Search[i]!=Search[25] && Search[26]!=Search[i] && Search[i]>0)
    	{
    		cout << "NO";
    		return 0;
    	}
    }
    cout << "YES";
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main ()
{

    int Search[27]={0} , Attack;
    string Data;
    cin >> Attack ;
    for(int i=0 ; i<Attack ; i++)
    {
    	cin >> Data ;
    	for(int i2=0 ; i2<Data.size() ; i2++)
    		  Search[Data[i2]-'a']++;

    }
    sort(Search , Search+27);
	//for(int i=0 ; i<27 ; i++)
	    //cout << Search[i] << " "  << i << endl;
    int Hacked=(Attack+Attack)-1;
	if(Attack==3)Hacked--;
	//cout << Hacked << Search[25];
	if(Search[25]!=Hacked||(Attack*Attack)-Hacked!=Search[26])cout <<"NO";
	else cout <<"YES";

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool Error=false;
int main ()
{

    int Search[27]={0} , Attack;
    string Data;
    cin >> Attack ;
    for(int i=0 ; i<Attack ; i++)
    {
    	cin >> Data ;
    	for(int i2=0 ; i2<Data.size() ; i2++)
		{
    		  Search[Data[i2]-'a']++;
			  if(i2>0 && Data[0]!=Data[i2])
			       Error=true;
		}
		if(Error==false)
		{
		   cout <<"NO";
		   return 0;
		}
		Error=false;

    }
    sort(Search , Search+27);
	//for(int i=0 ; i<27 ; i++)
	    //cout << Search[i] << " "  << i << endl;
    int Hacked=(Attack+Attack)-1;
	if(Attack==3)Hacked--;
	//cout << Hacked << Search[25];
	if(Search[25]!=Hacked||(Attack*Attack)-Hacked!=Search[26])cout <<"NO";
	else cout <<"YES";

	return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main(){

	int Data;
	char Target[301][301];
	scanf("%d" , &Data);
	for(int i=0 ; i<Data ; i++)
	    for(int i2=0 ; i2<Data ; i2++)
	         cin >> Target[i][i2];
	for(int i=0 ; i<Data ; i++){
		int Attack=0;
		for(int i2=0 ; i2<Data; i2++){
			if(Target[i-1][i2]==Target[i+1][i2] )Attack++;
			if(Target[i][i2-1]==Target[i][i2+1] )Attack++;
			if(Target[i][i2-1]==Target[i-1][i2] )Attack++;
			if(Target[i+1][i2]==Target[i][i2+1] )Attack++;
			if(Attack & 1 && Attack)return cout <<"NO" , 0;
		}

	}
	cout <<"YES" ;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main ()
{

    int Search[27]={0} , Attack;
    string Data;
    char Error;
    cin >> Attack ;
    for(int i=0 ; i<Attack ; i++)
    {
    	cin >> Data ;
    	for(int i2=0 ; i2<Data.size() ; i2++)
    		  Search[Data[i2]-'a']++;
		if(i==0)
			Error=Data[0];
		else{
			if((i+1)%2==0 && Error==Data[0])
			{
				cout << "NO";
				return 0;
			}
		}

    }
    sort(Search , Search+27);
	//for(int i=0 ; i<27 ; i++)
	    //cout << Search[i] << " "  << i << endl;
    int Hacked=(Attack+Attack)-1;
	if(Attack==3)Hacked--;
	//cout << Hacked << Search[25];
	if(Search[25]!=Hacked||(Attack*Attack)-Hacked!=Search[26])cout <<"NO";
	else cout <<"YES";

	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main (){

	int Data;
	vector<string>Attack;
	scanf("%d" , &Data);
	char Target[301][301], Trap1 ,Trap2;
	for(int i=0 ; i<Data ; i++)
	{   string Okay;
	    for(int i2=0 ; i2<Data ; i2++){cin >> Target[i][i2], Okay+=Target[i][i2];}
	    Attack.push_back(Okay);
	}
	Trap1=Target[0][0]; Trap2=Target[0][1];
	for(int i=0 ; i<Attack.size() ; i++){
		int Hacked1=count(Attack[i].begin() , Attack[i].end() , Trap1) , Hacked2=count(Attack[i].begin() , Attack[i].end() , Trap2);
		if(i+1!=(Data/2)+1){
			if(Hacked1%2==0 && Hacked2 &1 )continue;
			else return cout <<"NO" , 0;
		}
		else{
		            if(Hacked1==1 && Hacked2%2==0)continue;
		            else return cout <<"NO" , 0;
		}
	}
	cout <<"YES";
	// It will work ;

	return 0;
}


import sys
import os
range = xrange
input = raw_input

S = sys.stdin.read()
n = len(S)
A = []
i = 0
while i < n:
    c = 0
    while ord(S[i]) >= 48:
        c = 10 * c + ord(S[i]) - 48
        i += 1
    A.append(c)
    i += 1 + (S[i] == '\r')

inp = A; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        coupl[nei].remove(node)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

os.write(1, ' '.join(str(x + 1) for x in out))


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x + 1))
    builder.append(' ')

os.write(1, builder.build())


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = [[] for _ in range(n)]
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        if not found[nei]:
            children[node].append(nei)
            bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

out = [-1]*(2*n)
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        out[2*i] = j
    else:
        out[2*(~i) + 1] = j

print '\n'.join(str(x + 1) for x in out)


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
found[0] = 1
children = [[] for _ in range(n)]
for node in bfs:
    for nei in coupl[node]:
        if not found[nei]:
            found[nei] = 1
            children[node].append(nei)
            bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

#mark = [(l,r,node)]
mark = [(0, 2 * n - 1, 0)]
for l,r,node in mark:
    temp = l
    for child in children[node]:
        mark.append((temp, temp + 2 * family[child] - 1, child))
        temp += 2 * family[child] - 1

        A[r - 1] = ~child
        r -= 1
    A[r - 1] = node

L = [-1]*n
R = [-1]*n
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        L[i] = j + 1
    else:
        R[~i] = j + 1

print '\n'.join('%d %d' % (L[i], R[i]) for i in range(n))


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        coupl[nei].remove(node)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

print ' '.join(str(x + 1) for x in out)


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

looker = [str(x)[::-1] for x in range(10000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    x += 1
    while x >= 10000:
        x, xmod = divmod(x, 10000)
        builder.append(looker[xmod])
    builder.append(looker[x])
    builder.append(' ')

os.write(1, builder.build()[::-1])


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 1
#i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while S[i] >= '0':
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while S[i] >= '0':
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 1
    #i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

S = 'banana'

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while S[i] >= '0':
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while S[i] >= '0':
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [0]*(2 * n)
out[1] = 2 * n
mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        c = coupl[nei]
        i = 0
        while c[i] != node:
            i += 1
        c[i] = c.pop()

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x + 1))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while ord(S[i]) >= 48:
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while ord(S[i]) >= 48:
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

bfs = [0]
children = coupl
for node in bfs:
    for nei in coupl[node]:
        coupl[nei].remove(node)
        bfs.append(nei)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

looker = [str(x)[::-1] + ' ' for x in range(10000)]
looker2 = [str(x).rjust(4, '0')[::-1] for x in range(10000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    while x >= 10000:
        x, xmod = divmod(x, 10000)
        builder.append(looker2[xmod])
    builder.append(looker[x])

os.write(1, builder.build()[::-1])


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    x += 1
    while x:
        x, xmod = divmod(x, 10)
        builder.append(chr(xmod + 48))
    builder.append(' ')

os.write(1, builder.build()[::-1])


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        c = coupl[nei]
        i = 0
        while c[i] != node:
            i += 1
        c[i] = c[-1]
        c.pop()

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x + 1))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

looker = [str(x)[::-1] + ' ' for x in range(1000)]
looker2 = [str(x).rjust(3, '0')[::-1] for x in range(1000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    while x >= 1000:
        x, xmod = divmod(x, 1000)
        builder.append(looker2[xmod])
    builder.append(looker[x])

os.write(1, builder.build()[::-1])


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while ord(S[i]) >= 48:
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while ord(S[i]) >= 48:
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

bfs = [0]
children = coupl
for node in bfs:
    for nei in coupl[node]:
        coupl[nei].remove(node)
        bfs.append(nei)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import cStringIO
stream = cStringIO.StringIO()
f = stream.write
for x in out:
    f(str(x))
    f(' ')

os.write(1, stream.getvalue())


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 1
#i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while ord(S[i]) >= 48:
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while ord(S[i]) >= 48:
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 1
    #i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

bfs = [0]
children = coupl
for node in bfs:
    for nei in coupl[node]:
        d = coupl[nei]
        i = 0
        while d[i] != node:
            i += 1
        d[i] = d[-1]
        d.pop()
        bfs.append(nei)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import cStringIO
stream = cStringIO.StringIO()
f = stream.write
for x in out:
    f(str(x))
    f(' ')

os.write(1, stream.getvalue())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

looker = [str(x)[::-1] for x in range(1000)]
looker2 = [str(x).rjust(4, '0')[::-1] for x in range(1000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    x += 1
    while x >= 1000:
        x, xmod = divmod(x, 1000)
        builder.append(looker2[xmod])
    builder.append(looker[x])
    builder.append(' ')

os.write(1, builder.build()[::-1])


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        coupl[nei].remove(node)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

out = [-1]*(2*n)
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        out[2*i] = j
    else:
        out[2*(~i) + 1] = j

print ' '.join(str(x + 1) for x in out)


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        c = coupl[nei]
        i = 0
        while c[i] != node:
            i += 1
        c[i], c[-1] = c[-1], c[i]
        c.pop()

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x + 1))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        coupl[nei].remove(node)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

os.write(1, ' '.join(str(x + 1) for x in out))


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while S[i] >= '0':
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while S[i] >= '0':
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

S = 'banana'

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = [[] for _ in range(n)]
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        if not found[nei]:
            children[node].append(nei)
            bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

out = [-1]*(2*n)
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        out[2*i] = j
    else:
        out[2*(~i) + 1] = j

print ' '.join(str(x + 1) for x in out)


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = len(S)
A = []
i = 0
while i < n:
    c = 0
    while S[i] >= '0':
        c = 10 * c + ord(S[i]) - 48
        i += 1
    i += 1 + (S[i] == '\r')
    A.append(c)

inp = A; ii = 0
#inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        coupl[nei].remove(node)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

os.write(1, ' '.join(str(x + 1) for x in out))


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while S[i] >= '0':
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while S[i] >= '0':
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

bfs = [0]
children = coupl
for node in bfs:
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

looker = [str(x)[::-1] for x in range(1000)]
looker2 = [str(x).rjust(3, '0')[::-1] for x in range(1000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    x += 1
    while x >= 1000:
        x, xmod = divmod(x, 1000)
        builder.append(looker2[xmod])
    builder.append(looker[x])
    builder.append(' ')

os.write(1, builder.build()[::-1])


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while S[i] >= '0':
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while S[i] >= '0':
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[u - 1].append(v - 1)
    coupl[v - 1].append(u - 1)

bfs = [0]
children = coupl
for node in bfs:
    for nei in coupl[node]:
        coupl[nei].remove(node)
        bfs.append(nei)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        c = coupl[nei]
        i = 0
        while c[i] != node:
            i += 1
        c[i], c[-1] = c[-1], c[i]
        c.pop()

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

os.write(1, ' '.join(str(x + 1) for x in out))


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = len(S)
A = []
i = 0
while i < n:
    c = 0
    while ord(S[i]) >= 48:
        c = 10 * c + ord(S[i]) - 48
        i += 1
    A.append(c)
    i += 1 + (S[i] == '\r')

inp = A; ii = 0
#inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    for nei in coupl[node]:
        bfs.append(nei)
        c = coupl[nei]
        i = 0
        while c[i] != node:
            i += 1
        c[i], c[-1] = c[-1], c[i]
        c.pop()

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

os.write(1, ' '.join(str(x + 1) for x in out))


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
found[0] = 1
children = [[] for _ in range(n)]
for node in bfs:
    for nei in coupl[node]:
        if not found[nei]:
            found[nei] = 1
            children[node].append(nei)
            bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

#mark = [(l,node)]
mark = [0]*n
for node in bfs:
    temp = l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = temp
        temp += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

L = [-1]*n
R = [-1]*n
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        L[i] = j + 1
    else:
        R[~i] = j + 1

print '\n'.join(str(L[i]) + ' ' + str(R[i]) for i in range(n))


import sys
import os
range = xrange
input = raw_input

S = os.read(0, 2**25)
n = 0
i = 0
while S[i] >= '0':
    n = 10 * n + ord(S[i]) - 48
    i += 1
i += 2

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = v = 0
    while ord(S[i]) >= 48:
        u = 10 * u + ord(S[i]) - 48
        i += 1
    i += 1
    while ord(S[i]) >= 48:
        v = 10 * v + ord(S[i]) - 48
        i += 1
    i += 2
    coupl[(u if u < v else v) - 1].append((v if u < v else u) - 1)
for i in reversed(range(n)):
    for j in coupl[i]:
        coupl[j].append(i)

bfs = [0]
children = coupl
for node in bfs:
    for nei in coupl[node]:
        coupl[nei].remove(node)
        bfs.append(nei)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = [2 * n - 1]*(2 * n)
out[1] = 2 * n
for node in bfs:
    r = out[2 * node]
    l = r - (family[node] - 1)
    for child in children[node]:
        out[2 * child] = l = l + family[child] - 1
        out[2 * child + 1] = r
        r -= 1
    out[2 * node] = r

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in out:
    builder.append(str(x))
    builder.append(' ')

os.write(1, builder.build())


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

looker = [str(x)[::-1] + ' ' for x in range(10000)]
looker2 = [str(x).rjust(3, '0')[::-1] for x in range(10000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    x += 1
    while x >= 10000:
        x, xmod = divmod(x, 10000)
        builder.append(looker2[xmod])
    builder.append(looker[x])

os.write(1, builder.build()[::-1])


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
found[0] = 1
children = [[] for _ in range(n)]
for node in bfs:
    for nei in coupl[node]:
        if not found[nei]:
            found[nei] = 1
            children[node].append(nei)
            bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

#mark = [(l,node)]
mark = [0]*n
for node in bfs:
    temp = l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = temp
        temp += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

out = [-1]*(2*n)

for j in range(2 * n):
    i = A[j]
    if i >= 0:
        out[2*i] = j + 1
    else:
        out[2*(~i) + 1] = j + 1

print '\n'.join(str(x) for x in out)


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0

n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
found[0] = 1
children = [[] for _ in range(n)]
for node in bfs:
    for nei in coupl[node]:
        if not found[nei]:
            found[nei] = 1
            children[node].append(nei)
            bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    f = 0
    for child in children[node]:
        f += family[child]
    family[node] += f

A = [-1] * (2 * n)
A[-1] = ~0

#mark = [(l,node)]
mark = [(0, 0)]
for l,node in mark:
    r = l + 2 * family[node] - 1
    temp = l
    for child in children[node]:
        mark.append((temp, child))
        temp += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

L = [-1]*n
R = [-1]*n
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        L[i] = j + 1
    else:
        R[~i] = j + 1

print '\n'.join(str(L[i]) + ' ' + str(R[i]) for i in range(n))


import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    coupls = coupl[node]
    for i in range(len(coupls) - 1):
        if found[coupls[i]]:
            coupls[i],coupls[-1] = coupls[-1],coupls[i]
    if coupls and found[coupls[-1]]:
        coupls.pop()
    for nei in coupls:
        bfs.append(nei)

family = [1]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

A = [-1] * (2 * n)
A[-1] = ~0

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + 2 * family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += 2 * family[child] - 1
        r -= 1
        A[r] = ~child
    A[r - 1] = node

out = [-1]*(2*n)
for j in range(2 * n):
    i = A[j]
    if i >= 0:
        out[2*i] = j
    else:
        out[2*(~i) + 1] = j

print ' '.join(str(x + 1) for x in out)


import sys
import os
range = xrange
input = raw_input

inp = [int(x) for x in os.read(0, 2**25).split()]; ii = 0
n = inp[ii]; ii += 1

coupl = [[] for _ in range(n)]
for _ in range(n - 1):
    u = inp[ii] - 1; ii += 1
    v = inp[ii] - 1; ii += 1
    coupl[u].append(v)
    coupl[v].append(u)

bfs = [0]
found = [0]*n
children = coupl
for node in bfs:
    found[node] = 1
    bfs += coupl[node]
    for nei in coupl[node]:
        coupl[nei].remove(node)

family = [2]*n
for node in reversed(bfs):
    for child in children[node]:
        family[node] += family[child]

out = inp
out.append(0)
out[1] = 2 * n - 1

mark = [0]*n
for node in bfs:
    l = mark[node]
    r = l + family[node] - 1
    for child in children[node]:
        mark[child] = l
        l += family[child] - 1
        r -= 1
        out[2 * child + 1] = r
    out[2 * node] = r - 1

looker = [str(x)[::-1] for x in range(1000)]

import __pypy__
builder = __pypy__.builders.StringBuilder()
for x in reversed(out):
    x += 1
    while x >= 1000:
        x, xmod = divmod(x, 1000)
        builder.append(looker[xmod])
    builder.append(looker[x])
    builder.append(' ')

os.write(1, builder.build()[::-1])


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    st_append = st.append
    while st:
        v = st.pop()
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                st_append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                u_ = -u
                LL_rev[v] = u_
                #LL_rev[u_] = v_prev
                LL[v_prev] = u_
                LL[u_] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    sys.stdout.write(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    LL = [0] * (2 * N + 3)
    LL[-1] = 1
    LL[1] = 0
    LL_rev = [0] * (2 * N + 3)
    LL_rev[1] = -1
    LL_rev[-1] = 0
    st_append = st.append
    while st:
        v = st.pop()
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                st_append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                u_ = -u
                LL_rev[v] = u_
                #LL_rev[u_] = v_prev
                LL[v_prev] = u_
                LL[u_] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    sys.stdout.write(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    child = [[] for _ in range(N+1)]
    par = [0] * (N+1)
    seq = []
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                child[v].append(u)
                par[u] = v
                st.append(u)

    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    for v in seq:
        prev = v
        v_next = LL[v]
        for u in child[v]:
            LL[prev] = u
            LL_rev[u] = prev
            prev = u
        LL[prev] = v_next
        LL_rev[v_next] = prev

        prev = v
        v_next = LL_rev[v]
        for u in child[v]:
            LL_rev[prev] = -u
            LL[-u] = prev
            prev = -u
        LL_rev[prev] = v_next
        LL[v_next] = prev
    s = -1
    cnt = 1
    ans = [[] for _ in range(N+1)]
    while s != 0:
        ans[abs(s)].append(cnt)
        s = LL[s]
        cnt += 1
    for i in range(1, N+1):
        print(*ans[i])

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    seq = []
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                LL_rev[v] = -u
                LL_rev[-u] = v_prev
                LL[v_prev] = -u
                LL[-u] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    print(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    child = [[] for _ in range(N+1)]
    seq = []
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                child[v].append(u)
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                LL_rev[v] = -u
                LL_rev[-u] = v_prev
                LL[v_prev] = -u
                LL[-u] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    print(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    child = [[] for _ in range(N+1)]
    seq = []
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                child[v].append(u)
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                LL_rev[v] = -u
                LL_rev[-u] = v_prev
                LL[v_prev] = -u
                LL[-u] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    print(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    seq = []
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                u_ = -u
                LL_rev[v] = u_
                #LL_rev[u_] = v_prev
                LL[v_prev] = u_
                LL[u_] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    sys.stdout.write(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    seq = []
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                u_ = -u
                LL_rev[v] = u_
                #LL_rev[u_] = v_prev
                LL[v_prev] = u_
                LL[u_] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    print(ans)

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    child = [[] for _ in range(N+1)]
    seq = []
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                child[v].append(u)
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                LL_rev[v] = -u
                LL_rev[-u] = v_prev
                LL[v_prev] = -u
                LL[-u] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    for i in range(1, N + 1):
        print(ans1[i], ans2[i])

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    child = [[] for _ in range(N+1)]
    seq = []
    while st:
        v = st.pop()
        seq.append(v)
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                child[v].append(u)
                st.append(u)

    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    for v in seq:
        prev = v
        v_next = LL[v]
        for u in child[v]:
            LL[prev] = u
            LL_rev[u] = prev
            prev = u
        LL[prev] = v_next
        LL_rev[v_next] = prev

        prev = v
        v_next = LL_rev[v]
        for u in child[v]:
            LL_rev[prev] = -u
            LL[-u] = prev
            prev = -u
        LL_rev[prev] = v_next
        LL[v_next] = prev
    s = -1
    ans = [[] for _ in range(N+1)]
    for i in range(1, 2*N+1):
        ans[abs(s)].append(i)
        s = LL[s]
    for i in range(1, N+1):
        print(*ans[i])

if __name__ == '__main__':
    main()


def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    st = [1]
    seen = [0] * (N+1)
    seen[1] = 1
    LL = {-1: 1, 1: 0}
    LL_rev = {1: -1, -1: 0}
    while st:
        v = st.pop()
        for u in adj[v]:
            if not seen[u]:
                seen[u] = 1
                st.append(u)
                v_next = LL[v]
                v_prev = LL_rev[v]
                LL[v] = u
                LL[u] = v_next
                LL_rev[v_next] = u
                LL_rev[u] = v
                u_ = -u
                LL_rev[v] = u_
                #LL_rev[u_] = v_prev
                LL[v_prev] = u_
                LL[u_] = v
    s = -1
    ans1 = [0] * (N + 1)
    ans2 = [0] * (N + 1)
    for i in range(1, 2 * N + 1):
        if s < 0:
            ans1[-s] = i
        else:
            ans2[s] = i
        s = LL[s]
    ans = '\n'.join([' '.join(map(str, [ans1[i], ans2[i]])) for i in range(1, N + 1)])
    sys.stdout.write(ans)

if __name__ == '__main__':
    main()


@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE", "EqualsOrHashCode")

import java.io.PrintWriter
import java.lang.StringBuilder
import java.util.TreeSet
import kotlin.math.*
import kotlin.random.Random
import kotlin.collections.sort as _sort
import kotlin.collections.sortDescending as _sortDescending
import kotlin.io.println as iprintln

/** @author Spheniscine */
fun main() { _writer.solve(); _writer.flush() }
fun PrintWriter.solve() {
    val numCases = 1//readInt()
    case@ for(case in 1..numCases) {
        //print("Case #$case: ")

        val n = readInt()

        val G = Array(n) { IntList() }
        repeat(n-1) {
            val u = readInt()-1
            val v = readInt()-1
            G[u].add(v)
            G[v].add(u)
        }

        val L = IntArray(n)
        val R = IntArray(n)
        var j = 1

        val S = intListOf(0)
        L[0] = j++

        while(S.isNotEmpty()) {
            val u = S.pop()
            for(v in G[u]) if(L[v] == 0) {
                L[v] = j++
                S.add(v)
            }
            R[u] = j++
        }

        for(i in 0 until n) {
            println("${L[i]} ${R[i]}")
        }
    }
}

class IntList(initialCapacity: Int = 12) {
    private var _arr = IntArray(initialCapacity)
    private val capacity get() = _arr.size
    var size = 0
        private set
    inline val lastIndex get() = size - 1
    inline val indices get() = 0 until size

    constructor(copyFrom: IntArray): this(copyFrom.size) { copyFrom.copyInto(_arr); size = copyFrom.size }
    constructor(copyFrom: Collection<Int>): this(copyFrom.size) { _arr = copyFrom.toIntArray(); size = copyFrom.size }

    fun contentEquals(other: IntList): Boolean {
        return this === other || size == other.size && indices.all { this[it] == other[it] }
    }

    private fun grow(minCapacity: Int = 8) {
        val newCapacity = maxOf(minCapacity, capacity + (capacity shr 1))
        _arr = _arr.copyOf(newCapacity)
    }

    fun ensureCapacity(minCapacity: Int) { if(capacity < minCapacity) grow(minCapacity) }

    operator fun get(index: Int): Int {
        require(index in 0 until size)
        return _arr[index]
    }

    operator fun set(index: Int, value: Int) {
        require(index in 0 until size)
        _arr[index] = value
    }

    fun add(value: Int) {
        if(size == capacity) grow()
        _arr[size++] = value
    }

    fun add(index: Int, element: Int) {
        if(size == capacity) grow()
        _arr.copyInto(_arr, index + 1, index, size)
        size++
        set(index, element)
    }

    fun clear() { size = 0 }

    fun removeAt(index: Int): Int {
        val e = get(index)
        _arr.copyInto(_arr, index, index + 1, size)
        size--
        return e
    }

    operator fun iterator() = object: IntIterator() {
        private var pos = 0
        override fun hasNext() = pos < size
        override fun nextInt() = get(pos++)
    }

    inline fun isEmpty() = size == 0
    inline fun isNotEmpty() = size != 0

    fun pop() = _arr[--size]

    fun swap(i: Int, j: Int) { val t = this[i]; this[i] = this[j]; this[j] = t }
    fun reverse() {
        for(i in 0 until size / 2) swap(i, lastIndex - i)
    }

    fun shuffle(rnd: Random = random) = _shuffle(rnd, _arr::get, _arr::set, size)
    fun sort() { shuffle(); _arr._sort(0, size) }
    fun sortDescending() { sort(); reverse() }

    fun joinToString(separator: CharSequence) = if(size == 0) "" else let {
        buildString {
            append(it[0])
            for (i in 1 until size) {
                append(separator).append(it[i])
            }
        }
    }

    override fun toString() = "[" + joinToString(", ") + "]"

    fun toIntArray() = _arr.copyOf(size)
    fun toList() = List(size, ::get)

    inline fun first() = get(0)
    inline fun last() = get(lastIndex)
}

inline fun IntList(size: Int, init: (Int) -> Int) = IntList(size).apply {
    for(i in 0 until size) { add(init(i)) }
}
inline fun IntArray.toIntList() = IntList(this)
inline fun Collection<Int>.toIntList() = IntList(this)
inline fun intListOf(vararg values: Int) = IntList(values)

fun IntList.max() = (1 until size).fold(this[0]) { acc, i -> max(acc, this[i]) }
fun IntList.min() = (1 until size).fold(this[0]) { acc, i -> min(acc, this[i]) }
fun IntList.getOrNull(i: Int) = if(i in indices) get(i) else null
inline fun IntList.count(predicate: (Int) -> Boolean) = indices.count { predicate(this[it]) }
fun IntList.copyOf() = IntList(size, ::get)

/** IO */
//@JvmField val INPUT = File("input.txt").inputStream()
//@JvmField val OUTPUT = File("output.txt").outputStream()
@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

const val _BUFFER_SIZE = 1 shl 16
@JvmField val _buffer = ByteArray(_BUFFER_SIZE)
@JvmField var _bufferPt = 0
@JvmField var _bytesRead = 0

tailrec fun readChar(): Char {
    if(_bufferPt == _bytesRead) {
        _bufferPt = 0
        _bytesRead = INPUT.read(_buffer, 0, _BUFFER_SIZE)
    }
    return if(_bytesRead < 0) Char.MIN_VALUE
    else {
        val c = _buffer[_bufferPt++].toChar()
        if (c == '\r') readChar()
        else c
    }
}

fun readLine(): String? {
    var c = readChar()
    return if(c == Char.MIN_VALUE) null
    else buildString {
        while(true) {
            when(c) {
                '\n', Char.MIN_VALUE -> return@buildString
                else -> {
                    append(c)
                    c = readChar()
                }
            }
        }
    }
}
fun readLn() = readLine()!!

fun read() = buildString {
    var c = readChar()
    while(c <= ' ') {
        if(c == Char.MIN_VALUE) return@buildString
        c = readChar()
    }
    do {
        append(c)
        c = readChar()
    } while(c > ' ')
}
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

@JvmField val _writer = PrintWriter(OUTPUT, false)

/** shuffles and sort overrides to avoid quicksort attacks */
private inline fun <T> _shuffle(rnd: Random, get: (Int) -> T, set: (Int, T) -> Unit, size: Int) {
    // Fisher-Yates shuffle algorithm
    for (i in size - 1 downTo 1) {
        val j = rnd.nextInt(i + 1)
        val temp = get(i)
        set(i, get(j))
        set(j, temp)
    }
}

@JvmField var _random: Random? = null
val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }

fun IntArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun IntArray.sort() { shuffle(); _sort() }
fun IntArray.sortDescending() { shuffle(); _sortDescending() }

fun LongArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun LongArray.sort() { shuffle(); _sort() }
fun LongArray.sortDescending() { shuffle(); _sortDescending() }

fun DoubleArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun DoubleArray.sort() { shuffle(); _sort() }
fun DoubleArray.sortDescending() { shuffle(); _sortDescending() }

fun CharArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
inline fun CharArray.sort() { _sort() }
inline fun CharArray.sortDescending() { _sortDescending() }

inline fun <T: Comparable<T>> Array<out T>.sort() = _sort()
inline fun <T: Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
inline fun <T: Comparable<T>> MutableList<out T>.sort() = _sort()
inline fun <T: Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()

fun `please stop removing these imports IntelliJ`() { iprintln(max(1, 2)) }

/** additional commons */
inline fun <T> Iterable<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
inline fun <T> Sequence<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
inline fun <T> Array<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
fun IntArray.sumLong() = fold(0L, Long::plus)


import java.lang.StringBuilder
import java.util.*

fun main() {
    val n = readLine()!!.toInt()
    val adj = Array(n + 1) { mutableListOf<Int>() }
    for (j in 2..n) {
        val line = readLine()!!
        val six = line.indexOf(' ')
        val a = line.substring(0, six).toInt()
        val b = line.substring(six + 1).toInt()
        adj[a].add(b)
        adj[b].add(a)
    }
    val parent = IntArray(n + 1)
    val l = IntArray(n + 1)
    val r = IntArray(n + 1)
    val stack = Stack<Int>()
    l[1] = 1
    var k = 1
    stack.push(1)
    while (!stack.isEmpty()) {
        val a = stack.pop()
        for (b in adj[a]) {
            if (b != parent[a]) {
                parent[b] = a
                k++
                l[b] = k
            }
        }
        k++
        r[a] = k
        for (b in adj[a]) {
            if (b != parent[a]) {
                stack.push(b)
            }
        }
    }
    val out = StringBuilder()
    for (a in 1..n) {
        out.append(l[a]).append(' ').append(r[a]).append('\n')
    }
    print(out)
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class E {

	public static void main(String[] args) throws Exception {
//		StringTokenizer stok = new StringTokenizer(new Scanner(new File("F:/books/input.txt")).useDelimiter("\\A").next());
		StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next());
		StringBuilder sb = new StringBuilder();
		int n = Integer.parseInt(stok.nextToken());
		ArrayList<Integer>[] G = new ArrayList[n];
		for (int i = 0; i < n; i++)
			G[i] = new ArrayList<Integer>();
		for (int i = 0; i < n-1; i++) {
			int u = Integer.parseInt(stok.nextToken())-1;
			int v = Integer.parseInt(stok.nextToken())-1;
			G[u].add(v);
			G[v].add(u);
		}
		int[] a = dfsTree(G,0);
		int[][] b = new int[n+1][2];
		for(int i=0;i<a.length;i++) {
			if(b[a[i]][0]==0) b[a[i]][0] = i+1; else b[a[i]][1] = i+1;
		}
		for(int i=1;i<=n;i++) sb.append(b[i][0]+" "+b[i][1]+"\n");
		System.out.println(sb);
	}

	private static int[] dfsTree(ArrayList<Integer>[] G, int s) {
		int n = G.length;
		boolean[] visited= new boolean[n];
		int ptr=0;
		int[] ret = new int[2*n];
		int[] ss = new int[n];
		int[] p = new int[n];
		p[s] = s;
		ArrayDeque<Integer> S = new ArrayDeque<Integer>();
		S.add(s);
		while(!S.isEmpty()) {
			int c = S.pop();
			if(visited[c]!=true) {
				visited[c] = true;
				while(ss[c]<G[c].size() && visited[G[c].get(ss[c])]) ss[c]++;
				if(ss[c]==G[c].size()) {
					ret[ptr++]=c+1;
				}
				else {
					S.push(c);
					p[G[c].get(ss[c])]=c;
					S.push(G[c].get(ss[c]));
				}
			}
			else {
				ss[c]++;
				while(ss[c]<G[c].size() && visited[G[c].get(ss[c])]) ss[c]++;
				if(ss[c]==G[c].size()) {
					ret[ptr++] = c+1;
					for(int i=ss[c]-1;i>=0;i--) {
						if(G[c].get(i)!=p[c]) ret[ptr++] = G[c].get(i)+1;
					}
				}
				else {
					S.push(c);
					p[G[c].get(ss[c])]=c;
					S.push(G[c].get(ss[c]));
				}
			}
		}
		ret[ptr++]=s+1;
		return ret;
	}

}