#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <int,int> (x,i));
            bridg.insert(x);

        }
        int veiw = 0;
        sort(arr,arr+n-1);
        for (int i=0 ; i<n-1 ; i++)
        {
            it = bridg.lower_bound(arr[i].second.first);
            long long int now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                veiw = 1;
        }

        if (veiw == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <long long int,long long int> (x,i));
            bridg.insert(x);

        }
        int veiw = 0;
        sort(arr,arr+n-1);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            long long int now;
            it = bridg.lower_bound(1);
            if (*it == 1 && arr[i].second.first > 1)
                veiw = 1;

            it = bridg.lower_bound(arr[i].second.first);
            now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                veiw = 1;
        }

        if (veiw == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <long long int,long long int> (x,i));
            bridg.insert(x);

        }
        int veiw = 0;
        sort(arr,arr+n-1);
        for (int i=0 ; i<n-1 ; i++)
        {
            it = bridg.lower_bound(arr[i].second.first);
            long long int now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                veiw = 1;
        }

        if (veiw == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
pair <long long int,long long int> brr[maxix];
long long int ans[maxix];
set <long long int> bridg;
set <long long int>::iterator it;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    for (long long int i=0 ; i<n ; i++)
        cin >> brr[i].first >> brr[i].second;
    for (long long int i=0 ; i<n-1 ; i++)
    {
        arr[i].first = brr[i+1].second - brr[i].first;
        arr[i].second.first = brr[i+1].first - brr[i].second;
        arr[i].second.second = i;
    }
    for (long long int i=0 ; i<m ; i++)
    {

        cin >> x;
        brr[i].first = x;
        brr[i].second = i;
        bridg.insert(x);

    }
    int check = 0;
    sort(arr,arr+n-1);
    for (long long int i=0 ; i<n-1 ; i++)
    {
        it = bridg.lower_bound(arr[i].second.first);
        long long int now = *it;
        if (now <= arr[i].first)
        {
            ans[arr[i].second.second] = now;
            bridg.erase(now);
        }
        else
            check = 1;

    }
    sort(brr,brr+m);
    long long int index = 0;
    long long int mid, l, r;
    for (int i=0 ; i<n ; i++)
    {
        l = -1;
        r = m;
        while(r - l > 1)
        {
            mid = (l+r) / 2;
            if (ans[i] == brr[mid].first)
            {
                arr[index].first = brr[mid].second;
                index++;
                break;
            }
            else if (ans[i] > brr[mid].first)
                l = mid;
            else
                r = mid;
        }

    }
    if (index == n-1 && check == 0)
    {
        cout << "Yes" << "\n";
        for (long long int i=0 ; i<index ; i++)
            cout << arr[i].first + 1 << " ";
    }
    else
        cout << "No";
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
#include <map>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multiset <long long int>::iterator it2;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (long long int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (long long int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <int,int> (x,i));
            bridg.insert(x);

        }
        int veiw = 0;
        sort(arr,arr+n-1);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            it = bridg.lower_bound(arr[i].second.first);
            long long int now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it2 = bridg.find(now);
                bridg.erase(it2);
            }
            else
                veiw = 1;
        }

        long long int index = 0;
        if (veiw == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <long long int,long long int> (x,i));
            bridg.insert(x);

        }

        m = 0;
        sort(arr,arr+n-1);
        for (int i=0 ; i<n-1 ; i++)
        {
            long long int now;
            it = bridg.lower_bound(1);
            if (*it == 1 && arr[i].second.first > 1)
                m = 1;

            it = bridg.lower_bound(arr[i].second.first);
            now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                m = 1;
        }

        if (m == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
pair <long long int,long long int> brr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multiset <long long int>::iterator it2;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    for (long long int i=0 ; i<n ; i++)
        cin >> brr[i].first >> brr[i].second;
    for (long long int i=0 ; i<n-1 ; i++)
    {
        arr[i].first = brr[i+1].second - brr[i].first;
        arr[i].second.first = brr[i+1].first - brr[i].second;
        arr[i].second.second = i;
    }
    for (long long int i=0 ; i<m ; i++)
    {

        cin >> x;
        brr[i].first = x;
        brr[i].second = i;
        bridg.insert(x);

    }
    sort(arr,arr+n-1);
    sort(brr,brr+m);
    for (long long int i=0 ; i<n-1 ; i++)
    {
        it = bridg.lower_bound(arr[i].second.first);
        long long int now = *it;
        if (now <= arr[i].first)
        {
            ans[arr[i].second.second] = now;
            it2 = bridg.find(now);
            bridg.erase(it2);
        }
        else
            ans[arr[i].second.second] = -1;
    }
    long long int index = 0;
    long long int mid, l, r;
    for (int i=0 ; i<n ; i++)
    {
        l = -1;
        r = m;
        while(r - l > 1)
        {
            mid = (l+r) / 2;
            if (ans[i] == brr[mid].first && ans[i] != -1)
            {
                arr[index].first = brr[mid].second;
                brr[mid].first  = -1;
                index++;
                break;
            }
            else if (ans[i] > brr[mid].first)
                l = mid;
            else
                r = mid;
        }

    }
    if (index == n-1)
    {
        cout << "Yes" << "\n";
        for (long long int i=0 ; i<index ; i++)
            cout << arr[i].first + 1 << " ";
    }
    else
        cout << "No";
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (long long int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (long long int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <long long int,long long int> (x,i));
            bridg.insert(x);

        }

        m = 0;
        sort(arr,arr+n-1);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            long long int now;
            it = bridg.end();
            now = *--it;
            cout << now;
            if (arr[i].second.first > now)
                m = 1;

            it = bridg.lower_bound(arr[i].second.first);
            now = *it;

            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                m = 1;
        }

        if (m == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (long long int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
pair <long long int,long long int> brr[maxix];
long long int ans[maxix];
set <long long int> bridg;
set <long long int>::iterator it;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    for (long long int i=0 ; i<n ; i++)
        cin >> brr[i].first >> brr[i].second;
    for (long long int i=0 ; i<n-1 ; i++)
    {
        arr[i].first = brr[i+1].second - brr[i].first;
        arr[i].second.first = brr[i+1].first - brr[i].second;
        arr[i].second.second = i;
    }
    for (long long int i=0 ; i<m ; i++)
    {

        cin >> x;
        brr[i].first = x;
        brr[i].second = i;
        bridg.insert(x);

    }
    sort(arr,arr+n-1);
    for (long long int i=0 ; i<n-1 ; i++)
    {
        it = bridg.lower_bound(arr[i].second.first);
        long long int now = *it;
        if (now <= arr[i].first)
        {
            ans[arr[i].second.second] = now;
            bridg.erase(now);
        }
        else
            ans[arr[i].second.second] = -1;
    }
    sort(brr,brr+m);
    long long int index = 0;
    long long int mid, l, r;
    for (int i=0 ; i<n ; i++)
    {
        l = -1;
        r = m;
        while(r - l > 1)
        {
            mid = (l+r) / 2;
            if (ans[i] == brr[mid].first && ans[i] != -1)
            {
                arr[index].first = brr[mid].second;
                index++;
                break;
            }
            else if (ans[i] > brr[mid].first)
                l = mid;
            else
                r = mid;
        }

    }
    if (index == n-1)
    {
        cout << "Yes" << "\n";
        for (long long int i=0 ; i<index ; i++)
            cout << arr[i].first + 1 << " ";
    }
    else
        cout << "No";
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (long long int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (long long int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <long long int,long long int> (x,i));
            bridg.insert(x);

        }

        m = 0;
        sort(arr,arr+n-1);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            long long int now;
            it = bridg.end();
            now = *--it;
            if (arr[i].second.first > now)
                m = 1;

            it = bridg.lower_bound(arr[i].second.first);
            now = *it;

            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                m = 1;
        }

        if (m == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (long long int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
pair <long long int,long long int> brr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multiset <long long int>::iterator it2;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        for (long long int i=0 ; i<n ; i++)
            cin >> brr[i].first >> brr[i].second;
        for (long long int i=0 ; i<n-1 ; i++)
        {
            arr[i].first = brr[i+1].second - brr[i].first;
            arr[i].second.first = brr[i+1].first - brr[i].second;
            arr[i].second.second = i;
        }
        for (long long int i=0 ; i<m ; i++)
        {

            cin >> x;
            brr[i].first = x;
            brr[i].second = i;
                bridg.insert(x);

        }
        sort(arr,arr+n-1);
        sort(brr,brr+m);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            it = bridg.lower_bound(arr[i].second.first);
            long long int now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it2 = bridg.find(now);
                bridg.erase(it2);
            }
            else
                ans[arr[i].second.second] = -1;
        }

        long long int index = 0;
        long long int mid, l, r;
        for (int i=0 ; i<n-1 ; i++)
        {
            l = -1;
            r = m;
            int chlog = 0;
            while(r - l > 1 && chlog == 0)
            {
                mid = (l+r) / 2;
                if (ans[i] == brr[mid].first && ans[i] != -1 && brr[mid].second != -5)
                {
                    arr[index].first = brr[mid].second;
                    brr[mid].second = -5;
                    index++;
                    chlog = 1;
                }
                else if (ans[i] > brr[mid].first)
                    l = mid;
                else
                    r = mid;
            }

        }
        if (index == n-1)
        {
            cout << "Yes" << "\n";
            for (long long int i=0 ; i<index ; i++)
                cout << arr[i].first + 1 << " ";
        }
        else
            cout << "No";
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
#include <map>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
pair <long long int,long long int> brr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multiset <long long int>::iterator it2;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        for (long long int i=0 ; i<n ; i++)
            cin >> brr[i].first >> brr[i].second;
        for (long long int i=0 ; i<n-1 ; i++)
        {
            arr[i].first = brr[i+1].second - brr[i].first;
            arr[i].second.first = brr[i+1].first - brr[i].second;
            arr[i].second.second = i;
        }
        for (long long int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <int,int> (x,i));
            bridg.insert(x);

        }
        int veiw = 0;
        sort(arr,arr+n-1);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            it = bridg.lower_bound(arr[i].second.first);
            long long int now = *it;
            if (now <= arr[i].first)
            {
                ans[arr[i].second.second] = now;
                it2 = bridg.find(now);
                bridg.erase(it2);
            }
            else
                veiw = 1;
        }

        long long int index = 0;
        if (veiw == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const long long int maxix = 100*1000*2+10;
pair < long long int , pair <long long int,long long int> > arr[maxix];
long long int ans[maxix];
multiset <long long int> bridg;
multiset <long long int>::iterator it;
multimap <long long int,long long int> mp;
multimap <long long int,long long int>::iterator itmp;
int main()
{

    long long int n, m;
    long long int x;
    cin >> n >> m;
    if (m < n-1)
        cout << "No";
    else
    {
        long long int x1, x2, y1, y2;
        cin >> x1 >> y1;
        for (long long int i=0 ; i<n-1 ; i++)
        {
            cin >> x2 >> y2;
            arr[i].first = y2 - x1;
            arr[i].second.first = x2 - y1;
            arr[i].second.second = i;
            x1 = x2;
            y1 = y2;
        }
        for (long long int i=0 ; i<m ; i++)
        {

            cin >> x;
            mp.insert(pair <long long int,long long int> (x,i));
            bridg.insert(x);

        }

        m = 0;
        sort(arr,arr+n-1);
        for (long long int i=0 ; i<n-1 ; i++)
        {
            long long int now;
            it = bridg.end();
            now = *--it;
            if (arr[i].second.first > now)
                m = 1;

            it = bridg.lower_bound(arr[i].second.first);
            now = *it;

            if (now <= arr[i].first && m != 1)
            {
                ans[arr[i].second.second] = now;
                it = bridg.find(now);
                bridg.erase(it);
            }
            else
                m = 1;
        }

        if (m == 1)
            cout << "No";
        else
        {
            cout << "Yes" << "\n";
            for (long long int i=0 ; i<n-1 ; i++)
            {
                itmp = mp.find(ans[i]);
                cout << itmp->second + 1 << " ";
                mp.erase(itmp);

            }
        }
    }
   return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
struct bridge
{
		int index;
		long long value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
}x[200010];
struct interval
{
		long long x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.y==y?x<a.x:y<a.y;
		}
}y[200010];
int ans[200010];
int main()
{
		int n,m;
		long long a,b,c,d;
		while(scanf("%d%d",&n,&m)==2)
		{
				//scanf("%lld%lld",&a,&b);
				scanf("%I64d%I64d",&a,&b);
				for(int i=0;i<n-1;i++)
				{
						scanf("%I64d%I64d",&c,&d);
						//scanf("%lld%lld",&c,&d);
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						scanf("%I64d",&x[i].value);
						x[i].index=i;
				}
				std::sort(x,x+m);
				for(int i=0;i<n-1;i++)
				{
						bridge tmp;
						tmp.value = y[i].x;
						int low = std::lower_bound(x,x+m,tmp)-x;
						tmp.value = y[i].y;
						int up = std::upper_bound(x,x+m,tmp)-x;
						y[i].x = low;
						y[i].y = up-1;
				}
				std::sort(y,y+n-1);
				int index=0;
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						while(index<y[i].x) index++;
						if(index>y[i].y)
						{
								ok=false;
								break;
						}
						ans[y[i].index]=x[index].index+1;
						index++;
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
struct bridge
{
		int index;
		long long value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
}x[200010];
struct interval
{
		long long x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.x==x?y<a.y:x<a.x;
		}
}y[200010];
int ans[200010];
int main()
{
		int n,m;
		long long a,b,c,d;
		while(scanf("%d%d",&n,&m)==2)
		{
				scanf("%I64d%I64d",&a,&b);
				for(int i=0;i<n-1;i++)
				{
						scanf("%I64d%I64d",&c,&d);
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						scanf("%I64d",&x[i].value);
						x[i].index=i;
				}
				std::sort(x,x+m);
				for(int i=0;i<n-1;i++)
				{
						bridge tmp;
						tmp.value = y[i].x;
						int low = std::lower_bound(x,x+m,tmp)-x;
						tmp.value = y[i].y;
						int up = std::upper_bound(x,x+m,tmp)-x;
						y[i].x = low;
						y[i].y = up-1;
				}
				std::sort(y,y+n-1);
				int index=0;
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						while(index<y[i].x) index++;
						if(index>y[i].y)
						{
								ok=false;
								break;
						}
						ans[y[i].index]=x[index].index+1;
						index++;
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
struct bridge
{
		int index;
		int value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
}x[200010];
struct interval
{
		int x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.x==x?y<a.y:x<a.x;
		}
}y[200010];
int ans[200010];
int main()
{
		int n,m,a,b,c,d;
		while(scanf("%d%d",&n,&m)==2)
		{
				scanf("%d%d",&a,&b);
				for(int i=0;i<n-1;i++)
				{
						scanf("%d%d",&c,&d);
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						scanf("%d",&x[i].value);
						x[i].index=i;
				}
				std::sort(x,x+m);
				for(int i=0;i<n-1;i++)
				{
						bridge tmp;
						tmp.value = y[i].x;
						int low = std::lower_bound(x,x+m,tmp)-x;
						tmp.value = y[i].y;
						int up = std::upper_bound(x,x+m,tmp)-x;
						y[i].x = low;
						y[i].y = up-1;
				}
				std::sort(y,y+n-1);
				int index=0;
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						while(index<y[i].x) index++;
						if(index>y[i].y)
						{
								ok=false;
								break;
						}
						ans[y[i].index]=x[index].index+1;
						index++;
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
struct bridge
{
		int index;
		int value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
}x[200010];
struct interval
{
		int x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.x==x?y<a.y:x<a.x;
		}
}y[200010];
int ans[200010];
int main()
{
		int n,m,a,b,c,d;
		while(scanf("%d%d",&n,&m)==2)
		{
				scanf("%d%d",&a,&b);
				if(a<0||b<0) puts("jizz");
				for(int i=0;i<n-1;i++)
				{
						scanf("%d%d",&c,&d);
						if(c<0||d<0) puts("jizz");
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						scanf("%d",&x[i].value);
						if(x[i].value<0) puts("jizz");
						x[i].index=i;
				}
				std::sort(x,x+m);
				for(int i=0;i<n-1;i++)
				{
						bridge tmp;
						tmp.value = y[i].x;
						int low = std::lower_bound(x,x+m,tmp)-x;
						tmp.value = y[i].y;
						int up = std::upper_bound(x,x+m,tmp)-x;
						y[i].x = low;
						y[i].y = up-1;
				}
				std::sort(y,y+n-1);
				int index=0;
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						while(index<y[i].x) index++;
						if(index>y[i].y)
						{
								ok=false;
								break;
						}
						ans[y[i].index]=x[index].index+1;
						index++;
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
struct bridge
{
		int index;
		long long value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
};
std::multiset<bridge>x;
struct interval
{
		long long x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.y==y?x<a.x:y<a.y;
		}
}y[200010];
int ans[200010];
int main()
{
		int n,m;
		long long a,b,c,d,tmp;
		while(scanf("%d%d",&n,&m)==2)
		{
				scanf("%I64d%I64d",&a,&b);
				x.clear();
				for(int i=0;i<n-1;i++)
				{
						scanf("%I64d%I64d",&c,&d);
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						scanf("%I64d",&tmp);
						bridge OAO;
						OAO.value=tmp;
						OAO.index=i;
						x.insert(OAO);
				}
				std::sort(y,y+n-1);
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						bridge tmp;
						tmp.value = y[i].x;
						std::set<bridge>::iterator it=x.lower_bound(tmp);
						if(it==x.end() || it->value>y[i].y)
						{
								ok =false;
								break;
						}
						ans[y[i].index] = it->index+1;
						//printf("%d %d\n",y[i].index,it->index+1);
						x.erase(it);
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
struct bridge
{
		int index;
		long long value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
}x[200010];
struct interval
{
		long long x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.y==y?x<a.x:y<a.y;
		}
}y[200010];
int ans[200010];
bool used[200010];
int main()
{
		int n,m;
		long long a,b,c,d;
		while(scanf("%d%d",&n,&m)==2)
		{
				//scanf("%lld%lld",&a,&b);
				scanf("%I64d%I64d",&a,&b);
				for(int i=0;i<m;i++) used[i]=false;
				for(int i=0;i<n-1;i++)
				{
						scanf("%I64d%I64d",&c,&d);
						//scanf("%lld%lld",&c,&d);
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						//scanf("%lld",&x[i].value);
						scanf("%I64d",&x[i].value);
						x[i].index=i;
				}
				std::sort(x,x+m);
				std::sort(y,y+n-1);
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						bridge tmp;
						tmp.value = y[i].x;
						int low = std::lower_bound(x,x+m,tmp)-x;
						while(used[low]==true) low++;
						used[low]=true;
						if(low == m || x[low].value>y[i].y) ok =false;
						ans[y[i].index] = x[low].index+1;
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
struct bridge
{
		int index;
		int value;
		bool operator <(const bridge &a)const
		{
				return value<a.value;
		}
}x[200010];
struct interval
{
		int x,y;
		int index;
		bool operator <(const interval &a)const
		{
				return a.x==x?y<a.y:x<a.x;
		}
}y[200010];
int ans[200010];
int main()
{
		int n,m,a,b,c,d;
		while(scanf("%d%d",&n,&m)==2)
		{
				scanf("%d%d",&a,&b);
				for(int i=0;i<n-1;i++)
				{
						scanf("%d%d",&c,&d);
						y[i].x = c-b;
						y[i].y = d-a;
						y[i].index=i;
						a=c;
						b=d;
				}
				for(int i=0;i<m;i++)
				{
						scanf("%d",&x[i].value);
						x[i].index=i;
				}
				std::sort(x,x+m);
				std::sort(y,y+n-1);
				int index=0;
				bool ok=true;
				for(int i=0;i<n-1;i++)
				{
						while(x[index].value<y[i].x) index++;
						if(x[index].value>y[i].y)
						{
								ok=false;
								break;
						}
						ans[y[i].index]=x[index].index+1;
						index++;
				}
				if(ok)
				{
						puts("Yes");
						for(int i=0;i<n-1;i++) printf("%d%c",ans[i],i==n-2?'\n':' ');
				}
				else puts("No");
		}
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len==t.len ? i<t.i : len<t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return y==t.y ? (x==t.x ? i<t.i : x>t.x) : y<t.y;
    }
}s[N];
set<Bridge> bridge;
set<Bridge>::iterator it;
int ans[N];
int main()
{
    // freopen("in.txt", "r", stdin);
    int n, m;
    while(2==scanf("%d%d", &n, &m)){
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        bridge.clear();
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
            bridge.insert(b[i]);
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
        }
        sort(s, s+n-1);
        memset(ans, -1, sizeof(ans));
        b[m].i = -1;
        bool no = 0;
        for(int i=0; i<n-1 && !bridge.empty(); i++){
            b[m].i = -1;
            b[m].len = s[i].x;
            it = bridge.lower_bound(b[m]);
            if(it==bridge.end() || (*it).len>s[i].y){
                no = 1;
                break;
            }
            ans[s[i].i] = (*it).i;
            bridge.erase(it);
        }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len < t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return x==t.x ? y<t.y : x<t.x;
    }
}s[N];
int n, m, start;
int ans[N];
int set_bridge(int i, int limit){
    if(i>n-1)   return 0;
    if(i==n-1 || s[i+1].y>limit){
        if(s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return i;
    }
    if(s[i].y<s[i+1].y){
        if(s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return max(i+1, set_bridge(i+1, limit));
    }
    else{
        int j = max(i+1, set_bridge(i+1, s[i].y));
        if(s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return max(j+1, set_bridge(j+1, limit));
    }
}
int main()
{
    // freopen("in.txt", "r", stdin);
    while(2==scanf("%d%d", &n, &m)){
        ll limit = 0;
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
            limit = max(limit, s[i].y);
        }
        sort(s, s+n-1);
        sort(b, b+m);
        memset(ans, -1, sizeof(ans));
        start = 0;
        set_bridge(0, limit);
        if(start<n-1){
            printf("No\n");
            continue;
        }
        bool no = 0;
        for(int i=0; i<n-1; i++){
            if(ans[i]==-1){
                no = 1;
                break;
            }
        }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len < t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return x==t.x ? y<t.y : x<t.x;
    }
}s[N];
int n, m, start;
int ans[N];
int set_bridge(int i, int limit){
    if(i>n-2 || start>=m)   return 0;
    if(i==n-2 || s[i+1].y>limit){
        while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
        if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return i;
    }
    if(s[i].y<s[i+1].y){
        while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
        if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return set_bridge(i+1, limit);
    }
    else{
        int j = set_bridge(i+1, s[i].y);
        while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
        if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return max(j+1, set_bridge(j+1, limit));
    }
}
int main()
{
    // freopen("in.txt", "r", stdin);
    while(2==scanf("%d%d", &n, &m)){
        ll limit = 0;
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
            limit = max(limit, s[i].y);
        }
        sort(s, s+n-1);
        sort(b, b+m);
        memset(ans, -1, sizeof(ans));
        start = 0;
        set_bridge(0, limit);
        if(start<n-1){
            printf("No\n");
            continue;
        }
        bool no = 0;
        for(int i=0; i<n-1; i++){
            if(ans[i]==-1){
                no = 1;
                break;
            }
        }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len < t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return x==t.x ? y<t.y : x<t.x;
    }
}s[N];
int n, m, start;
int ans[N];
int set_bridge(int i, int limit){
    if(i>n-1)   return 0;
    if(i==n-1 || s[i+1].y>limit){
        if(s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return i;
    }
    if(s[i].y<s[i+1].y){
        if(s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return max(i+1, set_bridge(i+1, limit));
    }
    else{
        int j = max(i+1, set_bridge(i+1, s[i].y));
        if(s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return set_bridge(j+1, limit);
    }
}
int main()
{
    // freopen("in.txt", "r", stdin);
    while(2==scanf("%d%d", &n, &m)){
        ll limit = 0;
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
            limit = max(limit, s[i].y);
        }
        sort(s, s+n-1);
        sort(b, b+m);
        memset(ans, -1, sizeof(ans));
        start = 0;
        set_bridge(0, limit);
        if(start<n-1){
            printf("No\n");
            continue;
        }
        bool no = 0;
        for(int i=0; i<n-1; i++){
            if(ans[i]==-1){
                no = 1;
                break;
            }
        }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len < t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return x==t.x ? y<t.y : x<t.x;
    }
}s[N];
int n, m, start;
int ans[N];
int set_bridge(int i, int limit){
    // printf("set: %d %d %d %d\n", i, limit, start, n);
    if(i>=n-1 || start>=m)   return 0;
    if(i==n-2 || s[i+1].y>limit){
        while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
        if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return i;
    }
    if(s[i].y<s[i+1].y){
        // printf("34\n");
        while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
        if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
        }
        return set_bridge(i+1, limit);
    }
    else{
        // printf("42\n");
        int j = set_bridge(i+1, s[i].y);
        while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
        if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
            ans[s[i].i] = b[start++].i;
            // printf("48\n");
        }
        return max(j, set_bridge(j+1, limit));
    }
}
int main()
{
    // freopen("in.txt", "r", stdin);
    while(2==scanf("%d%d", &n, &m)){
        ll limit = 0;
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
            limit = max(limit, s[i].y);
        }
        sort(s, s+n-1);
        sort(b, b+m);
        memset(ans, -1, sizeof(ans));
        start = 0;
        set_bridge(0, limit);
        if(start<n-1){
            printf("No\n");
            continue;
        }
        bool no = 0;
        for(int i=0; i<n-1; i++){
            if(ans[i]==-1){
                no = 1;
                break;
            }
        }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len<t.len;
        return len==t.len ? i<t.i : len<t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return y<t.y;
        return y==t.y ? (x==t.x ? i<t.i : x>t.x) : y<t.y;
    }
}s[N];
set<Bridge> bridge;
set<Bridge>::iterator it;
int ans[N];
int main()
{
    // freopen("in.txt", "r", stdin);
    int n, m;
    while(2==scanf("%d%d", &n, &m)){
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        bridge.clear();
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
            bridge.insert(b[i]);
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
        }
        sort(s, s+n-1);
        memset(ans, -1, sizeof(ans));
        // b[m].i = -1;
        bool no = 0;
        for(int i=0; i<n-1; i++){
            // cout << s[i].x << " " << s[i].y << endl;
            b[m].i = -1;
            b[m].len = s[i].x;
            if(bridge.empty())  {no = 1; break;}
            it = bridge.lower_bound(b[m]);
            // printf("test: %d\n", (*it).i);
            if(it==bridge.end() || (*it).len>s[i].y)    {no = 1; break;}
            ans[s[i].i] = (*it).i;
            bridge.erase(it);
        }
        // bool no = 0;
        // for(int i=0; i<n-1; i++){
        //     if(ans[i]==-1){
        //         no = 1;
        //         break;
        //     }
        // }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 11;
ll l[N], r[N];
struct Bridge{
    int i;
    ll len;
    bool operator < (const Bridge &t)const{
        return len==t.len ? i<t.i : len<t.len;
    }
}b[N];
struct Seg{
    ll x, y;
    int i;
    bool operator <(const Seg &t)const{
        return y==t.y ? (x==t.x ? i<t.i : x>t.x) : y<t.y;
    }
}s[N];
int ans[N];
int main()
{
    // freopen("in.txt", "r", stdin);
    int n, m;
    while(2==scanf("%d%d", &n, &m)){
        for(int i=0; i<n; i++){
            cin >> l[i] >> r[i];
        }
        for(int i=0; i<m; i++){
            b[i].i = i+1;
            cin >> b[i].len;
        }
        for(int i=0; i<n-1; i++){
            s[i].x = l[i+1] - r[i];
            s[i].y = r[i+1] - l[i];
            s[i].i = i;
        }
        sort(s, s+n-1);
        sort(b, b+m);
        memset(ans, -1, sizeof(ans));
        int start = 0;
        for(int i=0; i<n-1 && start<m; i++){
            while(start<m && !(s[i].x <= b[start].len && b[start].len <= s[i].y))  start++;
            if(start <m && s[i].x <= b[start].len && b[start].len <= s[i].y){
                ans[s[i].i] = b[start++].i;
                // printf("set: %d %d\n", s[i].i, start-1);
            }
        }
        bool no = 0;
        for(int i=0; i<n-1; i++){
            if(ans[i]==-1){
                no = 1;
                break;
            }
        }
        if(no){
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        for(int i=0; i<n-1; i++){
            printf("%d%s", ans[i], i<n-2 ? " " : "\n");
        }
    }
    return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
// #pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int MAXN = 1e6 + 100;

vector<vector<int>> L;

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo]+1;
			DFS(v, nodo);
		}
	}
}

set<pair<int,int>> stt;

int arr[MAXN];

void DSU_ON_TREE_ADD(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]++;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

void DSU_ON_TREE_DELETE(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]--;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

int res[MAXN];

void DSU_ON_TREE(vector<vector<int>> &L, int root = 0)
{
    int N = L.size();
    vector<int> sz(N);
    vector<int> st(N);
    vector<int> ft(N);
    vector<int> ver(N);
    int contg = -1;
    function<void(int,int)> precal = [&](int nodo, int padre)
    {
        ver[++contg] = nodo;
        st[nodo] = contg;
        sz[nodo] = 1;
        for(auto v : L[nodo])
        {
            if(v != padre)
            {
                precal(v,nodo);
                sz[nodo] += sz[v];
            }
        }
        ft[nodo] = contg;
    };
    precal(root,-1);
    function<void(int,int,bool)> DSUT = [&](int nodo, int padre, bool keep)
    {
        int may = -1, big = -1;
        for(auto v : L[nodo])
            if(v != padre && sz[v] > may)
                may = sz[v], big = v;

        for(auto v : L[nodo])
            if(v != padre && v != big)
                DSUT(v,nodo,0);

        if(big != -1)DSUT(big,nodo,1);

        for(auto v : L[nodo])
            if(v != padre && v != big)
                for(int i = st[v] ; i <= ft[v] ; i++)
                    DSU_ON_TREE_ADD(ver[i]);

        DSU_ON_TREE_ADD(nodo);

        pair<int,int> pp = *(--stt.end());

        res[nodo] = -pp.second - lvl[nodo];

        if(!keep)
            for(int i = st[nodo] ; i <= ft[nodo] ; i++)
                DSU_ON_TREE_DELETE(ver[i]);
    };
    DSUT(root,-1,false);
}

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

    int N;
    cin >> N;

	L.resize(N+1);

    for(int i = 0 ; i < N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;
    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    DSU_ON_TREE(L, 1);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << res[i] << '\n';
    }

    return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

// #define int long long

using namespace std;

#ifdef LOCAL
#include "/media/brayand/BrayanD/debugger.h"
#else
#define db(...) false
#define dbg(...) false
#endif

const int MAXN = 1e6 + 100;

vector<int> L[MAXN];

vector<int> arr[MAXN];

pair<int,int> res[MAXN];

pair<int,int> ans[MAXN];

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	arr[nodo].push_back(1);
	res[nodo] = {1,0};

	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo] + 1;
			DFS(v, nodo);
			arr[v].push_back(0);
			res[v].second--;

			if(arr[nodo].size() < arr[v].size())
			{
				swap(arr[nodo], arr[v]);
				swap(res[nodo], res[v]);
			}

			int pt = arr[nodo].size()-1;

			while(arr[v].size())
			{
				arr[nodo][pt] += arr[v].back();
				res[nodo] = max(res[nodo], {arr[nodo][pt], -(arr[nodo].size() - pt - 1)});
				arr[v].pop_back();
				pt--;
			}
		}
	}

	ans[nodo] = res[nodo];
}

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

    int N;
    cin >> N;

    for(int i = 1 ; i <= N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;

    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << -ans[i].second << '\n';
    }

    return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

// #define int long long

using namespace std;

const int MAXN = 1e6 + 100;

vector<vector<int>> L;

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo]+1;
			DFS(v, nodo);
		}
	}
}

