#include <bits/stdc++.h>
#include "teleport.h"

signed main() {
    int c, n, m;
    std::cin >> c >> n >> m;

    std::vector<int> u(n), v(n), x(m), y(m);
    for (int i = 0; i < n - 1; i++) {
        std::cin >> u[i] >> v[i];
    }
    for (int i = 0; i < m; i++) {
        std::cin >> x[i] >> y[i];
    }

    std::vector<std::pair<long long, int>> a = teleport(c, n, m, u, v, x, y);

    if (static_cast<int>(a.size()) != m) {
        std::cout << "The length of a and b is not m.\n";
        return 0;
    }

    for (int i = 0; i < m; i++) {
        std::cout << a[i].first << ' ' << a[i].second << '\n';
    }
    return 0;
}
