#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 500000;
vector<int> adj[MAXN + 2];
int seg[MAXN + 2][2];
int k = 1;
void dfs(int u, int w) {
  for (int v : adj[u]) {
    if (v == w) continue;
    dfs(v, u);
  }
  seg[u][0] = k++;
  for (int i = adj[u].size() - 1; i >= 0; i--) {
    int v = adj[u][i];
    if (v == w) continue;
    seg[v][1] = k++;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  seg[1][1] = k;
  for (int i = 1; i <= n; i++) cout << seg[i][0] << ' ' << seg[i][1] << '\n';
}