set<pair<int,int>> stt;

int arr[MAXN];

void DSU_ON_TREE_ADD(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]++;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

void DSU_ON_TREE_DELETE(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]--;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

int res[MAXN];

void DSU_ON_TREE(vector<vector<int>> &L, int root = 0)
{
    int N = L.size();
    vector<int> sz(N);
    vector<int> st(N);
    vector<int> ft(N);
    vector<int> ver(N);
    int contg = -1;
    function<void(int,int)> precal = [&](int nodo, int padre)
    {
        ver[++contg] = nodo;
        st[nodo] = contg;
        sz[nodo] = 1;
        for(auto v : L[nodo])
        {
            if(v != padre)
            {
                precal(v,nodo);
                sz[nodo] += sz[v];
            }
        }
        ft[nodo] = contg;
    };
    precal(root,-1);
    function<void(int,int,bool)> DSUT = [&](int nodo, int padre, bool keep)
    {
        int may = -1, big = -1;
        for(auto v : L[nodo])
            if(v != padre && sz[v] > may)
                may = sz[v], big = v;

        for(auto v : L[nodo])
            if(v != padre && v != big)
                DSUT(v,nodo,0);

        if(big != -1)DSUT(big,nodo,1);

        for(auto v : L[nodo])
            if(v != padre && v != big)
                for(int i = st[v] ; i <= ft[v] ; i++)
                    DSU_ON_TREE_ADD(ver[i]);

        DSU_ON_TREE_ADD(nodo);

        pair<int,int> pp = *(--stt.end());

        res[nodo] = -pp.second - lvl[nodo];

        if(!keep)
            for(int i = st[nodo] ; i <= ft[nodo] ; i++)
                DSU_ON_TREE_DELETE(ver[i]);
    };
    DSUT(root,-1,false);
}

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

    int N;
    cin >> N;

	L.resize(N+1);

	for(int i = 0 ; i < N ; i++)
	{
		stt.insert({0,i});
	}

    for(int i = 0 ; i < N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;
    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    DSU_ON_TREE(L, 1);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << res[i] << '\n';
    }

    return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

