#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
struct edge {
  int x, y, z;
  edge(int a, int b, int c) {
    x = a;
    y = b;
    z = c;
  }
  bool operator<(edge a) const {
    return vector<int>{x, y, z} < vector<int>{a.x, a.y, a.z};
  }
};
int vp[511000][2];
vector<int> G[510000];
int now = 1;
void dfs(int p, int from) {
  for (int& adj : G[p])
    if (adj != from) dfs(adj, p);
  vp[p][0] = now++;
  auto& tv = G[p];
  for (auto i = tv.rbegin(); i != tv.rend(); i++) {
    int& tmp = *i;
    if (tmp != from) vp[tmp][1] = now++;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a = 1, b = 1;
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(1, 0);
  vp[1][1] = now;
  for (int i = 1; i <= n; i++) printf("%d %d\n", vp[i][0], vp[i][1]);
}
