#include <bits/stdc++.h>

using namespace std;

mt19937 rng((long long) (new char));

int sol=(int) 1e9+7;
int op;
int lim;

int ask(int r,int c)
{
        op++;
        if (op==lim)
        {
                cout<<sol<<"\n";
                exit(0);
        }
        cout<<"? "<<r<<" "<<c<<endl;
        int x;
        cin>>x;
        sol=min(sol,x);
        return x;
}

void solve(int r1,int c1,int r2,int c2)
{
        if (r1>r2 || c1>c2)
        {
                return;
        }
        if (r1==r2&&c1==c2)
        {
                ask(r1,c1);
                return;
        }
        int rm=(r1+r2)/2,mn=(int) 1e9+7;
        vector<int> e;
        int same=0,ant=-1;
        for (int j=c1;j<=c2;j++)
        {
                int x=ask(rm,j);
                if (x==ant)
                {
                        same++;
                }
                else
                {
                        same=0;
                }
                mn=min(mn,x);
                e.push_back(x);
        }
        for (int j=c1;j<=c2;j++)
                if (e[j-c1]==mn)
                {
                        if (rng()&1)
                        {
                                solve(r1,c1,rm-1,j);
                                solve(rm+1,j,r2,c2);
                        }
                        else
                        {
                                solve(rm+1,j,r2,c2);
                                solve(r1,c1,rm-1,j);
                        }
                        return;
                }
}