// #define int long long

using namespace std;

const int MAXN = 1e6 + 100;

vector<vector<int>> L;

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo]+1;
			DFS(v, nodo);
		}
	}
}

set<pair<int,int>> stt;

int arr[MAXN];

inline void DSU_ON_TREE_ADD(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]++;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

inline void DSU_ON_TREE_DELETE(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]--;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

int res[MAXN];

void DSU_ON_TREE(vector<vector<int>> &L, int root = 0)
{
    int N = L.size();
    vector<int> sz(N);
    vector<int> st(N);
    vector<int> ft(N);
    vector<int> ver(N);
    int contg = -1;
    function<void(int,int)> precal = [&](int nodo, int padre)
    {
        ver[++contg] = nodo;
        st[nodo] = contg;
        sz[nodo] = 1;
        for(auto v : L[nodo])
        {
            if(v != padre)
            {
                precal(v,nodo);
                sz[nodo] += sz[v];
            }
        }
        ft[nodo] = contg;
    };
    precal(root,-1);
    function<void(int,int,bool)> DSUT = [&](int nodo, int padre, bool keep)
    {
        int may = -1, big = -1;
        for(auto v : L[nodo])
            if(v != padre && sz[v] > may)
                may = sz[v], big = v;

        for(auto v : L[nodo])
            if(v != padre && v != big)
                DSUT(v,nodo,0);

        if(big != -1)DSUT(big,nodo,1);

        for(auto v : L[nodo])
            if(v != padre && v != big)
                for(int i = st[v] ; i <= ft[v] ; i++)
                    DSU_ON_TREE_ADD(ver[i]);

        DSU_ON_TREE_ADD(nodo);

        pair<int,int> pp = *(--stt.end());

        res[nodo] = -pp.second - lvl[nodo];

        if(!keep)
            for(int i = st[nodo] ; i <= ft[nodo] ; i++)
                DSU_ON_TREE_DELETE(ver[i]);
    };
    DSUT(root,-1,false);
}

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

    int N;
    cin >> N;

	L.resize(N+1);

	for(int i = 0 ; i < N ; i++)
	{
		stt.insert({0,i});
	}

    for(int i = 0 ; i < N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;
    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    DSU_ON_TREE(L, 1);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << res[i] << '\n';
    }

    return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

