#define ENABLE_DEBUG 0
// Kana's kitchen {{{
#include<bits/stdc++.h>
#define ALL(v) std::begin(v),std::end(v)
#define LOOP(k) for(lint ngtkana_is_a_genius=0; ngtkana_is_a_genius<(lint)k; ngtkana_is_a_genius++)
using lint=long long;
using ld=long double;
template<class T> using numr=std::numeric_limits<T>;
struct input_t {
    template<class T> operator T() {
        T t;
        std::cin>>t;
        return t;
    }
} input;
#ifdef NGTKANA
#include<debug.hpp>
#else
#define DEBUG(...)(void)0
#endif
/*}}}*/

// segtree {{{
/* APIs
 * - segtree(sz)
 * - size()
 * - fold(l, r)
 * - set(i, x)
 * - lazy_set(i, x)
 * - build()
 * - to_vec()
 */
template <class Monoid> struct segtree{/*{{{*/
    // a type alias
    using value_type = typename Monoid::value_type;

    // member variables
    std::vector<value_type>table;

    // trivial constructors
    segtree()=default;
    segtree(segtree const&)=default;
    segtree(segtree&&)=default;
    segtree&operator=(segtree const&)=default;
    segtree&operator=(segtree&&)=default;

    // a non-trivial constructor
    segtree(std::size_t n) : table(2*n, Monoid::id) {}

    // resize
    void resize(std::size_t n) {
        assert(table.empty());
        table.resize(2*n);
    }

    // size
    std::size_t size() const { return table.size() / 2; }

    // fold
    value_type fold(std::size_t l, std::size_t r) const {
        value_type fl = Monoid::id;
        value_type fr = Monoid::id;

        for(l+=size(),r+=size();l<r;l>>=1,r>>=1){
            if(l&1u) fl=Monoid::op(fl, table.at(l++));
            if(r&1u) fr=Monoid::op(fr, table.at(--r));
        }

        return Monoid::op(fl, fr);
    }

    // set
    void set(std::size_t i, value_type x) {
        assert(0 <= i);
        assert(i < size());

        i+=size();
        table.at(i) = x;

        for(i>>=1;i;i>>=1){ table.at(i) = Monoid::op(table.at(2*i), table.at(2*i+1)); }
    }

    // lazy_set
    void lazy_set(std::size_t i, value_type x) {
        assert(0 <= i);
        assert(i < size());

        i+=size();
        table.at(i) = x;
    }

    // map
    template <class F>
        void map(std::size_t i, F const& f) { set(i, f(at(i))); }

    // build
    void build() {
        for(std::size_t i=size()-1;i;i--){ table.at(i) = Monoid::op(table.at(2*i), table.at(2*i+1)); }
    }

    // at
    value_type at(std::size_t i) const { return table.at(size() + i); }

    // to_vec
    std::vector<value_type> to_vec() const {
        std::vector<value_type> ret(size());
        std::copy(table.begin()+size(), table.end(), ret.begin());
        return ret;
    }

};/*}}}*/
/*}}}*/
lint inf=std::numeric_limits<lint>::max();
// min-monoid{{{
template <class Value> struct min_monoid_t {
    using value_type = Value;
    static value_type op(value_type l, value_type r) { return std::min(l, r); }
    static const value_type id;
};
template <class Value> const typename min_monoid_t<Value>::value_type
min_monoid_t<Value>::id = {inf,inf};
/*}}}*/

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::setprecision(15) << std::fixed;

    lint n=input;
    std::vector<std::vector<lint>>g(n);
    for(lint i=0;i<n;i++){
        lint k=input;
        LOOP(k){
            lint j=input;
            g.at(i).push_back(j);
            g.at(j).push_back(i);
        }
    }
    DEBUG(matrix_style(g));

    std::vector<lint> ord, depth(n),in_pos(n);
    auto dfs=[&](auto&&f, lint x, lint p)->void {
        in_pos.at(x)=ord.size();
        ord.push_back(x);
        for(lint y:g.at(x)) if (y!=p) {
            depth.at(y)=depth.at(x)+1;
            f(f, y, x);
            ord.push_back(x);
        }
    };
    dfs(dfs, 0, 0);
    DEBUG(depth, ord, in_pos);
    assert((lint)ord.size()==2*n-1);

    auto seg=segtree<min_monoid_t<std::pair<lint,lint>>>(2*n-1);
    for(lint i=0;i<2*n-1;i++){
        seg.lazy_set(i, {depth.at(ord.at(i)), ord.at(i)});
    }
    seg.build();
    DEBUG(seg.to_vec());

    lint q=input;
    LOOP(q){
        lint u=input,v=input;
        if(u==v){
            std::cout<<u<<'\n';
            continue;
        }
        u=in_pos.at(u), v=in_pos.at(v);
        if (u>v) std::swap(u, v);
        auto f = seg.fold(u, v);
        DEBUG(u, v, f);
        std::cout << f.second << '\n';
    }
}




Measured memory_bytes: 21636