int main()
{
        int n,m;
        cin>>n>>m;
        lim=4*(n+m);
        if ((ll)n*m<=lim)
        {
                for (int i=1;i<=n;i++)
                        for (int j=1;j<=m;j++)
                                ask(i,j);
        }
        else
                solve(1,1,n,m);
        cout<<"! "<<sol<<endl;
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl
#else
#define dbg(x) {}
#endif

struct ModIntRuntime {
    using lint = long long int;
    static int get_mod() { return mod; }
    int val;
    static int mod;
    static std::vector<ModIntRuntime> &facs() {
        static std::vector<ModIntRuntime> facs_;
        return facs_;
    }
    static int &get_primitive_root() {
        static int primitive_root_ = 0;
        if (!primitive_root_) {
            primitive_root_ = [&]() {
                std::set<int> fac;
                int v = mod - 1;
                for (lint i = 2; i * i <= v; i++)
                    while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < mod; g++) {
                    bool ok = true;
                    for (auto i : fac)
                        if (ModIntRuntime(g).power((mod - 1) / i) == 1) {
                            ok = false;
                            break;
                        }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root_;
    }
    static void set_mod(const int &m) {
        if (mod != m) facs().clear();
        mod = m;
        get_primitive_root() = 0;
    }
    ModIntRuntime &_setval(lint v) {
        val = (v >= mod ? v - mod : v);
        return *this;
    }
    ModIntRuntime() : val(0) {}
    ModIntRuntime(lint v) { _setval(v % mod + mod); }
    explicit operator bool() const { return val != 0; }
    ModIntRuntime operator+(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val + x.val); }
    ModIntRuntime operator-(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val - x.val + mod); }
    ModIntRuntime operator*(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val * x.val % mod); }
    ModIntRuntime operator/(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val * x.inv() % mod); }
    ModIntRuntime operator-() const { return ModIntRuntime()._setval(mod - val); }
    ModIntRuntime &operator+=(const ModIntRuntime &x) { return *this = *this + x; }
    ModIntRuntime &operator-=(const ModIntRuntime &x) { return *this = *this - x; }
    ModIntRuntime &operator*=(const ModIntRuntime &x) { return *this = *this * x; }
    ModIntRuntime &operator/=(const ModIntRuntime &x) { return *this = *this / x; }
    friend ModIntRuntime operator+(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod + x.val); }
    friend ModIntRuntime operator-(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod - x.val + mod); }
    friend ModIntRuntime operator*(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod * x.val % mod); }
    friend ModIntRuntime operator/(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod * x.inv() % mod); }
    bool operator==(const ModIntRuntime &x) const { return val == x.val; }
    bool operator!=(const ModIntRuntime &x) const { return val != x.val; }
    bool operator<(const ModIntRuntime &x) const { return val < x.val; }  // To use std::map<ModIntRuntime, T>
    friend std::istream &operator>>(std::istream &is, ModIntRuntime &x) {
        lint t;
        is >> t;
        x = ModIntRuntime(t);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const ModIntRuntime &x) {
        os << x.val;
        return os;
    }

    lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % mod;
            tmp = tmp * tmp % mod;
            n /= 2;
        }
        return ans;
    }
    ModIntRuntime pow(lint n) const { return power(n); }
    lint inv() const { return this->power(mod - 2); }
    ModIntRuntime operator^(lint n) const { return ModIntRuntime(this->power(n)); }
    ModIntRuntime &operator^=(lint n) { return *this = *this ^ n; }

    ModIntRuntime fac() const {
        int l0 = facs().size();
        if (l0 > this->val) return facs()[this->val];

        facs().resize(this->val + 1);
        for (int i = l0; i <= this->val; i++) facs()[i] = (i == 0 ? ModIntRuntime(1) : facs()[i - 1] * ModIntRuntime(i));
        return facs()[this->val];
    }

    ModIntRuntime doublefac() const {
        lint k = (this->val + 1) / 2;
        if (this->val & 1)
            return ModIntRuntime(k * 2).fac() / ModIntRuntime(2).power(k) / ModIntRuntime(k).fac();
        else
            return ModIntRuntime(k).fac() * ModIntRuntime(2).power(k);
    }

    ModIntRuntime nCr(const ModIntRuntime &r) const {
        if (this->val < r.val) return ModIntRuntime(0);
        return this->fac() / ((*this - r).fac() * r.fac());
    }

    ModIntRuntime sqrt() const {
        if (val == 0) return 0;
        if (mod == 2) return val;
        if (power((mod - 1) / 2) != 1) return 0;
        ModIntRuntime b = 1;
        while (b.power((mod - 1) / 2) == 1) b += 1;
        int e = 0, m = mod - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModIntRuntime x = power((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModIntRuntime z = b.power(m);
        while (y != 1) {
            int j = 0;
            ModIntRuntime t = y;
            while (t != 1) j++, t *= t;
            z = z.power(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModIntRuntime(std::min(x.val, mod - x.val));
    }
};
int ModIntRuntime::mod = 1;

using mint = ModIntRuntime;
int main() {
    int N, M, P;
    cin >> N >> M >> P;
    mint::set_mod(P);

    vector ncr(M + N + 1, vector<mint>(N + M + 1));
    REP(i, ncr.size()) REP(j, ncr[i].size()) ncr[i][j] = mint(i).nCr(j);

    vector<mint> nbsuc(M + 1);
    vector<mint> dpsuc(M + 1);
    nbsuc[0] = 1;
    FOR(n, 1, M + 1) {
        REP(l, n) {
            int r = n - 1 - l;
            nbsuc[n] += nbsuc[l] * nbsuc[r] * ncr[n - 1][r] * (n + 1);
            dpsuc[n] += ncr[n - 1][r] * ((dpsuc[l] * nbsuc[r] + nbsuc[l] * dpsuc[r]) * (n + 1) + nbsuc[l] * nbsuc[r] * (l * (l + 1) + r * (r + 1)) / 2);
        }
    }
    dbg(dpsuc);
    dbg(nbsuc);

    vector dp0(M + 1, vector<mint>(M + 1)); // dp[i][j] i人使ってjグループできる組合せ
    vector dpv(M + 1, vector<mint>(M + 1)); // dp[i][j] i人使ってjグループできたときのスコア合計
    dp0[0][0] = 1;
    REP(i, M) REP(j, i + 1) if (dp0[i][j]) {
        FOR(k, 1, M + 1 - i) {
            dp0[i + k][j + 1] += dp0[i][j] * ncr[M - i][k] * nbsuc[k];
            dpv[i + k][j + 1] += dpv[i][j] * ncr[M - i][k] * nbsuc[k] + dp0[i][j] * ncr[M - i][k] * dpsuc[k];
        }
    }
    mint ret = 0;
    FOR(j, 1, N + 1) {
        mint tmp = dpv[M][j];
        int yo = N - M - (j - 1);
        if (yo < 0) continue;
        ret += tmp * ncr[yo + j][j];
    }

    cout << ret << '\n';
}


#include <iostream>
#include <chrono>
#include <random>
#include <vector>
#include <set>

#define ll long long
#define ft first
#define sd second

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("fast-math")

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef FREOPEN
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    ll n , k;
    cin >> n >> k;
    vector <ll> c(n),ans(k+1),sum(k+1);

    for (ll i = 0; i < n; ++i) {
        cin >> c[i];
    }
    sort(c.begin(),c.end());
    reverse(c.begin(),c.end());
    set <pair<ll,ll>> s;
    for (ll i = 0; i <= k; ++i)
        s.insert({0,i});
    for (auto &i: c) {
        ll ind = s.begin()->sd;
        s.erase(s.begin());
        ans[ind] += sum[ind];
        sum[ind] += i;
        s.insert({-sum[ind],ind});
    }
    ll out = 0;
    for (ll i = 0; i <= k; ++i) {
        out += ans[i];
    }
    cout << out;
    return 0;
}


吾嘗觀「「算經」」之書。方悟「取底」之義。

吾有一術。名之曰「输入」。是術曰。
    施「require('fs').readFileSync」於「「/dev/stdin」」。名之曰「入」。
    施「(buf => buf.toString().trim())」於「入」。昔之「入」者。今其是矣。
    施「(s => s.split('\n'))」於「入」。昔之「入」者。今其是矣。
    乃得「入」也。
是謂「输入」之術也。

吾有一數。曰零。名之曰「其行」。
施「输入」。名之曰「初始输入」。

吾有一術。名之曰「得数」。是術曰。
    吾有一列。名之曰「其数」。
    加「其行」以一。昔之「其行」者。今其是矣。
    夫「初始输入」之「其行」。名之曰「此行」。
    施「(s => s.split(' '))」於「此行」。昔之「此行」者。今其是矣。
    夫「此行」之長。名之曰「长」。
    吾有一數。曰零。名之曰「下标」。
    為是「长」遍。
        加「下标」以一。昔之「下标」者。今其是矣。
        夫「此行」之「下标」。名之曰「此数」。
        施「parseInt」於「此数」。昔之「此数」者。今其是矣。
        銜「其数」以「此数」。昔之「其数」者。今其是矣。
    云云。
    乃得「其数」也。
是謂「得数」之術也。


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int value(char c)
{
    if('A'<c<'Z'){return (c-'A'+ 1);}
    if('a'<c<'z'){return -(c-'a'+ 1);}
}
int main()
{
    string s;
    int k=0;
    cin>>s;
    int n;
    for(n=0;;n++)
    {
        if(s[n]=='\0'){break;}
    }
    for(int i=0;i<n;i++)
    {
        k+=value(k[i]);
    }
    cout<<k;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef long double ld;
typedef pair<ll,ll> pll;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;

int n;
vector<pll> p, P, totest;
set<pll> z;
ll ans = 0;
pll sum;

pll operator+ (const pll& l, const pll& r) {
    return {l.f+r.f,l.s+r.s};
}

pll operator- (const pll& l, const pll& r) {
    return {l.f-r.f,l.s-r.s};
}

void process(pll x) {
    if (x.f == 0 && x.s == 0) return;
    ll g = __gcd(x.f,x.s);
    x.f /= g, x.s /= g;
    if (x.f < 0 || (x.f == 0 && x.s < 0)) x.f *= -1, x.s *= -1;
    totest.pb(x);
}

void test(pll t) {
    vector<__int128> cur;
    for (auto a: P) {
        cur.pb((__int128)a.f*t.s-(__int128)a.s*t.f);
    }
    sort(cur.begin(),cur.end());
    F0R(i,cur.size()) if (cur[i] != -cur[cur.size()-1-i]) return;
    ans++;
}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n; p.resize(n);
	F0R(i,n) {
	    cin >> p[i].f >> p[i].s;
	    sum = sum+p[i];
	    p[i].f *= n, p[i].s *= n;
	}
	F0R(i,n) {
	    p[i] = p[i]-sum;
	    if (z.count(mp(-p[i].f,-p[i].s))) z.erase(mp(-p[i].f,-p[i].s));
	    else z.insert(p[i]);
	}
	for (auto a: z) P.pb(a);
	if (P.size() <= 1) {
	    cout << -1;
	    return 0;
	}
    F0R(i,P.size()) FOR(j,i,P.size()) process(p[i]+p[j]);
    sort(totest.begin(),totest.end());
    F0R(i,totest.size()) {
        int i1 = i;
        while (i < totest.size() && totest[i] == totest[i1]) i++;
        if (2*(i-i1+1) >= P.size()) test(totest[i1]);
    }
    cout << ans;
}

// read!
// ll vs. int!


#include<bits/stdc++.h>

using namespace std;

#define pq priority_queue
#define MZ 1000000007

int n,kk,xx;
int cnt=0;
int n2b[75],b2n[265];
int bs[10];
long long dis[10];
int p[30];
long long dp[2][75];
map <int,long long> mp;
struct Mat
{
    long long num[75][75];
    friend Mat operator *(Mat x1,Mat x2)
    {
        Mat ans;
        for(int i=1;i<=cnt;i++)
            for(int j=1;j<=cnt;j++)
            {
                ans.num[i][j]=2LL*MZ*MZ;
                for(int k=1;k<=cnt;k++)
                    ans.num[i][j]=min(x1.num[i][k]+x2.num[k][j],ans.num[i][j]);
            }
        return ans;
    }
}A;
queue <int> q;

Mat pows(Mat x,int y)
{
    Mat ans;
    for(int i=1;i<=cnt;i++)
        for(int j=1;j<=cnt;j++)
            ans.num[i][j]=2LL*MZ*MZ;
    while(y)
    {
        if(y%2)
            ans=ans*x;
        x=x*x;
        y>>=1;
    }
    return ans;
}
void init(int f,int x,int s)
{
    if(f==kk)
    {
        if(x==xx)
        {
            cnt++;
            b2n[s]=cnt;
            n2b[cnt]=s;
        }
        return;
    }
    init(f+1,x+1,s+bs[f]);
    init(f+1,x,s);
}
void solve1(Mat x)
{
    for(int i=1;i<=cnt;i++)
        dp[1][i]=2LL*MZ*MZ;
    for(int i=1;i<=cnt;i++)
        for(int j=1;j<=cnt;j++)
            dp[1][j]=min(dp[0][i]+x.num[i][j],dp[1][j]);
    for(int i=1;i<=cnt;i++)
        dp[0][i]=dp[1][i];
}
void solve2(int y)
{
    for(int i=1;i<=cnt;i++)
        dp[1][i]=2LL*MZ*MZ;
    for(int i=1;i<=cnt;i++)
    {
        if(n2b[i]%2)
        {
            for(int j=1;j<=kk;j++)
            {
                if((n2b[i]&bs[j])==0)
                    dp[1][b2n[(n2b[i]+bs[j])/2]]=min(dp[1][b2n[(n2b[i]+bs[j])/2]],dp[0][i]+A.num[i][b2n[(n2b[i]+bs[j])/2]]+mp[y+j]);
            }
        }
        else
            dp[1][b2n[n2b[i]/2]]=min(dp[1][b2n[n2b[i]/2]],dp[0][i]);
    }
    for(int i=1;i<=cnt;i++)
        dp[0][i]=dp[1][i];
}

int main()
{
    int qq;
    long long w;
    bs[0]=1;
    for(int i=1;i<=8;i++)
        bs[i]=bs[i-1]*2;
    scanf("%d%d%d%d",&xx,&kk,&n,&qq);
    init(0,0,0);
    for(int i=1;i<=kk;i++)
        scanf("%lld",&dis[i]);
    for(int i=1;i<=cnt;i++)
    {
        for(int j=1;j<=cnt;j++)
            A.num[i][j]=2LL*MZ*MZ;
        if(n2b[i]%2)
        {
            for(int j=1;j<=kk;j++)
            {
                if((n2b[i]&bs[j])==0)
                    A.num[i][b2n[(n2b[i]+bs[j])/2]]=dis[j];
            }
        }
        else
            A.num[i][b2n[n2b[i]/2]]=0;
    }
    for(int i=0;i<qq;i++)
    {
        scanf("%d%lld",&p[i],&w);
        mp[p[i]]=w;
    }
    p[qq]=n-xx+1;
    int now=1;
    for(int i=2;i<=cnt;i++)
        dp[0][i]=2LL*MZ*MZ;
    /*
    for(int i=0;i<=qq;i++)
    {
        if(p[i]>p[qq])
            p[i]=p[qq];
        if(now+kk<p[i])
        {
            solve1(pows(A,p[i]-kk-now));
            now=p[i]-kk;
        }
        while(now<p[i])
        {
            solve2(now);
            now++;
        }
    }
    */
    while(now+xx<=n)
    {
        solve2(now);
        now++;
    }
    printf("%lld\n",dp[0][1]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef double db;
typedef complex<db> cp;

const db eps = 1e-12;

int n;
string s;

int main() {
	cin >> n >> s;
	cp S(0, 0);
	for (int i = 0; i < n; i ++)
		S += cp(cos(2*i*M_PI/n), sin(2*i*M_PI/n))*cp(s[i]-'0',0);
	if (fabs(S.real()) < eps && fabs(S.imag()) < eps) puts ("YES");
	else puts ("NO");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LD long double
#define SC(t,x) static_cast<t>(x)
#define AR(t) vector < t >
#define PII pair < int, int >
#define PLL pair < LL, LL >
#define PIL pair < int, LL >
#define PLI pair < LL, int >
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define FF first
#define SS second
#define INIT(ar,val) memset ( ar, val, sizeof ( ar ) )
#define lp(loop,start,end) for ( int loop = start; loop < end; ++loop )
#define lpd(loop,start,end) for ( int loop = start; loop > end; --loop )
#define lpi(loop,start,end) for ( int loop = start; loop <= end; ++loop )
#define lpdi(loop,start,end) for ( int loop = start; loop >= end; --loop )
#define qmax(a,b) (((a)>(b))?(a):(b))
#define qmin(a,b) (((a)<(b))?(a):(b))
#define qabs(a) (((a)>=0)?(a):(-(a)))

const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 50007;
int MAXS = 997;
const LD PI = acos ( -1.0 );
//const LD EPS = 1e-9;
const LD EPS = 0.015;
const LD RATIO = 0.95;

struct cT
{
	cT () : x ( 0 ), y ( 0 ) {}
	cT ( LD _x, LD _y ) : x ( _x ), y ( _y ) {}

	cT operator + ( const cT &a ) const
	{
		return cT ( x + a.x, y + a.y );
	}

	void operator += ( const cT &a )
	{
		( *this ) = ( *this ) + a;
	}

	cT operator - ( const cT &a ) const
	{
		return cT ( x - a.x, y - a.y );
	}

	void operator -= ( const cT &a )
	{
		( *this ) = ( *this ) - a;
	}

	LD ang ()
	{
		return atan2 ( y, x );
	}

	LD abs ()
	{
		return x * x + y * y;
	}

	LD len ()
	{
		return sqrt ( abs () );
	}

	cT rot ( LD ang )
	{
		LD ca = cos ( ang ), sa = sin ( ang );
		return cT ( x * ca - y * sa, x * sa + y * ca );
	}

	bool operator < ( const cT &a ) const
	{
		return x < a.x;
	}

	LD x, y;
}c1[MAXN], c2[MAXN], r2[MAXN];

int n, n1, n2;
pair < cT, int > s1[MAXN];
pair < LD, PII > s2[MAXN];
int ans2[MAXN];
bool vis1[MAXN];

void init ();
void input ();
void work ();

void check ( int a11, int a12, int a21, int a22 );

int main()
{
	init();
	input();
	work();
}

void init ()
{
	// Init Everything Here

	ios::sync_with_stdio ( false );
}

void input ()
{
	// input method

	scanf ( "%d", &n );
	scanf ( "%d", &n1 );
	double tx, ty;
	lpi ( i, 1, n1 ) scanf ( "%lf%lf", &tx, &ty ), c1[i].x = tx, c1[i].y = ty;
	scanf ( "%d", &n2 );
	lpi ( i, 1, n2 ) scanf ( "%lf%lf", &tx, &ty ), c2[i].x = tx, c2[i].y = ty;
}

void work ()
{
	// main work

	MAXS = qmin ( MAXS, n );

	lpi ( i, 1, n1 ) s1[i] = MP ( c1[i], i );
	sort ( s1+1, s1+1+n1 );

	int k2 = 0;
	lpi ( i, 1, MAXS ){
		lpi ( j, i+1, MAXS ){
			s2[++k2] = MP ( ( c2[j] - c2[i] ).len (), MP ( i, j ) );
		}
	}

	sort ( s2+1, s2+1+MAXS );
	LD nd;
	int np;
	lpi ( i, 1, MAXS ){
		lpi ( j, 1, MAXS ){
			if ( i ^ j ){
				nd = ( c1[j] - c1[i] ).len ();
				np = lower_bound ( s2+1, s2+1+k2, MP ( nd, MP ( 0, 0 ) ) ) - s2;
				lpi ( k, np, k2 ){
					if ( s2[k].FF - nd < EPS ) check ( i, j, s2[k].SS.FF, s2[k].SS.SS );
					else break;
				}
				lpd ( k, np-1, 0 ){
					if ( nd - s2[k].FF < EPS ) check ( i, j, s2[k].SS.FF, s2[k].SS.SS );
					else break;
				}
			}
		}
	}
}

void check ( int a11, int a12, int a21, int a22 )
{
	LD ang1 = ( c1[a12] - c1[a11] ).ang (), ang2 = ( c2[a22] - c2[a21] ).ang ();
	LD ang = ang1 - ang2;
	lpi ( i, 1, n2 ) r2[i] = c2[i].rot ( ang );
//	lpi ( i, 1, n2 ) cerr << "(" << r2[i].x << "," << r2[i].y << ") "; cerr << endl;
	cT d = c1[a11] - r2[a21];
	lpi ( i, 1, n2 ) r2[i] += d;

	INIT ( ans2, 0 );
	INIT ( vis1, 0 );

//	cerr << a11 << " " << a12 << " " << a21 << " " << a22 << endl;
//	cerr << ang << endl;
//	lpi ( i, 1, n2 ) cerr << "(" << r2[i].x << "," << r2[i].y << ") "; cerr << endl;

	int np, cc = 0;
	lpi ( i, 1, n2 ){
		np = lower_bound ( s1+1, s1+1+n1, MP ( r2[i], 0 ) ) - s1;
		lpi ( k, np, n1 ){
			if ( s1[k].FF.x - r2[i].x < EPS ){
				if ( qabs ( s1[k].FF.y - r2[i].y ) < EPS && !vis1[s1[k].SS] ){
					ans2[i] = s1[k].SS;
					vis1[s1[k].SS] = true;
					break;
				}
			}else break;
		}
		if ( !ans2[i] ){
			lpd ( k, np-1, 0 ){
				if ( r2[i].x - s1[k].FF.x < EPS ){
					if ( qabs ( s1[k].FF.y - r2[i].y ) < EPS && !vis1[s1[k].SS] ){
						ans2[i] = s1[k].SS;
						vis1[s1[k].SS] = true;
						break;
					}
				}else break;
			}
		}
		if ( ans2[i] ) ++cc;
	}

	if ( cc > RATIO * n ){
		int nc = 0;
		lpi ( i, 1, n2 ){
			if ( ans2[i] ){
				printf ( "%d %d\n", ans2[i], i );
				if ( ++nc == n ) break;
			}
		}
		if ( nc < n ){
			int i1 = 1;
			lpi ( i, 1, n2 ){
				if ( !ans2[i] ){
					while ( vis1[i1] ) ++i1;
					printf ( "%d %d\n", i1, i );
					vis1[i1] = true;
					if ( ++nc == n ) break;
				}
			}
		}
		exit ( 0 );
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-18
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned int uint;
#define MAX 300105
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"Yes" : "No");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    x+=y; if (x>=mod) x-=mod;
}
void et() {
    puts("-1"); exit(0);
}
 ll fastPow(ll x, ll y, int mod=MOD) {
     ll ans = 1;
     while(y>0) {
         if(y&1) ans = (x * ans)%mod;
         x = x*x%mod;
         y>>=1;
     }
     return ans;
 }

typedef ll ValType;
struct Point {
    ValType x, y;
    Point() { }
    Point(ValType x, ValType y) : x(x), y(y) { }
    ld abs() const { return hypot(x, y); }
    ld arg() const { return atan2(y, x); }
    Point operator*(double o) const { return Point(x * o, y * o); }
    Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); }
    Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); }
    bool operator<(const Point& o) const { return x < o.x - EPS || (x < o.x + EPS && y < o.y - EPS); }
    Point scale(double o) const { return *this * (o / abs()); }
    Point rotY() const { return Point(-y, x); }
    Point rotX() const { return Point(y, -x); }
    ValType cross(Point b) const {return x*b.y- b.x*y;}
    ValType dot(Point b) const {return x*b.x + y*b.y;}
    ld disToSeg(Point &a, Point &b) {
        return fabs(((*this)-a).cross(b-a) / (b-a).abs());
    }
    void readin() {
        scanf("%lld%lld", &x, &y);
    }
    void pp() {
        printf("%lld %lld\n",x,y);
    }
}p[MAX];

vector<Point> convex(vector<Point> &vp) {
    sort(vp.begin(), vp.end());
    int N = unique(vp.begin(),vp.end())-vp.begin();
    vector<Point> ans1,ans2;
    REP(i,N) {
        while(ans1.size()>1) {
            Point o=ans1[ans1.size()-2], a= ans1.back();
            if ((a-o).cross(vp[i]-o) <= 0) {
                ans1.pop_back();
            } else break;
        }
        ans1.push_back(vp[i]);
    }
    for(int i=N-1;i>=0;i--) {
        while(ans2.size()>1) {
            Point o=ans2[ans2.size()-2], a= ans2.back();
            if ((a-o).cross(vp[i]-o) <= 0) {
                ans2.pop_back();
            } else break;
        }
        ans2.push_back(vp[i]);
    }
    for(int i=1;i<ans2.size()-1;i++) ans1.push_back(ans2[i]);
    return ans1;
}

ll xl[MAX],xr[MAX],yl[MAX],yr[MAX];
bool isIn(ll x, ll y) {
    for(int i=0;i<=1;i++)
        for(int j=0;j<=1;j++)
            if(xl[y+j]>x+i||xr[y+j]<x+i||yl[x+i]>y+j||yr[x+i]<y+j)
                return 0;
    return 1;
}
void fmain() {
    REP(i,MAX) {
        xl[i]=infL; yl[i]=infL;
    }
    while (~scanf("%d%d", &n,&m)) {
        if (n==0) break;
        REQ(i,m) p[i].readin();
        REQ(i,m) {
            xl[p[i].y]=min(xl[p[i].y], p[i].x);
            xr[p[i].y]=max(xr[p[i].y], p[i].x);
            yl[p[i].x] = min(yl[p[i].x], p[i].y);
            yr[p[i].x] = max(yr[p[i].x], p[i].y);
        }
        vector<Point> vp;
        REQ(i,m) {
            ll x=p[i].x,y=p[i].y;
            if (isIn(x, y)) vp.push_back(Point(x,y));
            if (isIn(x-1, y)) vp.push_back(Point(x-1,y));
            if (isIn(x, y-1)) vp.push_back(Point(x,y-1));
            if (isIn(x-1, y-1)) vp.push_back(Point(x-1,y-1));
//            int tio=0;
        }
        vector<Point> ans = convex(vp);
        printf("%d\n", ans.size());
        ans[0].pp();
        for(int i=ans.size()-1;i>=1;i--) ans[i].pp();

        REQ(i,m) {
            xl[p[i].y]=infL;
            xr[p[i].y]=0;
            yl[p[i].x] = infL;
            yr[p[i].x] = 0;
        }
    }
}
int main() {
    fmain();
    return 0;
}


#include <bits/stdc++.h>
#include <string.h>

using namespace std;

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ld eps = 1e-12;
const ld inf = 1e9;
const int ops = 100;

bool lt(ld a, ld b)
{
    return b - a > eps;
}

bool eq(ld a, ld b)
{
    return abs(a - b) <= eps;
}

bool leq(ld a, ld b)
{
    return lt(a, b) || eq(a, b);
}

bool gt(ld a, ld b)
{
    return a - b > eps;
}

bool geq(ld a, ld b)
{
    return gt(a, b) || eq(a, b);
}

struct pt
{
    ld x, y;
    pt() {}
    pt(ld x, ld y) : x(x), y(y) {}
    pt operator-(const pt& other) const
    {
        return {x - other.x, y - other.y};
    }
    pt operator+(const pt& other) const
    {
        return {x + other.x, y + other.y};
    }
    pt operator*(ld k) const
    {
        return {x * k, y * k};
    }
};

bool eq(const pt& p, const pt& q)
{
    return eq(p.x, q.x) && eq(p.y, q.y);
}

bool is_zero(const pt& p)
{
    return eq(p.x, 0) && eq(p.y, 0);
}

ld cross(const pt& p, const pt& q)
{
    return p.x * q.y - p.y * q.x;
}

ld dot(const pt& p, const pt& q)
{
    return p.x * q.x + p.y * q.y;
}

ld area(const pt& a, const pt& b, const pt& c)
{
    return cross(b - a, c - a);
}

ld len(const pt& p)
{
    return sqrt(p.x * p.x + p.y * p.y);
}

bool inside(const pt& p, const vector<pt>& v)
{
    ld s = 0;
    ld ans = 0;
    int n = sz(v);
    for (int i = 0; i < n; ++i) {
        s += abs(area(p, v[i], v[(i + 1) % n]));
        ans += area(p, v[i], v[(i + 1) % n]);
    }
    return eq(s, abs(ans));
}

bool on_seg(const pt& p, const pt& a, const pt& b)
{
    return leq(dot(a - p, b - p), 0);
}

struct line
{
    ld a, b, c;
    line() {}
    line(const pt& p, const pt& q)
    {
        a = p.y - q.y;
        b = q.x - p.x;
        c = cross(p, q);
        /*ld tmp = sqrt(a * a + b * b);
        a /= tmp, b /= tmp, c /= tmp;*/
    }
};

bool on_line(const pt& p, const line& l, bool flag = false)
{
    if (flag) {
        cout << "ON " << l.a * p.x + l.b * p.y + l.c << "\n";
    }
    return eq(l.a * p.x + l.b * p.y + l.c, 0);
}

vector<pt> intersect(const line& l, const line& m)
{
    ld a1 = l.a, b1 = l.b, c1 = -l.c;
    ld a2 = m.a, b2 = m.b, c2 = -m.c;
    ld det = a1 * b2 - a2 * b1;
    if (eq(det, 0)) {
        return {};
    }
    ld det1 = c1 * b2 - c2 * b1;
    ld det2 = a1 * c2 - a2 * c1;
    return {{det1 / det, det2 / det}};
}

vector<pt> intersect(const pt& a, const pt& b, const pt& c, const pt& d)
{
    line l(a, b), m(c, d);
    vector<pt> v = intersect(l, m);
    vector<pt> ans;
    for (pt& p : v) {
        if (on_seg(p, a, b) && on_seg(p, c, d)) {
            ans.pb(p);
        }
    }
    if (on_line(a, m) && on_seg(a, c, d)) {
        ans.pb(a);
    }
    if (on_line(b, m) && on_seg(b, c, d)) {
        ans.pb(b);
    }
    if (on_line(c, l) && on_seg(c, a, b)) {
        ans.pb(c);
    }
    if (on_line(d, l) && on_seg(d, a, b)) {
        ans.pb(d);
    }
    return ans;
}

vector<pt> intersect(const pt& a, const pt& b, const line& l)
{
    line m(a, b);
    vector<pt> ans = intersect(l, m);
    if (sz(ans) == 1 && !on_seg(ans[0], a, b)) {
        ans.pop_back();
    }
    if (on_line(a, l)) {
        ans.pb(a);
    }
    if (on_line(b, l)) {
        ans.pb(b);
    }

    return ans;
}

int main()
{

    //freopen("input.txt", "r", stdin);
    //ifstream cin("input.txt");

    //ios_base::sync_with_stdio(false);
    //cin.tie(0);

    int n;
    cin >> n;
    cout << fixed;
    cout.precision(12);

    vector<pt> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i].x >> p[i].y;
    }

    pt a;
    cin >> a.x >> a.y;
    pt v;
    cin >> v.x >> v.y;
    ld z1;
    cin >> z1;

    ld fdown;
    cin >> fdown;
    fdown = -fdown;

    pt u;
    cin >> u.x >> u.y;
    ld z2;
    cin >> z2;
    z2 = -z2;

    if (is_zero(v) && is_zero(u)) {
        cout << "-1 -1\n";
        return 0;
    }

    ld t1_ans;

    if (!eq(cross(v, u), 0)) {
        ld lf = 0, rg = inf;
        for (int step = 0; step < ops; ++step) {
            ld t1 = (lf + rg) / 2.0;
            //cout << t1 << " : ";
            pt b = a + v * t1;
            pt c = b + u * (z1 * t1 / z2);
            vector<pt> triangle = {a, b, c};
            bool ok = false;
            for (int i = 0; i < n; ++i) {
                if (inside(p[i], triangle)) {
                    ok = true;
                    //cout << "ok p[" << i << "]\n";
                    break;
                }
            }
            if (!ok) {
                for (int i = 0; i < n; ++i) {
                    vector<pt> vv = intersect(a, b, p[i], p[(i + 1) % n]);
                    if (!vv.empty()) {
                        ok = true;
                        //cout << "ok ab " << i << " " << i + 1 << "\n";
                        break;
                    }
                    vv = intersect(b, c, p[i], p[(i + 1) % n]);
                    if (!vv.empty()) {
                        ok = true;
                        //cout << "ok bc " << i << " " << i + 1 << "\n";
                        break;
                    }
                    vv = intersect(c, a, p[i], p[(i + 1) % n]);
                    if (!vv.empty()) {
                        ok = true;
                        //cout << "ok ca " << i << " " << i + 1 << "\n";
                        break;
                    }
                }
            }
            if (ok) {
                rg = t1;
            } else {
                //cout << "not ok\n";
                lf = t1;
            }
        }
        t1_ans = rg;
    } else {
        ld t1 = inf;
        line l;
        if (is_zero(u)) {
            l = line(a, a + v);
        } else {
            l = line(a, a + u);
        }
        pt w = v + u * (z1 / z2);
        for (int i = 0; i < n; ++i) {
            vector<pt> vv = intersect(p[i], p[(i + 1) % n], l);
            for (pt& q : vv) {
                /*if (i == 2) {
                    cout << "!" << q.x << " " << q.y << "\n";
                }*/
                if (!is_zero(v)) {
                    ld val = len(q - a) / len(v);
                    /*if (i == 2) {
                        pt tmp = q - a;
                        cout << tmp.x << " " << tmp.y << "\n";
                    }*/
                    if (eq(v * val, q - a) && gt(t1, val)) {
                        t1 = val;
                    }
                }
                if (!is_zero(w)) {
                    ld val = len(q - a) / len(w);
                    if (eq(w * val, q - a) && gt(t1, val)) {
                        t1 = val;
                    }
                }
            }
        }
        t1_ans = t1;
    }

    if (eq(t1_ans, inf)) {
        cout << "-1 -1\n";
        return 0;
    }

    cout << t1_ans << " ";

    if (is_zero(u)) {
        cout << "0\n";
        return 0;
    }

    eps = 1e-8;

    pt magic = a + v * t1_ans;
    pt border = magic + u * (z1 * t1_ans / z2);
    //cout << border.x << " " << border.y << "\n";
    line l(magic, border);

    for (int i = 0; i < n; ++i) {
        if (on_line(p[i], l) && on_seg(p[i], magic, border)) {
            magic = p[i];
        }
        if (eq(magic, border)) {
            break;
        }
        vector<pt> vv = intersect(a + v * t1_ans, border, p[i], p[(i + 1) % n]);
        for (pt& q : vv) {
            //if (i == 0) cout << "!" << i << " " << q.x << " " << q.y << "\n";
            if (on_seg(q, magic, border)) {
                magic = q;
            }
            if (eq(magic, border)) {
                break;
            }
        }
    }

    t1_ans += eps;
    pt magic2 = a + v * t1_ans;
    pt border2 = a + v * t1_ans + u * (z1 * t1_ans / z2);

    for (int i = 0; i < n; ++i) {
        if (on_line(p[i], l) && on_seg(p[i], magic2, border2)) {
            magic2 = p[i];
        }
        if (eq(magic2, border2)) {
            break;
        }
        vector<pt> vv = intersect(a + v * t1_ans, border2, p[i], p[(i + 1) % n]);
        for (pt& q : vv) {
            //cout << "!" << i << " " << q.x << " " << q.y << "\n";
            if (on_seg(q, magic2, border2)) {
                magic2 = q;
            }
            if (eq(magic2, border2)) {
                break;
            }
        }
    }

    //cout << magic.x << " " << magic.y << "\n";

    ld val = len(magic - (a + v * t1_ans));
    ld val2 = len(magic2 - (a + v * t1_ans));
    if (lt(val, val2)) {
        val = val2;
    }

    ld par = val / len(u);
    ld ans = (z1 * t1_ans - par * z2) / fdown + eps;
    cout << ans << "\n";

}