#define int long long

using namespace std;

#ifdef LOCAL
#include "/media/brayand/BrayanD/debugger.h"
#else
#define db(...) false
#define dbg(...) false
#endif

const int MAXN = 1e6 + 100;

vector<int> L[MAXN];

vector<int> arr[MAXN];

pair<int,int> res[MAXN];

pair<int,int> ans[MAXN];

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	arr[nodo].push_back(1);
	res[nodo] = {1,0};

	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo] + 1;
			DFS(v, nodo);
			arr[v].push_back(0);
			res[v].second--;

			if(arr[nodo].size() < arr[v].size())
			{
				swap(arr[nodo], arr[v]);
				swap(res[nodo], res[v]);
			}

			int pt = arr[nodo].size()-1;

			while(arr[v].size())
			{
				arr[nodo][pt] += arr[v].back();
				res[nodo] = max(res[nodo], {arr[nodo][pt], -(arr[nodo].size() - pt - 1)});
				arr[v].pop_back();
				pt--;
			}
		}
	}

	ans[nodo] = res[nodo];
}

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

    int N;
    cin >> N;

    for(int i = 1 ; i <= N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;

    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << -ans[i].second << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

#ifdef LOCAL
#include "/media/brayand/BrayanD/debugger.h"
#else
#define db(...) false
#define dbg(...) false
#endif

const int MAXN = 1e6 + 100;

vector<int> L[MAXN];

vector<int> arr[MAXN];

pair<int,int> res[MAXN];

pair<int,int> ans[MAXN];

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	arr[nodo].push_back(1);
	res[nodo] = {1,0};

	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo] + 1;
			DFS(v, nodo);
			arr[v].push_back(0);
			res[v].second--;

			if(arr[nodo].size() < arr[v].size())
			{
				swap(arr[nodo], arr[v]);
				swap(res[nodo], res[v]);
			}

			int pt = arr[nodo].size()-1;

			while(arr[v].size())
			{
				arr[nodo][pt] += arr[v].back();
				res[nodo] = max(res[nodo], {arr[nodo][pt], -(arr[nodo].size() - pt - 1)});
				arr[v].pop_back();
				pt--;
			}
		}
	}

	ans[nodo] = res[nodo];
}

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

    int N;
    cin >> N;

    for(int i = 1 ; i <= N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;

    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << -ans[i].second << '\n';
    }

    return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
