#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v[500100];
int f[500100];
int e[500100];
int mm[500100];
vector<int> dd;
void go(int cr, int p, int o) {
  if (v[cr].size() == 1 && p != 0) {
    e[cr] = o;
    mm[cr] = e[cr];
    return;
  }
  int last;
  if (p != 0) o--;
  last = o + v[cr].size() + 1;
  o += v[cr].size() - 1;
  e[cr] = o + 1;
  for (int i = v[cr].size() - 1; i >= 0; i--) {
    if (p == v[cr][i]) continue;
    f[v[cr][i]] = o;
    go(v[cr][i], cr, last);
    last = mm[v[cr][i]] + 1;
    mm[cr] = max(mm[cr], mm[v[cr][i]]);
    o--;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  f[1] = 1;
  go(1, 0, 2);
  for (int i = 1; i <= n; i++) printf("%d %d \n", f[i], e[i]);
  return 0;
}