#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tuple<int,int,int> iii;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef pair<char,char> cc;
typedef vector<pair<char,char>> vcc;

vector<set<int>> AL;
vi visited;

int main() {
    ios::sync_with_stdio(false); cin.tie(NULL);

    int tc; cin>>tc;
    while(tc--){
    	int n; cin>>n;
    	string a,b;
    	cin>>a>>b;
		set<pair<char,char>> setter;
		AL.assign(20, set<int>());
		bool can = true;
		for(int i=0; i<n; i++){
			if(a[i]!=b[i]){
				setter.insert(make_pair(a[i], b[i]));
			}
		}
		for(auto p:setter){
			AL[(int)p.first-'a'].insert((int)(p.second-'a'));
		}

//		for(int i=0; i<20; i++){
//			cout<<i<<": ";
//			for(auto v:AL[i]){
//				cout<<v<<" ";
//			}
//			cout<<endl;
//		}

		for(int i=0; i<20; i++){
			visited.assign(20,-1);
			visited[i] = 0;
			queue<ii> q; q.push(make_pair(i, 0));

			while(!q.empty()){
				int u = q.front().first, d = q.front().second;
				q.pop();
				for(auto v:AL[u]){
					if(visited[v] == 1){
						AL[i].erase(v);
					visited[v] = d+1;
					}
					if(visited[v] == -1){
						q.push(make_pair(v,d+1));
					visited[v] = d+1;
					}
				}
			}
		}

		int ans = 0;
		for(int i=0; i<20; i++){
			ans += AL[i].size();
		}

		cout<<ans<<endl;
	}

    return 0;
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;

#define int __int128

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double
#define ll __int128

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 1e6+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

int v[maxn],cnt,n;
struct _data {int x;ll t;}t[maxn];

#define ls p<<1
#define rs p<<1|1
#define mid ((l+r)/2)

struct ST_table {
    int f[50050][16],lg[50050];

    void build() {
        for(int i=1;i<=n;i++) f[i][0]=v[i];
        for(int j=1;j<=15;j++)
            for(int i=1;i<=n;i++)
                f[i][j]=__gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);
        for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
    }

    int query(int x,int y) {
        int t=lg[y-x+1];
        return __gcd(f[x][t],f[y-(1<<t)+1][t]);
    }
}T;

void prepare() {
    map<int,ll > s;
    T.build();
    for(int i=1;i<=n;i++) {
        int p=i,now=v[i];
        while(p<=n) {
            int l=p,r=n,pre=p;
            while(l<=r)
                if(T.query(i,mid)==now) p=mid,l=mid+1;
                else r=mid-1;
            s[now]+=p-pre+1;
            p++;now=__gcd(now,v[p]);
        }
    }
    for(map<int,ll > :: iterator i=s.begin();i!=s.end();i++)
        t[++cnt]=(_data){i->fr,i->sc};
}

ll f(ll a,ll b,ll c,ll n) {
    if(n<0) return 0;
    if(!a) return (n+1)*(b/c);
    if(a>=c||b>=c) return f(a%c,b%c,c,n)+n*(n+1)/2*(a/c)+(n+1)*(b/c);
    ll m=(n*a+b)/c;
    return n*m-f(c,c-b-1,a,m-1);
}

ll sum[maxn],ct[maxn];

ll s(ll a,ll b,ll c) {
    if(c<0) return 0;
    return f(a,c%a,b,c/a)+c/a+1;
}

ll calc(ll a,ll b,ll md) {
    if(a>=b) return 0;
    ll ss=md-(ct[b-1]-ct[a]),x=t[a].x,y=t[b].x;ss-=x,ss-=y;
    return s(x,y,ss)-s(x,y,ss-t[a].t*x)-s(x,y,ss-t[b].t*y)+s(x,y,ss-t[a].t*x-t[b].t*y);
}

ll get(ll x) {
    ll p=1,ans=0;
    for(int i=1;i<=cnt;i++) {
        ll w=min(x/t[i].x,t[i].t);
        ans+=(t[i].t+1)*w-w*(w+1)/2;
    }
    for(int i=1;i<=cnt;i++) {
        if(p>i+1) ans+=1ll*t[i].t*(sum[p-1]-sum[i]);
        while(p<=cnt&&ct[p]-ct[i]<=x) ans+=calc(i,p,x),p++;
        if(p<=cnt) ans+=calc(i,p,x);
    }
    // write(x);write(ans);
    return ans;
}