// #pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int MAXN = 1e6 + 100;

vector<vector<int>> L;

int lvl[MAXN];

void DFS(int nodo, int padre)
{
	for(auto v : L[nodo])
	{
		if(v != padre)
		{
			lvl[v] = lvl[nodo]+1;
			DFS(v, nodo);
		}
	}
}

set<pair<int,int>> stt;

int arr[MAXN];

void DSU_ON_TREE_ADD(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]++;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

void DSU_ON_TREE_DELETE(int nodo)
{
	stt.erase({arr[lvl[nodo]], -lvl[nodo]});
	arr[lvl[nodo]]--;
	stt.insert({arr[lvl[nodo]], -lvl[nodo]});
}

int res[MAXN];

void DSU_ON_TREE(vector<vector<int>> &L, int root = 0)
{
    int N = L.size();
    vector<int> sz(N);
    vector<int> st(N);
    vector<int> ft(N);
    vector<int> ver(N);
    int contg = -1;
    function<void(int,int)> precal = [&](int nodo, int padre)
    {
        ver[++contg] = nodo;
        st[nodo] = contg;
        sz[nodo] = 1;
        for(auto v : L[nodo])
        {
            if(v != padre)
            {
                precal(v,nodo);
                sz[nodo] += sz[v];
            }
        }
        ft[nodo] = contg;
    };
    precal(root,-1);
    function<void(int,int,bool)> DSUT = [&](int nodo, int padre, bool keep)
    {
        int may = -1, big = -1;
        for(auto v : L[nodo])
            if(v != padre && sz[v] > may)
                may = sz[v], big = v;

        for(auto v : L[nodo])
            if(v != padre && v != big)
                DSUT(v,nodo,0);

        if(big != -1)DSUT(big,nodo,1);

        for(auto v : L[nodo])
            if(v != padre && v != big)
                for(int i = st[v] ; i <= ft[v] ; i++)
                    DSU_ON_TREE_ADD(ver[i]);

        DSU_ON_TREE_ADD(nodo);

        pair<int,int> pp = *(--stt.end());

        res[nodo] = -pp.second - lvl[nodo];

        if(!keep)
            for(int i = st[nodo] ; i <= ft[nodo] ; i++)
                DSU_ON_TREE_DELETE(ver[i]);
    };
    DSUT(root,-1,false);
}

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

    int N;
    cin >> N;

	L.resize(N+1);

	for(int i = 0 ; i < N ; i++)
	{
		stt.insert({0,i});
	}

    for(int i = 0 ; i < N-1 ; i++)
    {
    	int a, b;
    	cin >> a >> b;
    	L[a].push_back(b);
    	L[b].push_back(a);
    }

    DFS(1, 0);

    DSU_ON_TREE(L, 1);

    for(int i = 1 ; i <= N ; i++)
    {
    	cout << res[i] << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], freq[N], cur = -1, mx, n;
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	st.insert({0, dep});
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	freq[depth[node]] += type;
	if(type == 1) {
		if(freq[depth[node]] > mx) {
			mx = freq[depth[node]];
			cur = depth[node];
		} else if(freq[depth[node]] == mx)
			cur = min(cur, depth[node]);
	}
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	if(cur == -1)
		cur = depth[node], mx = freq[depth[node]];
	add(node, par, 1);
	ans.push_back(cur - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1), cur = -1, mx = 0;
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], freq[N], cur = -1, n;
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	sz[node] = 1;
	depth[node] = dep;
	st.insert({0, dep});
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int big, int type) {
	freq[depth[node]] += type;
	if(type == 1) {
		if(freq[depth[node]] > freq[cur]) {
			cur = depth[node];
		} else if(freq[depth[node]] == freq[cur])
			cur = min(cur, depth[node]);
	}
	for(auto it: adj[node]) {
		if(it == par || big == it) continue;
		add(it, node, big, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1);
	if(cur == -1)
		cur = depth[node];
	add(node, par, bigChild, 1);
	ans.push_back(cur - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1, -1), cur = -1;
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], ans[N], bigVis[N], freq[N], cur;

void pre(int node, int par, int dep) {
    depth[node] = dep;
    sz[node] = 1;
    for(auto it: adj[node]) {
        if(it == par) continue;
        pre(it, node, dep + 1);
        sz[node] += sz[it];
    }
}

void add(int node, int par, int type) {
    if(type == 1) {
        ++freq[depth[node]];
        if(freq[depth[node]] > freq[cur]) {
            cur = depth[node];
        } else if(freq[depth[node]] == freq[cur])
            cur = min(depth[node], cur);
    } else {
        --freq[depth[node]];
    }
    for(auto it: adj[node]) {
        if(it == par || bigVis[it]) continue;
        add(it, node, type);
    }
}

void dfs(int node, int par, bool keep) {
    int big = -1;
    for(auto it: adj[node]) {
        if(it == par) continue;
        if(big == -1) big = it;
        else if(sz[it] > sz[big]) big = it;
    }
    for(auto it: adj[node]) {
        if(it == par || it == big) continue;
        dfs(it, node, 0);
    }
    if(big != -1)
        dfs(big, node, 1), bigVis[big] = 1;
    if(cur == -1) cur = depth[node];
    add(node, par, 1);
    ans[node] = cur - depth[node];
    if(big != -1)
        bigVis[big] = 0;
    if(!keep)
        add(node, par, -1), cur = -1;
}

void runtestcase() {
    int n, u, v;
    cin >> n;
    for(int i = 1; i < n; ++i) {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    pre(1, 1, 1);
    dfs(1, 1, 1);
    for(int i = 1; i <= n; ++i)
        cout << ans[i] << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], tree[4*N], n;
bool big[N];

void update(int node, int start, int end, int idx, int val) {
	if(start == end) {
		if(val == -1)
			tree[node] = 0;
		else
			tree[node]++;
		return;
	}
	int mid = (start + end) / 2;
	if(idx <= mid)
		update(node * 2, start, mid, idx, val);
	else
		update(node * 2 + 1, mid + 1, end, idx, val);
	tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}

int query(int node, int start, int end) {
	if(start == end)
		return start;
	int mid = (start + end) / 2;
	if(tree[node * 2] >= tree[node * 2 + 1])
		return query(node * 2, start, mid);
	else
		return query(node * 2 + 1, mid + 1, end);
}

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int val) {
	update(1, 1, n, depth[node], val);
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, val);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	add(node, par, 1);
	ans.push_back(query(1, 1, n) - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1);
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], freq[N], cur = -1, n;
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	sz[node] = 1;
	depth[node] = dep;
	st.insert({0, dep});
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	freq[depth[node]] += type;
	if(type == 1) {
		if(freq[depth[node]] > freq[cur]) {
			cur = depth[node];
		} else if(freq[depth[node]] == freq[cur])
			cur = min(cur, depth[node]);
	}
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	if(cur == -1)
		cur = depth[node];
	add(node, par, 1);
	ans.push_back(cur - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1), cur = -1;
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], tree[4], n, freq[N];
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	auto it = st.find({-freq[depth[node]], depth[node]});
	if(it != st.end())
		st.erase(it);
	freq[depth[node]] += type;
	st.insert({-freq[depth[node]], depth[node]});
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	add(node, par, 1);
	ans.push_back((*st.begin()).second - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1);
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], freq[N], cur, mx, n;
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	st.insert({0, dep});
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	freq[depth[node]] += type;
	if(freq[depth[node]] > mx) {
		mx = freq[depth[node]];
		cur = depth[node];
	} else if(freq[depth[node]] == mx)
		cur = min(cur, depth[node]);
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	add(node, par, 1);
	ans.push_back(cur - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1);
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], tree[4*N], n;
bool big[N];