signed main() {
    int st=clock();
    read(n);
    for(int i=1;i<=n;i++) read(v[i]);
    prepare();
    for(int i=1;i<=cnt;i++) sum[i]=sum[i-1]+t[i].t;
    for(int i=1;i<=cnt;i++) ct[i]=ct[i-1]+1ll*t[i].t*t[i].x;//,printf("%d %d\n",t[i].x,t[i].t);
    ll l=1,r=ct[cnt]+1,w=1ll*n*(n+1)/2;
    w=w*(w+1)/2;w=(w+1)/2;write(cnt);
    while(l<r) {
        if(get(mid)<w) l=mid+1;
        else r=mid;
    }write(l);
    cerr<<(lf)(clock()-st)/1e3<<endl;
    return 0;
}


//Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 2000005
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/

int pen[N];

void update(int ind,int add) {
  while(ind<N) {
    pen[ind] += add;
    ind += ind&(-ind);
  }
}
int query(int ind) {
  int ret = 0;
  while(ind>0) {
    ret = ret + pen[ind];
    ind = ind&(ind-1);
  }
  return ret;
}

pair<int,int> r[N];
bool vis[N/2];

int getIndex(int y) {
  int sum = 0;
  int ind = 0;
  for(int i=20;i>=0;--i) {
    int nind = ind + (1<<i);
    int nsum = sum + pen[nind];
    if(nsum<y) {
      sum = nsum;
      ind = nind;
    }
  }
  return ind + 1;
}

int out[N];

void solve() {
  int n,m;
  scanf(" %d %d ", &n,&m);
  int pos = m + 1;
  for(int i=1;i<=n;++i) {
    r[pos+i] = {-1,i};
    update(pos+i,1);
  }
  for(int i=0;i<m;++i) {
    int x,y;
    scanf("%d %d ", &x,&y);
    int ind = getIndex(y);
    if(vis[x]) {
      if(r[ind].first!=x) {
        printf("-1\n");
        return;
      }
    }
    update(ind,-1);
    update(pos,1);
    r[pos] = r[ind];
    r[pos].first = x;
    vis[x] = true;
    --pos;
  }
  int p = 1;
  for(int i=1;i<=n;++i) {
    int ind = getIndex(i);
    if(r[ind].first==-1) {
      while(p<=n) {
        if(vis[p]) {
          ++p;
        } else {
          break;
        }
      }
      assert(p<=n);
      r[ind].first = p++;
    }
    out[r[ind].second] = r[ind].first;
  }

  for(int i=1;i<=n;++i) {
    printf("%d ", out[i]);
  }
}

int main() {
  //freopen("input.txt","r",stdin);
  solve();
  return 0;
}


//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1000*1007;

vector <int> drz[nax];
int roz[nax], jump[nax], pre[nax], post[nax], fad[nax], czas;
int gle[nax];
vi jumppo[nax];

void dfs_roz(int v, int oj=0) {
	jumppo[v].push_back(oj);
	while(jumppo[v].back())
	{
		int a=jumppo[v].back();
		int b=min(jumppo[v].size(), jumppo[a].size())-1;
		jumppo[v].push_back(jumppo[a][b]);
	}
	for (int &i : drz[v])
	{
		if (i==oj)
		{
			swap(i, drz[v].back());
			drz[v].pop_back();
			break;
		}
	}
	roz[v]=1;                        // drz[] ma nie zawierać krawędzi
	for (int &i : drz[v]) {          // do ojca.
		fad[i]=v;                    // Init:
		gle[i]=gle[v]+1;
		dfs_roz(i, v);                  // dfs_roz(root);
		roz[v]+=roz[i];              // dfs_pre(root);
		if (roz[i]>roz[drz[v][0]])   // Użycie get_path(v, u) zwróci
			swap(i, drz[v][0]);      // przedziały odpowiadające ścieżce
	}                                // z v do u. Przedziały odpowiadające
}                                    // ścieżce z v do lca mają
void dfs_pre(int v)                  // first>=second, zaś te dla ścieżki
{                                    // z lca do u mają first<=second.
	if (!jump[v])                    // Przedziały są po kolei.
		jump[v]=v;                   // Lca występuje w nich dwa razy,
	pre[v]=(++czas);                 // najpierw jako second,
	if (!drz[v].empty())             // a zaraz potem jako first.
		jump[drz[v][0]]=jump[v];
	for (int i : drz[v])
		dfs_pre(i);
	post[v]=czas;
}
int lca(int v, int u) {
	while(jump[v]!=jump[u]) {
		if (pre[v]<pre[u])
			swap(v, u);
		v=fad[jump[v]];
	}
	return (pre[v]<pre[u] ? v : u);
}
vector < pair <int,int> > path_up(int v, int u) {
	vector < pair <int,int> > ret;
	while(jump[v]!=jump[u]) {
		ret.push_back({pre[jump[v]], pre[v]});
		v=fad[jump[v]];
	}
	ret.push_back({pre[u], pre[v]});
	return ret;
}
vector < pair <int,int> > get_path(int v, int u) {
	int w=lca(v, u);
	auto ret=path_up(v, w);
	auto pom=path_up(u, w);
	for (auto &i : ret)
		swap(i.first, i.second);
	while(!pom.empty()) {
		ret.push_back(pom.back());
		pom.pop_back();
	}
	return ret;
}

int n, m, k;

vi graf[nax];

int kon[nax][2];

ll wyn;

int podnies(int v, int ile)
{
	for (int i=0; (1<<i)<=ile; i++)
		if (ile&(1<<i))
			v=jumppo[v][i];
	return v;
}

int liczodl(int a, int b)
{
	return gle[a]+gle[b]-2*gle[lca(a, b)];
}

vi sta[nax];
vi spo[nax];
ordered_set<pii> setel[nax];

int wprze(ordered_set<int> &secik, int a, int b)
{
	return secik.order_of_key(b+1)-secik.order_of_key(a);
}

int wprze(ordered_set<pii> &secik, int a, int b)
{
	return secik.order_of_key({b, nax})-secik.order_of_key({a, -nax});
}

map <pii, vi> mapka;

int dziecko(int ma, int du)
{
	return pre[ma]>=pre[du] && post[ma]<=post[du];
}

bool mniej(int a, int b)
{
	return pre[a]<pre[b];
}

vi ver;
vi ndrz[nax];
int korz;

void skompresuj(vi wek)
{
	sort(wek.begin(), wek.end(), mniej);
	int roz=wek.size();
	for (int i=0; i+1<roz; i++)
		wek.push_back(lca(wek[i], wek[i+1]));
	sort(wek.begin(), wek.end(), mniej);
	wek.resize(unique(wek.begin(), wek.end())-wek.begin());
	ver=wek;
	vi stos;
	for (int i : wek)
	{
		ndrz[i].clear();
		if (stos.empty())
		{
			korz=i;
		}
		else
		{
			while(!dziecko(i, stos.back()))
				stos.pop_back();
			ndrz[stos.back()].push_back(i);
		}
		stos.push_back(i);
	}
}

int tmp[nax];

int dfs2(int v, int conaj)
{
	int ret=0;
	while(tmp[v])
	{
		if (gle[v]>=conaj)
			wyn-=ret;
		tmp[v]--;
		ret++;
	}
	for (int i : ndrz[v])
	{
		int x=dfs2(i, conaj);
		if (gle[v]>=conaj)
			wyn-=ret*(ll)x;
		ret+=x;
	}
	return ret;
}

void liczmale(vi wek, int kt, int conaj)
{
	ll ret=0;
	vi wie;
	for (int i : wek)
		wie.push_back(kon[i][kt]);
	skompresuj(wie);
	for (int i : ver)
		tmp[i]=0;
	for (int i : wie)
		tmp[i]++;
	dfs2(korz, conaj);
}

vi tmpwek[nax];

ordered_set<pii> tmpset[nax];

void dfs3(int v, int koko)
{
	tmpset[v].clear();
	for (int i : tmpwek[v])
	{
		int tutaj=gle[v]-gle[koko];
		int tam=max(0, k-tutaj);
		int d=kon[i][1];
		if (gle[d]-gle[koko]>=tam)
		{
			int gor=podnies(d, gle[d]-gle[koko]-tam);
			wyn+=wprze(tmpset[v], pre[gor], post[gor]);
		}
		tmpset[v].insert({pre[d], i});
	}
	for (int i : ndrz[v])
	{
		dfs3(i, koko);
		if ((int)tmpset[i].size()>(int)tmpset[v].size())
			tmpset[v].swap(tmpset[i]);
		for (pii j : tmpset[i])
		{
			int tutaj=gle[v]-gle[koko];
			int tam=max(0, k-tutaj);
			int d=kon[j.second][1];
			if (gle[d]-gle[koko]>=tam)
			{
				int gor=podnies(d, gle[d]-gle[koko]-tam);
				wyn+=wprze(tmpset[v], pre[gor], post[gor]);
			}
		}
		for (pii j : tmpset[i])
			tmpset[v].insert(j);
	}
}

void solve(vi wek, int v, int lew, int pra)
{
	int r=wek.size();
	if (r<=1)
		return;
	for (int i : wek)
		if (pre[kon[i][0]]>pre[kon[i][1]])
			swap(kon[i][0], kon[i][1]);
	liczmale(wek, 0, gle[v]+k);
	liczmale(wek, 1, gle[v]+k);

	vi wsz{lew};
	for (int i : wek)
		wsz.push_back(kon[i][0]);
	skompresuj(wsz);
	for (int i : ver)
		tmpwek[i].clear();
	for (int i : wek)
		tmpwek[kon[i][0]].push_back(i);
	dfs3(lew, v);
}

void dfs1(int v)
{
	for (int i : sta[v])
		setel[v].insert({pre[v], i});
	for (int i : drz[v])
	{
		dfs1(i);
		if ((int)setel[v].size()>(int)setel[i].size())
			setel[v].swap(setel[i]);
		for (pii j : setel[i])
			setel[v].insert(j);
	}
	mapka.clear();
	for (int i : spo[v])
	{
		int a=kon[i][0];
		int b=kon[i][1];
		setel[v].erase({pre[a], i});
		setel[v].erase({pre[b], i});
		if (gle[a]>=gle[v]+k)
		{
			int lim=podnies(a, gle[a]-gle[v]-k);
			wyn+=wprze(setel[v], pre[lim], post[lim]);
		}
		if (gle[b]>=gle[v]+k)
		{
			int lim=podnies(b, gle[b]-gle[v]-k);
			wyn+=wprze(setel[v], pre[lim], post[lim]);
		}
		if (a==v || b==v)
			continue;
		a=podnies(a, gle[a]-gle[v]-1);
		b=podnies(b, gle[b]-gle[v]-1);
		if (pre[a]>pre[b])
			swap(a, b);
		mapka[{a, b}].push_back(i);
	}
	for (auto i : mapka)
		solve(i.second, v, i.first.first, i.first.second);
}

int main()
{
	scanf("%d%d%d", &n, &m, &k);
	for (int i=1; i<n; i++)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		graf[a].push_back(b);
		graf[b].push_back(a);
		drz[a].push_back(b);
		drz[b].push_back(a);
	}
	gle[1]=1;
	dfs_roz(1);
	dfs_pre(1);
	for (int i=1; i<=m; i++)
	{
		for (int j=0; j<2; j++)
			scanf("%d", &kon[i][j]);
		if (liczodl(kon[i][0], kon[i][1])<k)
		{
			i--;
			m--;
		}
	}
	debug() << imie(m);
	for (int i=1; i<=m; i++)
	{
		for (int j=0; j<2; j++)
			sta[kon[i][j]].push_back(i);
		spo[lca(kon[i][0], kon[i][1])].push_back(i);
	}
	dfs1(1);

	printf("%lld\n", wyn);
	return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=262144<<1;
const int P=998244353;
const int g[]={3,(P+1)/3};

int rev[MAXN],n;
ll tp[MAXN],itp[MAXN],tp2[MAXN],itp2[MAXN];
ll _G_[2][MAXN],G[MAXN],G0[MAXN],G1[MAXN],G2[MAXN],X1[MAXN],X3[MAXN],X4[MAXN],F0[MAXN],F1[MAXN],F2[MAXN];

constexpr ll Add(ll a,ll b){a+=b;return a>=P?a-P:a;}

constexpr ll Sub(ll a,ll b){a-=b;return a<0?a+P:a;}

void Inc(ll &a,ll b){a+=b;if(a>=P) a-=P;}

void Dec(ll &a,ll b){a-=b;if(a<0) a+=P;}

ll ksm(ll a,ll b)
{
	ll ret=1;
	for(;b;b>>=1,(a*=a)%=P) if(b&1) (ret*=a)%=P;
	return ret;
}

int init(int n)
{
	int tot=1,lg2=0;
	while(tot<=n) tot<<=1,lg2++;
	for(int i=0;i<tot;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(lg2-1));
	return tot;
}

void NTT(ll *a,int n,int f)
{
	ll *ed=a+n;
	for(int i=0;i<n;i++)
		if(i>rev[i]) swap(a[i],a[rev[i]]);
	for(int i=2;i<=n;i<<=1)
		for(ll *x=a,*y=a+(i>>1);x!=ed;x+=i,y+=i)
			for(int k=0;k<(i>>1);k++)
			{
				ll u=x[k],t=y[k]*_G_[f][k*MAXN/i]%P;
				x[k]=Add(u,t);y[k]=Sub(u,t);
			}
	if(f) for(int i=0,inv=ksm(n,P-2);i<n;i++) a[i]=a[i]*inv%P;
}

void Inv(ll *a,ll *Res,int n)
{
    if(n==1) return Res[0]=ksm(a[0],P-2),void();
    Inv(a,Res,(n+1)>>1);
    int tot=init(n<<1),lst=(n+1)>>1;
    static ll t1[MAXN],t2[MAXN];
    memset(t1,0,sizeof(ll)*tot);memset(t2,0,sizeof(ll)*tot);
    for(int i=0;i<n;i++) t1[i]=a[i];
    for(int i=0;i<lst;i++) t2[i]=Res[i];
    NTT(t1,tot,0);NTT(t2,tot,0);
    for(int i=0;i<tot;i++) t1[i]=Sub(2*t2[i],t2[i]*t2[i]%P*t1[i]%P);
    NTT(t1,tot,1);
    for(int i=0;i<n;i++) Res[i]=t1[i];
}

int main()
{
	_G_[0][0]=_G_[1][0]=1;
	_G_[0][1]=ksm(g[0],(P-1)/MAXN);_G_[1][1]=ksm(g[1],(P-1)/MAXN);
	for(int i=2;i<MAXN;i++) _G_[0][i]=_G_[0][i-1]*_G_[0][1]%P,_G_[1][i]=_G_[1][i-1]*_G_[1][1]%P;
	scanf("%d",&n);G[0]=1;
	int tot=init(4*n);//notice
	for(int i=2;i<=n;i++) G[i]=Add(G[i-2],i>3?G[i-4]:0);
	for(int i=0;i<=n;i++) G0[i]=G[i]*i%P*i%P,G1[i]=G[i]*(i+1)%P*(i+1)%P,G2[i]=G[i]*(i+2)%P*(i+2)%P;
	X1[1]=1;X3[3]=1;X4[4]=1;
	NTT(G0,tot,0);NTT(G1,tot,0);NTT(G2,tot,0);NTT(X1,tot,0);NTT(X3,tot,0);NTT(X4,tot,0);
	for(int i=0;i<tot;i++) tp[i]=Sub(Add(X4[i]*G2[i]%P*G0[i]%P,1),Add(X1[i]*G0[i]%P,Add(X3[i]*G2[i]%P,X4[i]*G1[i]%P*G1[i]%P)));
	for(int i=0;i<tot;i++) tp2[i]=Sub(1,X3[i]*G2[i]%P);
	NTT(tp2,tot,1);Inv(tp2,itp2,n);
	init(4*n);NTT(itp2,tot,0);
	NTT(tp,tot,1);Inv(tp,itp,n);
	init(4*n);NTT(itp,tot,0);
	for(int i=0;i<tot;i++) F0[i]=Add(G0[i]*Sub(1,X3[i]*G2[i]%P)%P,X3[i]*G1[i]%P*G1[i]%P)*itp[i]%P;
	for(int i=0;i<tot;i++) F1[i]=Add(G1[i]*Sub(1,X1[i]*G0[i]%P)%P,X1[i]*G1[i]%P*G0[i]%P)*itp[i]%P;
	for(int i=0;i<tot;i++) F2[i]=Add(G2[i],X1[i]*F1[i]%P*G1[i]%P)*itp2[i]%P;
	NTT(F0,tot,1);NTT(F1,tot,1);NTT(F2,tot,1);
	ll Ans=Add(G[n-1],G[n-3])*(n-1)%P*(n-1)%P*n%P;
	for(int i=2;i<=n-2;i++)
	{
		Inc(Ans,G[i-1]*(i-1)%P*(i-1)%P*F0[n-i-1]%P*i%P);
		Inc(Ans,G[i-2]*(i-1)%P*(i-1)%P*F1[n-i-2]%P*i%P*2%P);
		if(i>=3&&i<=n-3) Inc(Ans,G[i-3]*(i-1)%P*(i-1)%P*F2[n-i-3]%P*i%P);
	}
	printf("%lld\n",Ans);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
    ll sum;
    ll asum;
};
int a[3005];
vector<int>v[3005];
node dp[3005][3005];
node tmp[3005];
int dfs(int past,int now)
{
    int sz=1;
    dp[now][0].sum=0;
    dp[now][0].asum=a[now];
    for(auto u:v[now]){
        if(u==past)continue;
        int l=dfs(now,u);
        for(int i=0;i<l+sz;i++){
            tmp[i].sum=-1;
            tmp[i].asum=-1;
        }
        for(int i=0;i<=l;i++){
            for(int j=0;j<sz;j++){
                if(tmp[i+j].sum<dp[u][i].sum+dp[now][j].sum){
                    tmp[i+j].sum=dp[u][i].sum+dp[now][j].sum;
                    tmp[i+j].asum=dp[u][i].asum+dp[now][j].asum;
                }else if(tmp[i+j].sum==dp[u][i].sum+dp[now][j].sum&&tmp[i+j].asum<dp[u][i].asum+dp[now][j].asum){
                    tmp[i+j].sum=dp[u][i].sum+dp[now][j].sum;
                    tmp[i+j].asum=dp[u][i].asum+dp[now][j].asum;
                }
            }
        }
        for(int i=0;i<l+sz;i++){
            dp[now][i]=tmp[i];
        }
        sz+=l;
    }
    for(int i=sz-1;i>=0;i--){
        node x=dp[now][i];
        if(x.asum>0){
            x.sum++;
        }
        x.asum=0;
        if(dp[now][i+1].sum<x.sum||dp[now][i+1].sum==x.sum&&dp[now][i+1].asum<x.asum){
            dp[now][i+1]=x;
        }
    }
    return sz;
}
void work()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        v[i].clear();
    }
    for(int i=1;i<=n;i++){
        int k;
        scanf("%d",&k);
        a[i]=k-a[i];
    }
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<=n;j++){
            dp[i][j].sum=-1;
            dp[i][j].asum=-1;
        }
    }
    dfs(0,1);
    printf("%lld\n",dp[1][m-1].sum+(dp[1][m-1].asum>0));(dp[1][m]的asum小于0时是不成立的，所以最后不直接输出dp[1][m])
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int T;
    scanf("%d",&T);
    //cin>>T;
    //T=1;
    while(T--){
        work();
    }
}


// =========== Template ========== //
#include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }
int __fastio = fastio();
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p);
template<typename A> ostream& operator<<(ostream &os, const set<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A> ostream& operator<<(ostream &os, const multiset<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e.first << ": " << e.second, sep = ", "; return os << "}"; }
template<typename T>ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; string sep = ""; for (auto e: v) os << sep << e, sep = ", "; return os << "]"; }
template<typename T, size_t L>ostream& operator<<(ostream& os, const array<T, L>& v) { os << "["; string sep = ""; for (int i = 0; i < L; ++i) os << sep << v[i], sep = ", "; return os << "]"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef STEVEN
#define pp(...) cerr << "\t[" << #__VA_ARGS__ << "]:\t", dbg_out(__VA_ARGS__)
#define ppp(x) cerr << x << "\n"
#else
#define pp(...)
#define ppp(...)
#endif
template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } };
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
#define yc y_combinator
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
#define fi first
#define se second
#define sz(x) (int((x).size()))
#define data _data
// =========== Book ============ //

template <int MOD_> struct modnum {
   static constexpr int MOD = MOD_;
   static_assert(MOD_ > 0, "MOD must be positive");
   private:
   using ll = long long;
   int v;
   static int minv(int a, int m) {
      a %= m;
      assert(a);
      return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
   }
   public:
   modnum() : v(0) {}
   modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
   explicit operator int() const { return v; }
   friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
   friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }
   friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
   friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
   modnum inv() const {
      modnum res;
      res.v = minv(v, MOD);
      return res;
   }
   friend modnum inv(const modnum& m) { return m.inv(); }
   modnum neg() const {
      modnum res;
      res.v = v ? MOD-v : 0;
      return res;
   }
   friend modnum neg(const modnum& m) { return m.neg(); }
   modnum operator- () const {
      return neg();
   }
   modnum operator+ () const {
      return modnum(*this);
   }
   modnum& operator ++ () {
      v ++;
      if (v == MOD) v = 0;
      return *this;
   }
   modnum& operator -- () {
      if (v == 0) v = MOD;
      v --;
      return *this;
   }
   modnum& operator += (const modnum& o) {
      v += o.v;
      if (v >= MOD) v -= MOD;
      return *this;
   }
   modnum& operator -= (const modnum& o) {
      v -= o.v;
      if (v < 0) v += MOD;
      return *this;
   }
   modnum& operator *= (const modnum& o) {
      v = int(ll(v) * ll(o.v) % MOD);
      return *this;
   }
   modnum& operator /= (const modnum& o) {
      return *this *= o.inv();
   }
   friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
   friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
   friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
   friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
   friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
   friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

// ============= Solution ============= //

int main() {
   using mint = modnum<998244353>;
   int n;
   cin >> n;
   vector<mint> f(n + 1), g(n + 1);
   g[0] = 1;
   auto chs = [&](mint a, int k) {
      mint res = 1;
      for (int i = 1; i <= k; ++i) {
         res *= a - i + 1;
         res /= i;
      }
      return res;
   };
   for (int i = 1; i <= n; ++i) {
      g[i] = chs(g[i - 1] + 2, 2);
      f[i] = chs(g[i - 1] + 3, 3) - g[i - 1] - 1;
   }
   mint ans = 2 * (f[n] - f[n - 1]);

   mint sm = 0;
   vector<mint> h(n), hh(n);
   h[0] = g[0];
   for (int i = 1; i <= n; ++i) {
      h[i] = g[i] - g[i - 1];
   }
   hh[0] = h[0];
   for (int i = 1; i <= n; ++i) {
      hh[i] = h[i] - h[i - 1];
   }

   for (int i = n - 1; i >= 0; --i) {
      sm += hh[n - 1 - i];
      ans += sm * hh[i];
   }
   cout << ans << "\n";
}

// ============== Notes =============== //
// common bugs
// - fastio
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN?)
// - special cases (n=1? graph not connected?)
// - re-initialize efficiently between test cases


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;

void read(int &x){
    static char c;int f=0;
    while(!isdigit(c=getchar()))
        if(c^45) f=1;
    x=c^48;
    while(isdigit(c=getchar())) x=(x*10)+(c^48);
    if(f) x=-x;
}
void read(ll &x){
    static char c;int f=0;
    while(!isdigit(c=getchar()))
    if(c^45) f=1;
    x=c^48;
    while(isdigit(c=getchar())) x=(x*10)+(c^48);
}
void read(uint &x){
    static char c;
    while(!isdigit(c=getchar()));x=c^48;
    while(isdigit(c=getchar())) x=(x*10)+(c^48);
}
void read(ull &x){
    static char c;
    while(!isdigit(c=getchar()));x=c^48;
    while(isdigit(c=getchar())) x=(x*10)+(c^48);
}
void chkmax(int &x,int y){if(y>x)x=y;}
void chkmin(int &x,int y){if(y<x)x=y;}
void chkmax(ll &x,ll y){if(y>x)x=y;}
void chkmin(ll &x,ll y){if(y<x)x=y;}
void chkmax(uint &x,uint y){if(y>x)x=y;}
void chkmin(uint &x,uint y){if(y<x)x=y;}
void chkmax(ull &x,ull y){if(y>x)x=y;}
void chkmin(ull &x,ull y){if(y<x)x=y;}

const int N=5e3+10,Mod=998244353;
void Add(int &x,int y){(x+=y)>=Mod?x-=Mod:x;}

int C[N][N];
void Prework(){
	const int U=5e3;
	for(int i=0;i<=U;++i) C[i][0]=1;
	for(int i=1;i<=U;++i)
		for(int j=1;j<=i;++j)
			C[i][j]=C[i-1][j],Add(C[i][j],C[i-1][j-1]);
}

int n,m;

int Ans;

void Work(){
	Prework();
	read(n),read(m);
	if(n==1||m==1) return puts("0");
	for(int d=1;d<n;++d){
		int s=0;
		for(int L=m-1;L;--L){
			if(d==n-1) Add(s,m-L);
			else Add(s,1ll*C[n-d-1+L][n-d-1]*C[m-L+n-d-1][n-d]%Mod);
			if(d==1) Add(Ans,1ll*s*L%Mod);
			else Add(Ans,1ll*s*C[L+d-1][d]%Mod*C[m-L+d-1][d-1]%Mod);
		}
	}
	swap(n,m);
	for(int d=1;d<n;++d){
		int s=0;
		for(int L=m-2;L;--L){
			if(d==n-1) Add(s,m-L-1);
			else Add(s,1ll*C[n-d+L][n-d-1]*C[m-L+n-d-2][n-d]%Mod);
			if(d==1) Add(Ans,1ll*s*L%Mod);
			else Add(Ans,1ll*s*C[L+d-1][d]%Mod*C[m-L+d-1][d-1]%Mod);
		}
	}
	Add(Ans,Ans);
	printf("%d\n",Ans);
}

int main(){Work();}


#define _CRT_SECURE_NO_WARNINGS
#define pb push_back

#include <iostream>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <stack>
#include <set>
#include <cstdio>
#include <cctype>
#include <queue>
#include <bitset>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define int long long

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
typedef double db;

const ll N = 1e6 + 20;
const ll INF = INT_MAX;

/*int p = 3571;
int mod = 2147483647;

namespace op {
	void add(int &a, int val) {
		a = ((a * p % mod + val) % mod + mod) % mod;
	}
	int calc(int a, int b) {
		return ((a % mod + b % mod) % mod + mod) % mod;
	}
	int mul(int a, int b) {
		return (((a % mod) * (b % mod)) % mod + mod) % mod;
	}
	int pow(int a, int n) {
		if (n == 0) return 1LL;
		if (n & 1) {
			int b = pow(a, n ^ 1) % mod;
			return mul(a, b);
		}
		else {
			int b = pow(a, n >> 1) % mod;
			return mul(b, b);
		}
	}
};
using namespace op;

struct hsh {
	string vec;
	vector<int> seg;
	int n;
	hsh(string a) {
		n = a.size();
		vec = a;
		seg.resize(a.size() + 1);
		for (int i = 0; i < a.size(); ++i) {
			seg[i + 1] = seg[i];
			add(seg[i + 1], a[i]);
		}
	}
	int seg_hash(int l, int r) {
		return calc(seg[r], (-1) * seg[l - 1] * pow(p, r - l + 1));
	}
};*/