void update(int node, int start, int end, int idx, int val) {
	if(start == end) {
		tree[node] += val;
		return;
	}
	int mid = (start + end) / 2;
	if(idx <= mid)
		update(node * 2, start, mid, idx, val);
	else
		update(node * 2 + 1, mid + 1, end, idx, val);
	tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}

int query(int node, int start, int end) {
	if(start == end)
		return start;
	int mid = (start + end) / 2;
	if(tree[node * 2] >= tree[node * 2 + 1])
		return query(node * 2, start, mid);
	else
		return query(node * 2 + 1, mid + 1, end);
}

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int val) {
	update(1, 1, n, depth[node], val);
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, val);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	add(node, par, 1);
	ans.push_back(query(1, 1, n) - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1);
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], tree[4], n, freq[N];
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	st.insert({0, dep});
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	auto it = st.find({-freq[depth[node]], depth[node]});
	st.erase(it);
	if(type == -1)
		freq[depth[node]]--;
	else
		freq[depth[node]]++;
	st.insert({-freq[depth[node]], depth[node]});
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	add(node, par, 1);
	ans.push_back((*st.begin()).second - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1);
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], tree[4], n, freq[N];
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	auto it = st.find({-freq[depth[node]], depth[node]});
	if(it != st.end())
		st.erase(it);
	if(type == -1)
		freq[depth[node]] = 0;
	else
		freq[depth[node]]++;
	st.insert({-freq[depth[node]], depth[node]});
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	add(node, par, 1);
	ans.push_back((*st.begin()).second - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1);
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e6 + 5, M = 65, INF = 1e9, MOD = 1e9 + 7;

vector<int> adj[N];
int depth[N], sz[N], freq[N], cur = -1, mx, n;
bool big[N];
set<pair<int, int>> st;

void pre(int node, int par, int dep) {
	++sz[node];
	depth[node] = dep;
	st.insert({0, dep});
	for(auto it: adj[node]) {
		if(it == par) continue;
		pre(it, node, dep + 1);
		sz[node] += sz[it];
	}
}

void add(int node, int par, int type) {
	freq[depth[node]] += type;
	if(type == 1) {
		if(freq[depth[node]] > mx) {
			mx = freq[depth[node]];
			cur = depth[node];
		} else if(freq[depth[node]] == mx)
			cur = min(cur, depth[node]);
	}
	for(auto it: adj[node]) {
		if(it == par || big[it]) continue;
		add(it, node, type);
	}
}

vector<int> ans;
void dfs(int node, int par, bool keep) {
	int bigChild = -1;
	for(auto it: adj[node]) {
		if(it == par) continue;
		if(bigChild == -1 || sz[it] > sz[bigChild])
			bigChild = it;
	}
	for(auto it: adj[node]) {
		if(it == par || it == bigChild) continue;
		dfs(it, node, 0);
	}
	if(bigChild != -1)
		dfs(bigChild, node, 1), big[bigChild] = 1;
	if(cur == -1)
		cur = depth[node];
	add(node, par, 1);
	ans.push_back(cur - depth[node]);
	if(bigChild != -1)
		big[bigChild] = 0;
	if(!keep)
		add(node, par, -1), cur = -1;
}