vector<int> g[N];
int used[N], ct[N];

vector<int> comp[2];

int id;
void dfs(ll v) {
	comp[id].push_back(v);
	used[v] = 1;

	for (ll to : g[v]) {
		if (!used[to])
			dfs(to);
	}
}

void clear(int n) {
	for (ll i = 0; i < n; ++i) {
		g[i].clear();
		used[i] = ct[i] = 0;
	}
}

signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#else
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int t;
	cin >> t;
	while (t--) {
		int n, m, a, b;
		cin >> n >> m >> a >> b;
		a--; b--;
		for (int i = 0; i < m; ++i) {
			int x, y;
			cin >> x >> y;
			--x; --y;
			g[x].push_back(y);
			g[y].push_back(x);
		}

		id = 0; used[b] = 1;
		dfs(a);
		for (auto &x : comp[0]) {
			used[x] = 0;
		}
		used[b] = 0;
		id = 1; used[a] = 1;
		dfs(b);
		for (auto &x : comp[0])
			ct[x]++;
		for (auto &x : comp[1])
			ct[x]++;

		int arr[2] = { -1,-1 };
		for (int i = 0; i < 2; ++i)
			for (auto &x : comp[i])
				if (ct[x] == 1)
					arr[i]++;

		cout << max(0ll, arr[0] * arr[1]) << '\n';
		clear(n);
	}

	return 0;
}


#pramga GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    int x=0,f=1;
    char ch = 0;
    while(!isdigit(ch)) {if(ch=='-') f=-1; ch = getchar();}
    while(isdigit(ch)) {x = x*10+ch-'0'; ch = getchar();}
    return x*f;
}
//typedef __int128 ll;
typedef long long ll;
const int mod = 1e9+7;
const int maxn = 1e6+100;
int T,m,x;
ll n;
int wei[70],cnt;
//ll dp[70][8];

ll qpow(ll x,ll p)
{
    ll ret = 1;
    while(p)
    {
        if(p&1) ret = ret*x%mod;
        x = x*x%mod;
        p >>= 1;
    }
    return ret;
}
int main()
{
    //T = read();
    //ll _2 = qpow(2,mod-2);
    const int _2 = 500000004;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while(T--)
    {
        //n = read();
        //scanf("%lld",&n);
        cin >> n;
        //BigInteger ans = BigInteger(n);
        //cout << ans << "---\n";
        //ll ans = (ans/2+2+(n%4>=2))*(ans/4+1)/2;
        //cout << (n%mod*_2+2+(n%4>=2))%mod << "-----" << (n/4%mod+1) <<endl;
        //ll ans = (n%mod*_2+2+(n%4>=2))%mod*(n%mod*_4%mod+1)*_2%mod;
        //ll ans = (n/2+2+(n%4>=2))%mod*(n/4%mod+1)%mod*_2%mod;
        cout << (n/2%mod+2+(n%4>=2))*(n/4%mod+1)%mod*_2%mod << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define de() cout<<"test"<<"\n"
using namespace std;

const ll maxn=3e6+10;
const ll mod=1e9+7;
ll n,l1,l2,s1,s2,S1,S2,N,r[maxn];
ll mul[maxn],inv[maxn],q1[maxn],q2[maxn],ans,er[maxn];
char a[maxn],b[maxn];
ll limit=1,l;
ll poww(ll x,ll k) {
	ll ans=1;
	while(k) {
		if(k&1) ans=ans*x%mod;
		x=x*x%mod;
		k>>=1;
	}return ans;
}
ll C(ll a,ll b) {
	return mul[a]*inv[b]%mod*inv[a-b]%mod;
}
ll gcd(ll a,ll b) {
	return b==0?a:gcd(b,a%b);
}
ll wo(ll a,ll b) {
	if(1ll*a*b>0) return 0;a=abs(a);b=abs(b);
	if(a==0&&b==0) return er[n]*er[n]%mod;
	if(a==0||b==0) return 0;
	if(a>b) swap(a,b);
	ll qwq=n/b;
	return er[qwq];
}
const double Pi=acos(-1.0);
struct com
{
	long double x,y;com(long double xx=0,long double yy=0):x(xx),y(yy){}
}A[maxn],B[maxn];
inline ll read() {ll data=0,f=1;char c=getchar();while('0'>c||c>'9') {if(c=='-') f=-1;c=getchar();}while('0'<=c&&c<='9') {data=data*10+c-'0';c=getchar();}return data;}
com operator * (com A,com B) {
	return com(A.x*B.x-A.y*B.y,A.x*B.y+A.y*B.x);
}
com operator + (com A,com B) {
	return com(A.x+B.x,A.y+B.y);
}
com operator - (com A,com B) {
	return com(A.x-B.x,A.y-B.y);
}
void fft(com *A,ll type)
{
	for(ll i=0;i<limit;++i) if(i<r[i]) swap(A[i],A[r[i]]);
	for(ll mid=1;mid<limit;mid<<=1) {
		com Wn(cos(Pi/mid),type*sin(Pi/mid));
		for(ll R=mid<<1,j=0;j<limit;j+=R) {
			com w(1,0);
			for(ll k=0;k<mid;++k,w=w*Wn) {
				com x=A[j+k],y=w*A[j+k+mid];
				A[j+k]=x+y;A[j+k+mid]=x-y;
			}
		}
	}
}
int main()
{
	scanf("%s",a+1);scanf("%s",b+1);er[0]=1;
	scanf("%d",&n);mul[0]=1;l1=strlen(a+1);l2=strlen(b+1);N=max(l1,l2);
	for(ll i=1;i<=N;++i) mul[i]=mul[i-1]*i%mod;
	for(ll i=1;i<=n;++i) er[i]=er[i-1]*2%mod;
	er[0]=0;
	for(ll i=1;i<=n;++i) er[i]=(er[i]+er[i-1])%mod;
	inv[N]=poww(mul[N],mod-2);
	for(ll i=N;i;i--) inv[i-1]=inv[i]*i%mod;
	for(ll i=1;i<=N;++i) {
		if(a[i]=='A') S1++;if(b[i]=='A') S2++;
		if(a[i]=='?') s1++;if(b[i]=='?') s2++;
	}
	for(ll i=S1;i<=S1+s1;++i) q1[i]=C(s1,i-S1);
	for(ll i=S2;i<=S2+s2;++i) q2[i]=C(s2,i-S2);
	for(ll i=S1;i<=S1+s1;++i) A[i].x=q1[i];
	for(ll i=S2;i<=S2+s2;++i) {
		B[l2-i].x=q2[i];
	}
	while(limit<=l1+l2) limit<<=1,l++;
	for(ll i=0;i<=limit;++i) r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
	fft(A,1);fft(B,1);for(ll i=0;i<limit;++i) A[i]=A[i]*B[i];fft(A,-1);
	for(ll i=max(0ll,S1-(S2+s2)+l2);i<=S1+s1-S2+l2;++i) {
		ll x=i-l2;
		ans=(ans+1ll*((unsigned long long)(A[i].x/limit+0.5)%mod*wo(x,l1-l2-x)%mod))%mod;//cout<<ans<<"\n";
	}
/*	ans=0;
	for(ll i=S1;i<=S1+s1;++i) {
		for(ll j=S2;j<=S2+s2;++j) {
			cout<<j<<":"<<q2[j]<<"*\n";
			ans=(ans+1ll*q1[i]*q2[j]%mod*wo(i-j,(l1-l2-(i-j)))%mod)%mod;
		}
	}*/
	cout<<ans<<"\n";
	return 0;
}
/*

*/


#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<time.h>
using namespace std;
const long long inf=1e18;
long long ex[15];
int low[15],high[15];
int num_state;
char str[16];
int len,stk[15][15],cnt_stk[15],sum[15],bs[15];
int ve[1<<14],vis[15],times,list[1<<14],cnt_list,f[1<<14],oe[1<<14],bn[15];
long long dp[1<<14][16],ans;
long long gs[1<<14],g[1<<14];
int fir[1<<14],gm[1<<14],ls[15],cnt_ls,adj[16];
long long cg[16];
int change(char ch)
{
	if(ch<='9'&&ch>='0')
	   return ch-'0';
    return ch-'a'+10;
}
int main()
{
	int i,j,s,p,q;
    start=clock();
	for(i=0;i<=14;i++)
	{
		if(i==0)
		   ex[i]=1;
        else
           ex[i]=ex[i-1]*16;
	}
	scanf("%s",str);
	len=strlen(str);
	memset(cnt_stk,0,sizeof(cnt_stk));
	int nc=0,tot=0;
	for(i=0;i<len;i++)
	   oe[1<<i]=i;
    ans=inf;
    for(i=(1<<len)-1;i>=0;i--)
    {
    	if(i&1)
    	    continue;
	    int sum=0;
	    for(j=0;j<len;j++)
	       bs[j]=change(str[j]);
	    for(j=len-1;j>=0;j--)
	    {
    		if((!(i&(1<<j))))
    		{
		    	low[j]=0;
		    	high[j]=15-bs[j];
		    }
		    else
		    {
		    	bs[j-1]++;
    			low[j]=16-bs[j];
    			high[j]=15;
    		}
    		sum=(sum+bs[j])%16;
    	}
    	bool ok=(sum==0);
    	if(ok)
    	{
	    	for(j=0;j<len;j++)
	    	{
	    		if(low[j]>high[j])
	    		   ok=false;
	    	}
	    }
		if(ok)
    	{
    		if(ans<inf)
    		{
    		    for(j=0;j<len;j++)
    	    	{
		        	if(low[j]!=(ans/ex[len-1-j])%16)
		        	    break;
		        }
		        if(j>=len||low[j]>(ans/ex[len-1-j])%16)
		           continue;
    		}
 	         cnt_list=0;
 	         for(j=0;j<(1<<len);j++)
 	         {
 	         	   if(j)
 	         	   {
					    f[j]=(f[j^(j&(-j))]+bs[oe[j&-j]])%16;
 	         	        if(f[j]==0)
 	         	           list[cnt_list++]=j;
    			   }
         	 }
         	 for(j=0;j<cnt_list;j++)
         	 {
 	         	for(s=j+1;s<cnt_list;s++)
 	         	{
	         	     if((list[j]&list[s])==list[j])
					      break;
         	    }
         	    if(s<cnt_list)
         	    {
    	         	for(s=j+1;s<cnt_list;s++)
    	         	   list[s-1]=list[s];
 	         	    cnt_list--;
    	        }
 	         }
 	         times++;
 	         for(s=0;s<cnt_list;s++)
 	            ve[list[s]]=times;
         	 for(j=0;j<(1<<len);j++)
         	 {
			      g[j]=inf;
         	      for(s=0;s<16;s++)
         	         dp[j][s]=inf;
			  }
			 for(j=0;j<len;j++)
		     {
		          for(s=low[j];s<=high[j];s++)
		          	   dp[1<<j][(s+bs[j])%16]=s*ex[len-1-j];
			 }
 	         for(j=0;j<16;j++)
 	         {
 	         	adj[j]=0;
         	 	for(s=0;s<len;s++)
         	 	{
	 	         	if(j>=low[s]&&j<=high[s])
	 	         	   adj[j]|=(1<<s);
	 	         }
         	 }
			 for(j=0;j<(1<<len);j++)
		     {
		     	  for(s=0;s<16;s++)
		     	  {
  	     			  if(dp[j][s]>=ans)
						continue;
				      int state=((1<<len)-1-j)&adj[s];
				      for(;state>0;state-=(state&-state))
				      {
      					   q=oe[state&-state];
							 dp[j|(1<<q)][(s+bs[q])%16]=min(dp[j|(1<<q)][(s+bs[q])%16],dp[j][s]+s*ex[len-1-q]);
				   	  }
  	     		  }
     		 }
     		 for(j=0;j<(1<<len);j++)
     		 {
     		 	gs[j]=inf;
 		     	for(s=0;s<16;s++)
 		     	{
	     		 	if(gs[j]>dp[j][s])
	     		 	   gs[j]=dp[j][s];
	     		 }
 		     }
     		 g[0]=0;
     		 for(j=0;j<(1<<len);j++)
     		 {
     		 	  if(g[j]==inf)
     		 	      continue;
 		     	   int state=(1<<len)-1-j;
 		     	   for(s=state;;s=(s-1)&state)
 		     	   {
   	     		 	     if(ve[s]==times)
   	     		 	     {
     	 		     	     g[j|s]=min(g[j|s],g[j]+gs[s]);
 		     	         }
 		     	         if(s==0)
 		     	            break;
   	     		   }
 		     }
 		     if(ans>g[(1<<len)-1])
 		        ans=g[(1<<len)-1];
             tot++;
		}
    }
    if(ans<inf)
    {
       for(i=0;i<len;i++)
       {
        	int vl=(ans/ex[len-1-i])%16;
        	if(vl<10)
        	   printf("%d",vl);
 	        else
 	          putchar(vl-10+'a');
        }
        printf("\n");
    }
    else
       puts("NO");
	return 0;
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define outvec(A) printf(#A" = ");for (auto __v : A) printf("%d,",__v);puts("");
using namespace std;
typedef long long LL;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f|=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int N=10005;
int n;
map <int,int> mpa[N],mpb[N];
vector <int> a[N],b[N];
int p[N];
void Getp(vector <int> &A,vector <int> &B){
	static int ga[10][10],gb[10][10],ya[N],yb[N];
	clr(ga),clr(gb);
	memset(ya,-1,sizeof ya);
	memset(yb,-1,sizeof yb);
	int m=A.size(),cnt=1;
	for (int i=1;i<=m;i++)
		cnt*=i;
	for (int i=0;i<m;i++)
		ya[A[i]]=i;
	for (int i=0;i<m;i++)
		yb[B[i]]=i;
	for (int i=0;i<m;i++){
		int x=A[i];
		for (auto y : a[x])
			if (ya[y]!=-1)
				ga[i][ya[y]]=1;
	}
	for (int i=0;i<m;i++){
		int x=B[i];
		for (auto y : b[x])
			if (yb[y]!=-1)
				gb[i][yb[y]]=1;
	}
	vector <int> id(0);
	for (int i=0;i<m;i++)
		id.push_back(i);
	while (cnt--){
		int flag=1;
		for (int i=0;i<m&&flag;i++)
			for (int j=0;j<m&&flag;j++)
				if (ga[i][j]&&gb[id[i]][id[j]])
					flag=0;
		if (flag){
			for (int i=0;i<m;i++)
				p[A[i]]=B[id[i]];
			return;
		}
		next_permutation(id.begin(),id.end());
	}
	assert(0);
}
void vec_remove(vector <int> &v,int u){
	int flag=0;
	for (int i=0;i<(int)v.size();i++)
		if (v[i]==u){
			swap(v[i],v.back());
			v.pop_back();
			flag=1;
		}
	assert(flag);
}
int solve(){
	vector <int> A(0),B(0);
	int ra=0,rb=0;
	for (int i=1;i<=n;i++){
		if (!a[i].empty())
			A.push_back(i);
		if (!b[i].empty())
			B.push_back(i);
		if (a[i].size()>a[ra].size())
			ra=i;
		if (b[i].size()>b[rb].size())
			rb=i;
	}
	int m=A.size();
//	printf("ra = %d, rb = %d\n",ra,rb);
//	outvec(A);outvec(B);
	if ((int)a[ra].size()+1==m||(int)b[rb].size()+1==m)
		return 0;
	if (m<=5)
		return Getp(A,B),1;
	if ((int)a[ra].size()+1==m-1){
		int ai=0;
		for (int i=0;i<m&&!ai;i++)
			if (a[A[i]].size()==1)
				ai=A[i];
		int bi=0;
		for (int i=0;i<m&&!bi;i++)
			if (b[B[i]].size()==1)
				bi=B[i];
		p[ra]=bi;
		assert(!b[bi].empty());
		p[ai]=b[bi][0];
		vec_remove(A,ra);
		vec_remove(A,ai);
		vec_remove(B,bi);
		vec_remove(B,b[bi][0]);
		for (int i=0;i<m-2;i++)
			p[A[i]]=B[i];
		return 1;
	}
	if ((int)b[rb].size()+1==m-1){
		int ai=0;
		for (int i=0;i<m&&!ai;i++)
			if (a[A[i]].size()==1)
				ai=A[i];
		int bi=0;
		for (int i=0;i<m&&!bi;i++)
			if (b[B[i]].size()==1)
				bi=B[i];
		p[ai]=rb;
		assert(!a[ai].empty());
		p[a[ai][0]]=bi;
		vec_remove(B,rb);
		vec_remove(B,bi);
		vec_remove(A,ai);
		vec_remove(A,a[ai][0]);
		for (int i=0;i<m-2;i++)
			p[A[i]]=B[i];
		return 1;
	}
//	puts("dec");
	int xa=0,ya=0,xb=0,yb=0;
	int xza=0,yza=0,xzb=0,yzb=0;
	for (int i=0;i<m&&!xa;i++)
		if (a[A[i]].size()==1&&a[A[i]][0]!=ra)
			xa=A[i];
//	puts("---1");
	for (int i=0;i<m;i++)
		if (a[A[i]].size()==1&&(a[A[i]][0]!=ra||!ya)&&a[A[i]][0]!=a[xa][0])
			ya=A[i];
//	puts("---2");
	for (int i=0;i<m&&!xb;i++)
		if (b[B[i]].size()==1&&b[B[i]][0]!=rb)
			xb=B[i];
//	puts("---3");
	for (int i=0;i<m;i++)
		if (b[B[i]].size()==1&&(b[B[i]][0]!=rb||!yb)&&b[B[i]][0]!=b[xb][0])
			yb=B[i];
//	puts("---4");
//	printf("xa = %d, ya = %d, xb = %d, yb = %d\n",xa,ya,xb,yb);
	xza=a[xa][0];
	yza=a[ya][0];
	xzb=b[xb][0];
	yzb=b[yb][0];
//	printf("xza = %d, yza = %d, xzb = %d, yzb = %d\n",xza,yza,xzb,yzb);
//	puts("---5");
	#define rm vec_remove
	#define rme(a,x,y) rm(a[x],y),rm(a[y],x)
//	puts("rme - IN");
	rme(a,xa,xza);
	rme(a,ya,yza);
	rme(b,xb,xzb);
	rme(b,yb,yzb);
//	puts("rme - OUT");
	#undef rme
	#undef rm
	int res=solve();
	if (!res)
		return 0;
//	for (int i=1;i<=n;i++)
//		printf("p[%d] = %d, ",i,p[i]);puts("");
	if ((p[xza]==xzb&&mpa[xa][xza]&&mpb[xb][xzb])||(p[yza]==yzb&&mpa[ya][yza]&&mpb[yb][yzb]))
		swap(xb,yb),swap(xzb,yzb);
	assert(!(p[xza]==xzb&&mpa[xa][xza]&&mpb[xb][xzb])&&!(p[yza]==yzb&&mpa[ya][yza]&&mpb[yb][yzb]));
	p[xa]=xb,p[ya]=yb;
	return 1;
}
int main(){
	n=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		a[x].push_back(y);
		a[y].push_back(x);
		mpa[x][y]=mpa[y][x]=1;
	}
	for (int i=1;i<n;i++){
		int x=read()-n,y=read()-n;
		b[x].push_back(y);
		b[y].push_back(x);
		mpb[x][y]=mpb[y][x]=1;
	}
	int res=solve();
	if (!res)
		puts("No");
	else {
		puts("Yes");
		for (int i=1;i<=n;i++)
			printf("%d ",p[i]+n);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define N 10010

int k, n;

const double ep = 1e-8;
const double pi = 3.14159265358979323846264338327950288L;
struct pnt {
    double x, y;
    pnt(double x = 0, double y = 0) : x(x), y(y) {}
    pnt operator + (const pnt &b) {
        return pnt(x + b.x, y + b.y);
    }
    pnt operator - (const pnt &b) {
        return pnt(x - b.x, y - b.y);
    }
    pnt operator * (const double &b) {
        return pnt(x * b, y * b);
    }
    pnt operator / (const double &b) {
        return pnt(x / b, y / b);
    }
    double operator & (const pnt &b) {
        return x * b.x + y * b.y;
    }
    double operator ^ (const pnt &b) {
        return x * b.y - b.x * y;
    }
    pnt rot() {
        return pnt(-y, x);
    }
    friend double dist(pnt a) {
        return sqrt(a.x * a.x + a.y * a.y);
    };
    void input() {
        scanf("%lf %lf", &x, &y);
    }
    void print() {
        printf("%.12lf %.12lf ", x, y);
    }
} p[N];

pnt outc(pnt a, pnt b, pnt c) {
    return (a + b + (b - a).rot() * (a - c & b - c) / (a - c ^ b - c)) / 2;
}

bool vis[N];

double del;

bool check(pnt O, double r, vector <int> &rlt) {
    if (dist(O) > 1000100 || r < 240000 || r > 760000) return 0;
    int cnt = 0; vector <int> cur;
    for (int i = 0; i < n * k; i ++) {
        if (vis[i]) continue;
        double d = dist(O - p[i]);
        if (0.9 * r <= d && d <= 1.1 * r) cnt ++, cur.push_back(i);
    }
    //if (cnt < 0.6 * n) return 0;
    if (cnt < del * n) return 0;
//    for (int i = 0; i < n * k; i ++) {
//        if (vis[i]) continue;
//        double d = dist(O - p[i]);
//        if (0.9 * r <= d && d <= 1.1 * r) cout << d << ' ' << r << endl;
//    }
    random_shuffle(cur.begin(), cur.end()); rlt.clear();
    for (int i = 0; i < cnt && i < n; i ++) vis[cur[i]] = 1, rlt.push_back(cur[i]);
    return 1;
}

vector <pnt> ans;
vector <double> ans_r;

bool solve(int cc) {
    if (cc == k) return 1;
    vector <int> vec;
    for (int j = 0; j < n * k && vec.size() < 14; j ++) if (!vis[j]) vec.push_back(j);
    int sz = vec.size();
    for (int a = 0; a < sz; a ++) {
        for (int b = 0; b < a; b ++) {
            for (int c = 0; c < b; c ++) {
                pnt O = outc(p[vec[a]], p[vec[b]], p[vec[c]]);
                for (int x = -0; x <= 0; x ++) {
                    for (int y = -0; y <= 0; y ++) {
                        pnt P = O; P.x += x, P.y += y;
                        double r = dist(P - p[vec[a]]);
                        vector <int> vec;
                        if (check(P, r, vec)) {
                            ans[cc] = P, ans_r[cc] = r;
                            if (solve(cc + 1)) return 1;
                            int sz = vec.size();
                            for (int j = 0; j < sz; j ++) vis[vec[j]] = 0;
                        }
                    }
                }
            }
        }
    }
    return 0;
}

int main() {
    //srand(time(0));
    scanf("%d %d", &k, &n);
    for (int i = 0; i < n * k; i ++) p[i].input();
    random_shuffle(p, p + n * k);
    ans.resize(k), ans_r.resize(k);
    for (del = 1.0; !solve(0); del -= 0.015);
    for (int i = 0; i < k; i ++) ans[i].print(), printf("%.12lf\n", ans_r[i]);
    return 0;
}


#include <iostream>
#include <vector>

template <int MOD>
struct ModInt {
    using lint = long long;
    int val;

    // constructor
    ModInt(lint v = 0) : val(v % MOD) {
        if (val < 0) val += MOD;
    };

    // unary operator
    ModInt operator+() const { return ModInt(val); }
    ModInt operator-() const { return ModInt(MOD - val); }
    ModInt inv() const { return this->pow(MOD - 2); }

    // arithmetic
    ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
    ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
    ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
    ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }
    ModInt pow(lint n) const {
        auto x = ModInt(1);
        auto b = *this;
        while (n > 0) {
            if (n & 1) x *= b;
            n >>= 1;
            b *= b;
        }
        return x;
    }

    // compound assignment
    ModInt& operator+=(const ModInt& x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt& operator-=(const ModInt& x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt& operator*=(const ModInt& x) {
        val = lint(val) * x.val % MOD;
        return *this;
    }
    ModInt& operator/=(const ModInt& x) { return *this *= x.inv(); }

    // compare
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }

    // I/O
    friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

int main() {
    int n, x;
    std::string t;
    std::cin >> n >> x >> t;

    auto sdp = vec(x + 1, vec(n + 1, vec(n + 1, mint(0))));
    auto pdp = sdp, adp = sdp;
    auto cnts = vec(x + 1, mint(0));    // 解
    auto lenpow = vec(x + 1, mint(0));  // 2^|F(k)|

    for (int k = 0; k < 2; ++k) {
        std::string s(1, '0' + k);  // F(k)

        for (int l = 0; l <= n; ++l) {
            // 空文字列
            sdp[k][l][l] = 2;
            pdp[k][l][l] = 2;
            adp[k][l][l] = 1;

            if (s == t.substr(l, 1)) {
                sdp[k][l][l + 1] = 1;
                pdp[k][l][l + 1] = 1;
                adp[k][l][l + 1] = 1;
            }
        }
        cnts[k] = adp[k][0][n];
        lenpow[k] = 2;
    }

    for (int k = 2; k <= x; ++k) {
        lenpow[k] = lenpow[k - 1] * lenpow[k - 2];

        for (int l = 0; l <= n; ++l) {
            for (int r = l; r <= n; ++r) {
                pdp[k][l][r] = 0;
                sdp[k][l][r] = 0;
                adp[k][l][r] = 0;

                for (int i = l; i <= r; ++i) {
                    // F(k-1)からi文字
                    pdp[k][l][r] += (i == r ? pdp : adp)[k - 1][l][i] * pdp[k - 2][i][r];
                    sdp[k][l][r] += sdp[k - 1][l][i] * (i == l ? sdp : adp)[k - 2][i][r];
                    adp[k][l][r] += adp[k - 1][l][i] * adp[k - 2][i][r];
                }
            }
        }

        // 解の更新
        cnts[k] = cnts[k - 1] * lenpow[k - 2] + cnts[k - 2] * lenpow[k - 1];
        for (int i = 1; i < n; ++i) {
            cnts[k] += sdp[k - 1][0][i] * pdp[k - 2][i][n];
        }
    }

    std::cout << cnts[x] << std::endl;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<map>
#include<set>
#include<string.h>
using namespace std;
/*
int main()
  {
  for each(i,j,k,bot,l,cur)
    {
    if (j+1<=m && !bot)
      {
      for (int p=0; p<=k/2; ++p)
        if (c[k][p]*l<=q && i+p+k<=n)
          {
            {
            int tmp=n-i-p-k;
            int tmp2=c[k][p]*l;
            for (int w=0; w<=tmp; ++w)
              if (tmp2*power[p][w]<=q)
                addto(cur,i+k+p+w,j+1,p+w,false,tmp2*power[p][w]);
              else
                break;
            }
          if (p!=k/2 && i+k-p+k<=n)
            {
            p=k-p;
            int tmp=n-i-p-k;
            int tmp2=c[k][p]*l;
            for (int w=0; w<=tmp; ++w)
              if (tmp2*power[p][w]<=q)
                addto(cur,i+k+p+w,j+1,p+w,false,tmp2*power[p][w])
              else
                break;
            p=k-p;
            }
          }
      }
    else
      {
      //case one newbot=false;
      for (int p=0; p<=k/2; ++p)
        if (c[k][p]*l<=q && i+p+k<=n)
          {
            {
            int tmp=n-i-p-k;
            int tmp2=c[k][p]*l;
            for (int w=0; w<=tmp; ++w)
              if (tmp2*power[p][w]<=q)
                addto(cur,i+k+p+w,j+1,p+w,false,tmp2*power[p][w])
              else
                break;
            }
          if (p!=k/2 && i+k-p+k<=n)
            {
            p=k-p;
            int tmp=n-i-p-k;
            int tmp2=c[k][p]*l;
            for (int w=0; w<=tmp; ++w)
              if (tmp2*power[p][w]<=q)
                addto(cur,i+k+p+w,j+1,p+w,false,tmp2*power[p][w])
              else
                break;
            p=k-p;
            }
          }
      //case two newbot=true
      for (int p=0; p<=k/2; ++p)
        if (c[k][p]*l<=q && i+p+k+1<=n)
          {
            {
            int tmp=n-i-p-k-1;
            int tmp2=c[k][p]*l;
            for (int w=0; w<=tmp; ++w)
              if (tmp2*power[p+1][w]<=q)
                addto(cur,i+k+p+w+1,j+1,p+w,true,tmp2*power[p+1][w])
              else
                break;
            }
          if (p!=k/2 && i+k-p+k+1<=n)
            {
            p=k-p;
            int tmp=n-i-p-k-1;
            int tmp2=c[k][p]*l;
            for (int w=0; w<=tmp; ++w)
              if (tmp2*power[p+1][w]<=q)
                addto(cur,i+k+p+w+1,j+1,p+w,true,tmp2*power[p+1][w])
              else
                break;
            p=k-p;
            }
          }
      }
    }
  }
*/
const int hashn=100007;
const int modular=1000000007;
struct fish
  {
  int cur,i,j,k,l;
  } que[1000000];
struct pear
  {
  int data,next;
  } hash[1000000];
int hashf(int i,int j,int k,int l)
  {
  return (i*10007+j*9901+k*11001+l*23123)% hashn;
  }
int lef,righ,statepool,m;
void addto(int cur,int i,int j,int k,int l)
  {
  if (j>m) return;
  if (l==0)
    {
    ++cur;
    --cur;
    }
  int h=hashf(i,j,k,l);
  for (int p=hash[h].next;p!=0;p=hash[p].next)
    {
    int x=hash[p].data;
    if (que[x].i==i && que[x].j==j && que[x].k==k && que[x].l==l)
      {
      que[x].cur+=cur;
      if (que[x].cur>modular) que[x].cur-=modular;
      return;
      }
    }
  ++righ;
  que[righ].i=i;
  que[righ].j=j;
  que[righ].k=k;
  que[righ].l=l;
  que[righ].cur=cur;
  ++statepool;
  hash[statepool].next=hash[h].next;
  hash[statepool].data=righ;
  hash[h].next=statepool;
  }
int power[110][110];
int c[110][110],bottle[110][110];
int q;
void init()
  {
  lef=1; righ=0; statepool=0;
  memset(power,0,sizeof(power));
  memset(hash,0,sizeof(hash));
  memset(c,0,sizeof(c));
  for (int i=0; i<=100; ++i)
    {
    power[i][0]=1;
    for (int j=1; j<=100 && power[i][j-1]<=q; ++j)
      power[i][j]=power[i][j-1]*i;
    }
  c[0][0]=1;
  for (int i=1; i<=100; ++i)
    {
    c[i][0]=1;
    c[i][i]=1;
    for (int j=1; j<=i-1 && c[i][j-1]<=q; ++j)
      c[i][i-j]=c[i][j]=c[i-1][j-1]+c[i-1][j];
    }
  memset(bottle,0,sizeof(bottle));
  bottle[0][0]=1;
  for (int i=1; i<=100; ++i)
    for (int j=0;j<=100; ++j)
      for (int k=0; k<=j; ++k)
        bottle[i][j]+=bottle[i-1][k];
  }
int n,i,j,k,l,p,w,tmp,tmp2,cur;
int totalans;
bool bot;
int main()
  {
  cin >> n >> m >> q;
  ++n;
  init();
  for (i=2; i<=(n+1)/2; ++i)
    addto(1,i,1,i-1,1);
  totalans=0;
  for (;lef<=righ;++lef)
    {
    i=que[lef].i;
    j=que[lef].j;
    k=que[lef].k;
    l=que[lef].l;
    cur=que[lef].cur;
    if (k==0)
      {
      totalans=(totalans+cur*(long long)(m-j+1))% modular;
      }
    //printf("%d %d %d %d %d %d\n",i,j,k,l,cur,cur*(m-j+1));

    if (lef==1)
      {
      ++i;
      --i;
      }
    if (j+1<=m && k!=0)
      {
      /*
      for (p=0; p<=(k+1)/2; ++p)
        if (c[k][p]*l<=q && i+p+k<=n)
          {
            {
            tmp=n-i-p-k;
            tmp2=c[k][p]*l;
            for (w=0; w<=tmp; ++w)
              {
              if (tmp2*power[p][w]<=q)
                addto(cur,i+k+p+w,j+1,p+w,tmp2*power[p][w]);
              else
                break;
              if (p==0) break;
              }
            }
          if (k-p>(k+1)/2 && i+k-p+k<=n)
            {
            p=k-p;
            tmp=n-i-p-k;
            tmp2=c[k][p]*l;
            for (w=0; w<=tmp; ++w)
              if (tmp2*power[p][w]<=q)
                addto(cur,i+k+p+w,j+1,p+w,tmp2*power[p][w]);
              else
                break;
            p=k-p;
            }
          }
      */
      //p:下一行点的个数
      for (p=k; p<=n;++p)
        if (bottle[k][p-k]*l<=q && i+p<=n)
          {
          addto(cur,i+p,j+1,p-k,bottle[k][p-k]*l);
          }
        else
          break;
      }
    }
  cout << totalans << endl;
  }


#include<bits/stdc++.h>
using namespace std;
vector<char> opers;
int modulo(1e9 + 7);
typedef long long LL;
int pw(int x, int n) {
	int res(1);
	while(n) {
		if(n & 1)
			res = (LL)res * x % modulo;
		x = (LL)x * x % modulo;
		n /= 2;
	}
	return res;
}
inline bool isDigit(char c) {
	return c >= '0' && c <= '9';
}
const int N(111111);
char st[N];
struct Opad {
	string str;
	int val, mul;
	Opad() {
		val = 0;
		mul = 1;
	}
};
vector<Opad> opads;
void operate() {
	Opad o1, o2;
	o2 = opads.back();
	opads.pop_back();
	o1 = opads.back();
	opads.pop_back();
	if(opers.back() == '+') {
		Opad res;
		res.val = ((LL)o1.val * o2.mul + o2.val) % modulo;
		res.mul = (LL)o1.mul * o2.mul % modulo;
		opads.push_back(res);
	}else {
		Opad res;
		for(int i((int)o1.str.size() - 1); i >= 0; i--) {
			for(int j(0); j < (int)o1.str[i] - '0'; j++) {
				res.val = ((LL)res.val * o2.mul + o2.val) % modulo;
				res.mul = ((LL)res.mul * o2.mul) % modulo;
			}
			int mul(o2.mul), add(o2.val);
			for(int j(0); j < 9; j++) {
				o2.mul = ((LL)mul * o2.mul) % modulo;
				o2.val = ((LL)mul * o2.val + add) % modulo;
			}
		}
		opads.push_back(res);
	}
	opers.pop_back();
}
Opad calc(const string & a) {
	Opad res;
	res.str = a;
	for(int i(0); i < (int)a.size(); i++) {
		res.mul = res.mul * 10ll % modulo;
		res.val = (res.val * 10ll + a[i] - '0') % modulo;
	}
	return res;
}
Opad calc(const string & le, const string & ri) {
	//cout << le << ' ' << ri << endl;
	Opad res;
	Opad a(calc(le)), b(calc(ri));
	int phil(0), phir(0);
	for(int i(0); i < (int)le.size(); i++) {
		phil = ((LL)phil * 10ll + le[i] - '0') % (modulo - 1);
	}
	for(int i(0); i < (int)ri.size(); i++) {
		phir = ((LL)phir * 10ll + ri[i] - '0') % (modulo - 1);
	}
	//cout << a.val << ' ' << b.mul << endl;
	int cur(1), curphi(1);
	for(int i(1); i < le.size(); i++) {
		cur = cur * 10ll % modulo;
		curphi = curphi * 10ll % (modulo - 1);
	}
	for(int len(le.size()); len <= ri.size(); len++) {
		int leval, rival, lephi, riphi;
		if(len == le.size()) {
			leval = a.val;
			lephi = phil;
		}else {
			leval = cur;
			lephi = curphi;
		}
		if(len == ri.size()) {
			rival = b.val;
			riphi = phir;
		}else {
			rival = (cur * 10ll + modulo - 1) % modulo;
			riphi = (curphi * 10ll + modulo - 2) % (modulo - 1);
		}
		//printf("%d %d %d %d\n", leval, lephi, rival, riphi);
		int bs(pw(10, len));
		//cout << bs << endl;
		//cout << (rival + 1ll) * ((pw(bs, (riphi - lephi + modulo) % (modulo - 1)) - 1ll + modulo) * pw(bs - 1, modulo - 2) % modulo) % modulo << endl;
		//cout << ((rival - leval + 1ll + modulo) * pw(bs, (rival - leval + 1 + modulo) % (modulo - 1)) - (rival - leval + 2ll + modulo) * pw(bs, (riphi - lephi + modulo) % (modulo - 1)) + 1) % modulo * pw(bs - 1, modulo - 2) % modulo * pw(bs - 1, modulo - 2) % modulo << endl;
		//cout << ((rival + 1ll) * ((pw(bs, (riphi - lephi + modulo) % (modulo - 1)) - 1ll + modulo) * pw(bs - 1, modulo - 2) % modulo) % modulo - ((rival - leval + 1ll + modulo) * pw(bs, (rival - leval + 1 + modulo) % (modulo - 1)) - (rival - leval + 2ll + modulo) * pw(bs, (riphi - lephi + modulo) % (modulo - 1)) + 1) % modulo * pw(bs - 1, modulo - 2) % modulo * pw(bs - 1, modulo - 2) % modulo) << endl;
		int val = (((rival + 1ll) * ((pw(bs, (riphi - lephi + modulo) % (modulo - 1)) - 1ll + modulo) * pw(bs - 1, modulo - 2) % modulo) % modulo - ((rival - leval + 1ll + modulo) * pw(bs, (riphi - lephi + 1 + modulo) % (modulo - 1)) - (rival - leval + 2ll + modulo) * pw(bs, (riphi - lephi + modulo) % (modulo - 1)) + 1) % modulo * pw(bs - 1, modulo - 2) % modulo * pw(bs - 1, modulo - 2) % modulo) + modulo) % modulo;
		int mul = pw(bs, (riphi - lephi + modulo) % (modulo - 1));
		res.val = ((LL)res.val * mul + val) % modulo;
		res.mul = (LL)res.mul * mul % modulo;
		//cout << val << ' ' << mul << endl;
		cur = cur * 10ll % modulo;
		curphi = curphi * 10ll % (modulo - 1);
	}
	return res;
}
int main() {
	scanf("%s", st);
	int len(strlen(st));
	for(int i(0); i < len; i++) {
		if(isDigit(st[i])) {
			string s;
			int j(i);
			for( ; isDigit(st[j]); j++)
				s.push_back(st[j]);
			if(st[j] == '-') {
				string t;
				for(j++; isDigit(st[j]); j++)
					t.push_back(st[j]);
				opads.push_back(calc(s, t));
			}else {
				opads.push_back(calc(s));
			}
			i = j - 1;
		}else if(st[i] == '(') {
			opers.push_back('(');
		}else if(st[i] == '+') {
			opers.push_back('+');
		}else if(st[i] == ')') {
			while(opers.back() != '(') {
				operate();
			}
			operate();
		}
	}
	while(opers.size())
		operate();
	cout << opads.back().val << endl;
}


#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int N, V, E;
int mat[301][301];
int A[301], B[301];
vector<pair<pair<int, int>, int> > R;

void flow2(vector<int>& P, int si, int ti, int d) {
	int i;
	if (si == ti) return;
	if (A[P[si + 1]] + d <= V) {
		A[P[si]] -= d;
		A[P[si + 1]] += d;
		R.push_back(make_pair(make_pair(P[si], P[si + 1]), d));
		flow2(P, si + 1, ti, d);
	}
	else {
		int d2 = V - A[P[si + 1]];
		A[P[si]] -= d2;
		A[P[si + 1]] += d2;
		if (d2 > 0) R.push_back(make_pair(make_pair(P[si], P[si + 1]), d2));
		flow2(P, si + 1, ti, d);
		A[P[si]] -= d - d2;
		A[P[si + 1]] += d - d2;
		R.push_back(make_pair(make_pair(P[si], P[si + 1]), d - d2));
	}
}

#define FOR(y, N) for (int y = 0; y < N; ++y)

int flow(int s, int t, int d) {
	int i, j, y;
	vector<int> P;
	P.push_back(s);
	for (i = s; i != t;) {
		FOR(y, N) if (mat[i][y] == 1 && mat[y][t] == mat[i][t] - 1) break;
		P.push_back(i = y);
	}

	flow2(P, 0, P.size() - 1, d);
}

#define _P printf

void solve() {
	int f, r, i, j, k, l, x, y;

	cin >> N >> V >> E;
	FOR(i, N) cin >> A[i];
	FOR(i, N) cin >> B[i];

	FOR(x, N) FOR(y, N) mat[x][y] = 9999;
	FOR(x, N) mat[x][x] = 0;
	FOR(i, E) {
		cin >> x >> y;
		mat[x - 1][y - 1] = mat[y - 1][x - 1] = 1;
	}
	FOR(i, N) FOR(j, N) FOR(k, N) mat[j][k] = min(mat[j][k], mat[j][i] + mat[i][k]);
	FOR(x, N) FOR(y, N) {
		if (mat[x][y] > N) continue;
		j = min(A[x] - B[x], B[y] - A[y]);
		if (j <= 0) continue;
		flow(x, y, j);
	}

	FOR(x, N) if (A[x] != B[x]) return (void)_P("NO\n");
	_P("%d\n", R.size());
	FOR(i, R.size()) _P("%d %d %d\n", R[i].first.first + 1, R[i].first.second + 1, R[i].second);
}