void runtestcase() {
	int u, v;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pre(1, 1, 1);
	dfs(1, 1, 0);
	reverse(ans.begin(), ans.end());
	for(auto it: ans) cout << it << '\n';
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
//	freopen("output.in", "wt", stdout);
#endif
	int t = 1, tt = 1;
//	cin >> t;
	while(t) {
//		cout << "Case #" << tt << ':' << '\n';
		runtestcase();
//		cout << '\n';
		--t, ++tt;
	}
}


/*
By Tianyi Chen. All rights reserved.
Date: 2018-07-14
*/
#include<bits/stdc++.h>
using namespace std;
vector<int>e[1000001];
int n,s,t,ans[1000001],vis[1000001];
pair<vector<int>,int>dfs(int i){
 vis[i]=1;
 if(e[i].size()==1&&i!=1){
  return {{1},0};
 }
 vector<pair<vector<int>,int>>subs;
 int mid=0,ms=0,rti,rtv;
 for(auto&&x:e[i])if(!vis[x]){
  subs.emplace_back(dfs(x));
  if(subs.back().first.size()>ms){
   ms=subs.back().first.size();
   mid=subs.size()-1;rti=subs.back().second;
  }
 }
 auto&mainvec=subs[mid].first;
 rtv=mainvec[rti];
 for(int i=0;i<subs.size();++i)if(i!=mid){
  auto df=mainvec.size()-subs[i].first.size();
  for(int j=subs[i].first.size()-1;j>=0;--j){
   mainvec[df+j]+=subs[i].first[j];
   if(mainvec[df+j]>rtv){
    rtv=mainvec[df+j];
    rti=df+j;
   }
  }
 }
 ans[i]=mainvec.size()-rti;
 mainvec.push_back(1);
 if(rtv==1)ans[i]=0,rti=mainvec.size()-1;
 return {mainvec,rti};
}
int main(){
 scanf("%d",&n);
 for(int i=0;i<n-1;++i){
  scanf("%d%d",&s,&t);
  e[s].push_back(t);
  e[t].push_back(s);
 }
 dfs(1);
 for(int i=1;i<=n;++i)printf("%d\n",ans[i]);
}


/*
By Tianyi Chen. All rights reserved.
Date: 2018-07-14
*/
#include<bits/stdc++.h>
using namespace std;
vector<int>e[1000001];
int n,s,t,ans[1000001],vis[1000001];
pair<vector<int>,int>dfs(int i){
 vis[i]=1;
 if(e[i].size()==1&&i!=1){
  return {{1},0};
 }
 vector<pair<vector<int>,int>>subs;
 int mid=0,ms=0,rti,rtv;
 for(auto&&x:e[i])if(!vis[x]){
  subs.emplace_back(dfs(x));
  if(subs.back().first.size()>ms){
   ms=subs.back().first.size();
   mid=subs.size()-1;rti=subs.back().second;
  }
 }
 auto&mainvec=subs[mid].first;
 rtv=mainvec[rti];
 for(int i=0;i<subs.size();++i)if(i!=mid){
  auto df=mainvec.size()-subs[i].first.size();
  for(int j=subs[i].first.size()-1;j>=0;--j){
   mainvec[df+j]+=subs[i].first[j];
   if(mainvec[df+j]>rtv){
    rtv=mainvec[df+j];
    rti=df+j;
   }
  }
 }
 ans[i]=mainvec.size()-rti;
 mainvec.push_back(1);
 if(rtv==1)ans[i]=0,rti=mainvec.size();
 return {mainvec,rti};
}
int main(){
 scanf("%d",&n);
 for(int i=0;i<n-1;++i){
  scanf("%d%d",&s,&t);
  e[s].push_back(t);
  e[t].push_back(s);
 }
 dfs(1);
 for(int i=1;i<=n;++i)printf("%d\n",ans[i]);
}


/*
By Tianyi Chen. All rights reserved.
Date: 2018-07-15
*/
#include<bits/stdc++.h>
using namespace std;
vector<int>e[1000001];
int n,s,t,ans[1000001],vis[1000001];
pair<vector<int>,int>dfs(int i){
 vis[i]=1;
 if(e[i].size()==1&&i!=1){
  return {{1},0};
 }
 vector<pair<vector<int>,int>>subs;
 int mid=0,ms=0,rti,rtv;
 for(auto&&x:e[i])if(!vis[x]){
  subs.emplace_back(dfs(x));
  if(subs.back().first.size()>ms){
   ms=subs.back().first.size();
   mid=subs.size()-1;rti=subs.back().second;
  }
 }
 auto&mainvec=subs[mid].first;
 rtv=mainvec[rti];
 for(int i=0;i<subs.size();++i)if(i!=mid){
  auto df=mainvec.size()-subs[i].first.size();
  for(int j=0;j<subs[i].first.size();++j){
   if((mainvec[df+j]+=subs[i].first[j])>rtv||mainvec[df+j]==rtv&&df+j>rti)rtv=mainvec[rti=df+j];
  }
 }
 ans[i]=mainvec.size()-rti;
 mainvec.push_back(1);
 if(rtv==1)ans[i]=0,rti=mainvec.size()-1;
 return {move(mainvec),rti};
}
int main(){
 scanf("%d",&n);if(n==1)return puts("0"),0;
 for(int i=0;i<n-1;++i){
  s=i+1;t=i+2;
  e[s].push_back(t);
  e[t].push_back(s);
 }
 dfs(1);
 for(int i=1;i<=n;++i)printf("%d\n",ans[i]);
}


/*
By Tianyi Chen. All rights reserved.
Date: 2018-07-15
*/
#include<bits/stdc++.h>
using namespace std;
vector<int>e[1000001];
int n,s,t,ans[1000001],vis[1000001];
pair<vector<int>,int>dfs(int i){
 vis[i]=1;
 if(e[i].size()==1&&i!=1){
  return {{1},0};
 }
 vector<pair<vector<int>,int>>subs;
 int mid=0,ms=0,rti,rtv;
 for(auto&&x:e[i])if(!vis[x]){
  subs.emplace_back(dfs(x));
  if(subs.back().first.size()>ms){
   ms=subs.back().first.size();
   mid=subs.size()-1;rti=subs.back().second;
  }
 }
 auto&mainvec=subs[mid].first;
 rtv=mainvec[rti];
 for(int i=0;i<subs.size();++i)if(i!=mid){
  auto df=mainvec.size()-subs[i].first.size();
  for(int j=0;j<subs[i].first.size();++j){
   if((mainvec[df+j]+=subs[i].first[j])>rtv||mainvec[df+j]==rtv&&df+j>rti)rtv=mainvec[rti=df+j];
  }
 }
 ans[i]=mainvec.size()-rti;
 mainvec.push_back(1);
 if(rtv==1)ans[i]=0,rti=mainvec.size()-1;
 return {move(mainvec),rti};
}
int main(){
 scanf("%d",&n);if(n==1)return puts("0"),0;
 for(int i=0;i<n-1;++i){
  scanf("%d%d",&s,&t);
  e[s].push_back(t);
  e[t].push_back(s);
 }
 dfs(1);
 for(int i=1;i<=n;++i)printf("%d\n",ans[i]);
}


/*
By Tianyi Chen. All rights reserved.
Date: 2018-07-14
*/
#include<bits/stdc++.h>
using namespace std;
vector<int>e[1000001];
int n,s,t,ans[1000001],vis[1000001];
pair<vector<int>,int>dfs(int i){
 vis[i]=1;
 if(e[i].size()==1&&i!=1){
  return {{1},0};
 }
 vector<pair<vector<int>,int>>subs;
 int mid=0,ms=0,rti,rtv;
 for(auto&&x:e[i])if(!vis[x]){
  subs.emplace_back(dfs(x));
  if(subs.back().first.size()>ms){
   ms=subs.back().first.size();
   mid=subs.size()-1;rti=subs.back().second;
  }
 }
 auto&mainvec=subs[mid].first;
 rtv=mainvec[rti];
 for(int i=0;i<subs.size();++i)if(i!=mid){
  auto df=mainvec.size()-subs[i].first.size();
  for(int j=subs[i].first.size()-1;j>=0;--j){
   mainvec[df+j]+=subs[i].first[j];
   if(mainvec[df+j]>rtv){
    rtv=mainvec[df+j];
    rti=df+j;
   }
  }
 }
 ans[i]=mainvec.size()-rti;
 mainvec.push_back(1);
 if(rtv==1)ans[i]=0;
 return {mainvec,rti};
}
int main(){
 scanf("%d",&n);
 for(int i=0;i<n-1;++i){
  scanf("%d%d",&s,&t);
  e[s].push_back(t);
  e[t].push_back(s);
 }
 dfs(1);
 for(int i=1;i<=n;++i)printf("%d\n",ans[i]);
}


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

typedef long long ll;

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

const int maxn = 1e6 + 5;

int n;
vector<int> g[maxn];
map<int,int> bag[maxn]; //depth->freq
int depth[maxn];
pair<int,int> mx[maxn]; //(freq,-depth0). ans: depth0 - depth

void dfs(int at, int p, int d=0) {
  depth[at] = d;
  for (int to: g[at]) {
    if (to == p) continue;
    dfs(to, at, d+1);

    mx[at] = max(mx[at], mx[to]);

    if (bag[to].size() > bag[at].size()) {
      swap(bag[to], bag[at]);
    }
    for (auto p: bag[to]) {
      bag[at][p.first] += p.second;
      mx[at] = max(mx[at], make_pair(bag[at][p.first], -p.first));
    }
    g[to].clear(); //mle orz
  }

  bag[at][d]++;
  mx[at] = max(mx[at], make_pair(1, -d));
}

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

  cin>>n;
  for (int i=0; i<n-1; i++) {
    int u,v; cin>>u>>v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1,0);
  for (int i=1; i<=n; i++) {
    int ans = -mx[i].second - depth[i];
    cout<<ans<<"\n";
  }
  return 0;
}
// don't forget to read in the input :^)


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

typedef long long ll;

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

const int maxn = 1e6 + 5;

map<int,int> dp[maxn];
int n;
vector<int> g[maxn];
int ans[maxn];

void dfs(int at, int p) {
  dp[at][0] = 1;
  for (int to: g[at]) {
    if (to == p) continue;
    dfs(to, at);
    for (auto p: dp[to]) {
      dp[at][p.first+1] += p.second;
    }
  }

  // watch(at);
  // for (auto p: dp[at]) cout<<p.first<<": "<<p.second<<endl;
  int j=0;
  int hi = 0;
  for (auto p: dp[at]) {
    if (p.second > hi) {
      j = p.first;
    }
    hi = max(p.second, hi);
  }
  ans[at] = j;
}

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

  cin>>n;
  for (int i=0; i<n-1; i++) {
    int u,v; cin>>u>>v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1,0);
  for (int i=1; i<=n; i++) cout<<ans[i]<<"\n";
  return 0;
}
// don't forget to read in the input :^)


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

typedef long long ll;

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

const int maxn = 1e6 + 5;

map<int,int> dp[maxn];
int n;
vector<int> g[maxn];
int ans[maxn];

void dfs(int at, int p) {
  dp[at][0] = 1;
  for (int to: g[at]) {
    if (to == p) continue;
    dfs(to, at);
    for (auto p: dp[to]) {
      dp[at][p.first+1] += p.second;
    }
    dp[to].clear();
  }

  // watch(at);
  // for (auto p: dp[at]) cout<<p.first<<": "<<p.second<<endl;
  int j=0;
  int hi = 0;
  for (auto p: dp[at]) {
    if (p.second > hi) {
      j = p.first;
    }
    hi = max(p.second, hi);
  }
  ans[at] = j;
}

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

  cin>>n;
  for (int i=0; i<n-1; i++) {
    int u,v; cin>>u>>v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1,0);
  for (int i=1; i<=n; i++) cout<<ans[i]<<"\n";
  return 0;
}
// don't forget to read in the input :^)


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

typedef long long ll;

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

const int maxn = 1e6 + 5;

int n;
vector<int> g[maxn];
map<int,int> bag[maxn]; //depth->freq
int depth[maxn];
pair<int,int> mx[maxn]; //(freq,-depth0). ans: depth0 - depth

void dfs(int at, int p, int d=0) {
  depth[at] = d;
  for (int to: g[at]) {
    if (to == p) continue;
    dfs(to, at, d+1);

    mx[at] = max(mx[at], mx[to]);

    if (bag[to].size() > bag[at].size()) {
      swap(bag[to], bag[at]);
    }
    for (auto p: bag[to]) {
      bag[at][p.first] += p.second;
      mx[at] = max(mx[at], make_pair(bag[at][p.first], -p.first)); //negative to bias for smaller depth on ties.
    }
    g[to].clear(); //mle orz
  }

  bag[at][d]++;
  mx[at] = max(mx[at], make_pair(1, -d));
}

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

  cin>>n;
  for (int i=0; i<n-1; i++) {
    int u,v; cin>>u>>v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1,0);
  for (int i=1; i<=n; i++) {
    int ans = -mx[i].second - depth[i];
    cout<<ans<<"\n";
  }
  return 0;
}
// don't forget to read in the input :^)


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

typedef long long ll;

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

const int maxn = 1e6 + 5;

int n;
vector<int> g[maxn];
map<int,int> bag[maxn]; //depth->freq
int depth[maxn];
pair<int,int> mx[maxn]; //(freq,-depth0). ans: depth0 - depth

void dfs(int at, int p, int d=0) {
  depth[at] = d;
  for (int to: g[at]) {
    if (to == p) continue;
    dfs(to, at, d+1);

    mx[at] = max(mx[at], mx[to]);

    if (bag[to].size() > bag[at].size()) {
      swap(bag[to], bag[at]);
    }
    for (auto p: bag[to]) {
      bag[at][p.first] += p.second;
      mx[at] = max(mx[at], make_pair(bag[at][p.first], -p.first)); //negative to bias for smaller depth on ties.
    }
    //g[to].clear(); //mle orz
  }

  bag[at][d]++;
  mx[at] = max(mx[at], make_pair(1, -d));
}

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

  cin>>n;
  for (int i=0; i<n-1; i++) {
    int u,v; cin>>u>>v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1,0);
  for (int i=1; i<=n; i++) {
    int ans = -mx[i].second - depth[i];
    cout<<ans<<"\n";
  }
  return 0;
}
// don't forget to read in the input :^)


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int n;
int ans[maxn],dep[maxn];
map<int,int> f[maxn];
void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	ans[x]=dep[x];
	f[x][dep[x]]++;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		for(auto &j:f[i]){
			int deep=j.first;
			f[x][deep]+=j.second;
			if(f[x][deep]>f[x][ans[x]])
				ans[x]=deep;
			else if(f[x][deep]==f[x][ans[x]])
				ans[x]=min(ans[x],deep);
		}
		f[i].clear();
	}

}
int main(){
	cin>>n;

	//22
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}
	dep[0]=-1;
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",ans[i]-dep[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int n;
int ans[maxn],dep[maxn];
map<int,int> f[maxn];
void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	ans[x]=dep[x];
	f[x][dep[x]]++;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		for(auto &j:f[i]){
			int deep=j.first;
			f[x][deep]+=j.second;
			if(f[x][deep]>f[x][ans[x]])
				ans[x]=deep;
			else if(f[x][deep]>f[x][ans[x]])
				ans[x]=min(ans[x],deep);
		}
	}
}
int main(){
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}
	dep[0]=-1;
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",ans[i]-dep[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int n;
int ans[maxn],dep[maxn];
map<int,int> f[maxn];
void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	ans[x]=dep[x];
	f[x][dep[x]]++;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		for(auto &j:f[i]){
			int deep=j.first;
			f[x][deep]+=j.second;
			if(f[x][deep]>f[x][ans[x]])
				ans[x]=deep;
			else if(f[x][deep]>f[x][ans[x]])
				ans[x]=min(ans[x],deep);
		}
	}
}
int main(){
	cin>>n;

	//22
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}
	dep[0]=-1;
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",ans[i]-dep[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int n;
int ans[maxn],dep[maxn];
map<int,int> f[maxn];
void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	ans[x]=dep[x];
	f[x][dep[x]]++;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		for(auto &j:f[i]){
			int deep=j.first;
			f[x][deep]+=j.second;
			if(f[x][deep]>f[x][ans[x]])
				ans[x]=deep;
		}
	}
}
int main(){
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}
	dep[0]=-1;
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",ans[i]-dep[i]);
	}
	return 0;
}