#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

typedef int ll;
typedef long double ld;
ll MOD = 1e9 + 7;
ll MOD2 = 998244353;
ll INF = 1e18;

const int L = 19;
const int N = (1 << L);
vector<ll> gr[N];
vector<ll> gr2[N];

ll kol_c = 0;
ll comp[N];
ll h[N];
ll used[N];
ll dfs(ll v, ll pr){
    h[v] = h[pr] + 1;
    ll an = MOD;
    for (auto i : gr[v]){
        if (!h[i]){
            an = min(an, dfs(i, v));
        }
        else{
            if (i != pr)
                an = min(an, h[i]);
        }
    }
    comp[v] = (an >= h[v]);
    return an;
}
void cm(ll v, ll kol){
    if (comp[v])
        comp[v] = ++kol_c;
    else
        comp[v] = kol;
    used[v] = 1;
    for (auto i : gr[v]){
        if (!used[i]){
            cm(i, comp[v]);
        }
    }
}

vector<ll> num[N];

vector<ll> cnt, cnt2;
vector<ll> cnt_, cnt2_;

ll binpow(ll x, ll y)
{
    if (!y)
        return 1;
    if (y % 2)
        return (1ll * (long long)x * (long long)binpow(x, y - 1)) % MOD;
    return binpow((1ll * (long long)x * (long long)x) % MOD, y / 2);
}

ll revers(ll x)
{
    return binpow(x % MOD, MOD - 2);
}

void adamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    adamar(a, l, mid);
    adamar(a, mid, r);
    for (int i = l; i < mid; i++)
    {
        ll u = a[i], v = a[i + mid - l];
        a[i] = (u + v) % MOD;
        a[i + mid - l] = (u - v) % MOD;
    }
}

void andamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    andamar(a, l, mid);
    andamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] + a[i + mid - l])  % MOD;
}

void revandamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revandamar(a, l, mid);
    revandamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] - a[i + mid - l]) % MOD;
}

void ordamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    ordamar(a, l, mid);
    ordamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (a[i] + a[i + mid - l]) % MOD;
}

void revordamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revordamar(a, l, mid);
    revordamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (-a[i] + a[i + mid - l]) % MOD;
}

vector<ll> xormult(vector<ll> a, vector<ll> b)
{
    adamar(a, 0, a.size());
    adamar(b, 0, b.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)b[i])  % MOD;
    adamar(a, 0, a.size());
    ll revn = revers(a.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)revn) % MOD;
    return a;
}

ll binpow2(ll x, ll y)
{
    if (!y)
        return 1;
    if (y % 2)
        return (1ll * (long long)x * (long long)binpow2(x, y - 1)) % MOD2;
    return binpow2((1ll * (long long)x * (long long)x) % MOD2, y / 2);
}

ll revers2(ll x)
{
    return binpow2(x % MOD2, MOD2 - 2);
}

void adamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    adamar2(a, l, mid);
    adamar2(a, mid, r);
    for (int i = l; i < mid; i++)
    {
        ll u = a[i], v = a[i + mid - l];
        a[i] = (u + v) % MOD2;
        a[i + mid - l] = (u - v) % MOD2;
    }
}

void andamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    andamar2(a, l, mid);
    andamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] + a[i + mid - l])  % MOD2;
}

void revandamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revandamar2(a, l, mid);
    revandamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] - a[i + mid - l]) % MOD2;
}

void ordamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    ordamar2(a, l, mid);
    ordamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (a[i] + a[i + mid - l]) % MOD2;
}

void revordamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revordamar2(a, l, mid);
    revordamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (-a[i] + a[i + mid - l]) % MOD2;
}

vector<ll> xormult2(vector<ll> a, vector<ll> b)
{
    adamar2(a, 0, a.size());
    adamar2(b, 0, b.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)b[i])  % MOD2;
    adamar2(a, 0, a.size());
    ll revn = revers2(a.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)revn) % MOD2;
    return a;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);

    ll n, m;
    cin >> n >> m;
    ll x = 0;
    for (int i = 0; i < m; i++){
        ll u, v, w;
        cin >> u >> v >> w;
        x ^= w;
        u--,v--;
        gr[u].push_back(v);
        gr2[u].push_back(w);
        gr[v].push_back(u);
        gr2[v].push_back(w);
    }
    dfs(0, 0);
    cm(0, 0);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < gr[i].size(); j++){
            ll to = gr[i][j];
            ll w = gr2[i][j];
            if (i > to && comp[i] == comp[to]){
                num[comp[i]].push_back(w);
            }
        }
    }
//    for (int i = 0; i < N; i++){
//        num[i].push_back(0);
//    }
    ///////////////////////////////////
    cnt.assign(N, 0);
    cnt_.assign(N, 0);
    cnt[x] = 1;
    //cnt_[x] = 1;
    for (int ii = 0; ii < N; ii++){
        if (num[ii].size()) {
            cnt2.assign(N, 0);
           // cnt2_.assign(N, 0);
            for (auto j : num[ii]) {
                cnt2[j]++;
            //    cnt2_[j]++;
            }
            cnt = xormult(cnt, cnt2);
            //cnt_ = xormult2(cnt_, cnt2_);
//            for (int i = 0; i < N; i++){
//                cnt[i] %= MOD;
//                cnt_[i] %= MOD2;
//                if (cnt[i] < 0){
//                    cnt[i] += MOD;
//                }
//                if (cnt_[i] < 0){
//                    cnt_[i] += MOD2;
//                }
//            }
        }
    }
    for (int i = 0; i < N; i++){
        if (cnt[i] || cnt_[i]){
            cout << i << " " << cnt[i];
            break;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

typedef int ll;
typedef long double ld;
ll MOD = 1e9 + 7;
ll MOD2 = 998244353;
ll INF = 1e18;

const int L = 17;
const int N = (1 << L);
vector<ll> gr[N];
vector<ll> gr2[N];

ll kol_c = 0;
ll comp[N];
ll h[N];
ll used[N];
ll dfs(ll v, ll pr){
    h[v] = h[pr] + 1;
    ll an = MOD;
    for (auto i : gr[v]){
        if (!h[i]){
            an = min(an, dfs(i, v));
        }
        else{
            if (i != pr)
                an = min(an, h[i]);
        }
    }
    comp[v] = (an >= h[v]);
    return an;
}
void cm(ll v, ll kol){
    if (comp[v])
        comp[v] = ++kol_c;
    else
        comp[v] = kol;
    used[v] = 1;
    for (auto i : gr[v]){
        if (!used[i]){
            cm(i, comp[v]);
        }
    }
}

vector<ll> num[N];

vector<ll> cnt, cnt2;
vector<ll> cnt_, cnt2_;

ll binpow(ll x, ll y)
{
    if (!y)
        return 1;
    if (y % 2)
        return (1ll * (long long)x * (long long)binpow(x, y - 1)) % MOD;
    return binpow((1ll * (long long)x * (long long)x) % MOD, y / 2);
}

ll revers(ll x)
{
    return binpow(x % MOD, MOD - 2);
}

void adamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    adamar(a, l, mid);
    adamar(a, mid, r);
    for (int i = l; i < mid; i++)
    {
        ll u = a[i], v = a[i + mid - l];
        a[i] = (u + v) % MOD;
        a[i + mid - l] = (u - v) % MOD;
    }
}

void andamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    andamar(a, l, mid);
    andamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] + a[i + mid - l])  % MOD;
}

void revandamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revandamar(a, l, mid);
    revandamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] - a[i + mid - l]) % MOD;
}

void ordamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    ordamar(a, l, mid);
    ordamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (a[i] + a[i + mid - l]) % MOD;
}

void revordamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revordamar(a, l, mid);
    revordamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (-a[i] + a[i + mid - l]) % MOD;
}

vector<ll> xormult(vector<ll> a, vector<ll> b)
{
    adamar(a, 0, a.size());
    adamar(b, 0, b.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)b[i])  % MOD;
    adamar(a, 0, a.size());
    ll revn = revers(a.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)revn) % MOD;
    return a;
}

ll binpow2(ll x, ll y)
{
    if (!y)
        return 1;
    if (y % 2)
        return (1ll * (long long)x * (long long)binpow2(x, y - 1)) % MOD2;
    return binpow2((1ll * (long long)x * (long long)x) % MOD2, y / 2);
}

ll revers2(ll x)
{
    return binpow2(x % MOD2, MOD2 - 2);
}

void adamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    adamar2(a, l, mid);
    adamar2(a, mid, r);
    for (int i = l; i < mid; i++)
    {
        ll u = a[i], v = a[i + mid - l];
        a[i] = (u + v) % MOD2;
        a[i + mid - l] = (u - v) % MOD2;
    }
}

void andamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    andamar2(a, l, mid);
    andamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] + a[i + mid - l])  % MOD2;
}

void revandamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revandamar2(a, l, mid);
    revandamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] - a[i + mid - l]) % MOD2;
}

void ordamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    ordamar2(a, l, mid);
    ordamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (a[i] + a[i + mid - l]) % MOD2;
}

void revordamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revordamar2(a, l, mid);
    revordamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (-a[i] + a[i + mid - l]) % MOD2;
}

vector<ll> xormult2(vector<ll> a, vector<ll> b)
{
    adamar2(a, 0, a.size());
    adamar2(b, 0, b.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)b[i])  % MOD2;
    adamar2(a, 0, a.size());
    ll revn = revers2(a.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)revn) % MOD2;
    return a;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);

    ll n, m;
    cin >> n >> m;
    ll x = 0;
    for (int i = 0; i < m; i++){
        ll u, v, w;
        cin >> u >> v >> w;
        x ^= w;
        u--,v--;
        gr[u].push_back(v);
        gr2[u].push_back(w);
        gr[v].push_back(u);
        gr2[v].push_back(w);
    }
    dfs(0, 0);
    cm(0, 0);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < gr[i].size(); j++){
            ll to = gr[i][j];
            ll w = gr2[i][j];
            if (i > to && comp[i] == comp[to]){
                num[comp[i]].push_back(w);
            }
        }
    }
//    for (int i = 0; i < N; i++){
//        num[i].push_back(0);
//    }
    ///////////////////////////////////
    cnt.assign(N, 0);
    cnt_.assign(N, 0);
    cnt[x] = 1;
    cnt_[x] = 1;
    for (int ii = 0; ii < N; ii++){
        if (num[ii].size()) {
            cnt2.assign(N, 0);
            cnt2_.assign(N, 0);
            for (auto j : num[ii]) {
                cnt2[j]++;
                cnt2_[j]++;
            }
            cnt = xormult(cnt, cnt2);
            cnt_ = xormult2(cnt_, cnt2_);
            for (int i = 0; i < N; i++){
                cnt[i] %= MOD;
                cnt_[i] %= MOD2;
                if (cnt[i] < 0){
                    cnt[i] += MOD;
                }
                if (cnt_[i] < 0){
                    cnt_[i] += MOD2;
                }
            }
        }
    }
    for (int i = 0; i < N; i++){
        if (cnt[i] || cnt_[i]){
            cout << i << " " << cnt[i];
            break;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

typedef int ll;
typedef long double ld;
ll MOD = 1e9 + 7;
ll MOD2 = 998244353;
ll INF = 1e18;

const int L = 18;
const int N = (1 << L);
vector<ll> gr[N];
vector<ll> gr2[N];

ll kol_c = 0;
ll comp[N];
ll h[N];
ll used[N];
ll dfs(ll v, ll pr){
    h[v] = h[pr] + 1;
    ll an = MOD;
    for (auto i : gr[v]){
        if (!h[i]){
            an = min(an, dfs(i, v));
        }
        else{
            if (i != pr)
                an = min(an, h[i]);
        }
    }
    comp[v] = (an >= h[v]);
    return an;
}
void cm(ll v, ll kol){
    if (comp[v])
        comp[v] = ++kol_c;
    else
        comp[v] = kol;
    used[v] = 1;
    for (auto i : gr[v]){
        if (!used[i]){
            cm(i, comp[v]);
        }
    }
}

vector<ll> num[N];

vector<ll> cnt, cnt2;
vector<ll> cnt_, cnt2_;

ll binpow(ll x, ll y)
{
    if (!y)
        return 1;
    if (y % 2)
        return (1ll * (long long)x * (long long)binpow(x, y - 1)) % MOD;
    return binpow((1ll * (long long)x * (long long)x) % MOD, y / 2);
}

ll revers(ll x)
{
    return binpow(x % MOD, MOD - 2);
}

void adamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    adamar(a, l, mid);
    adamar(a, mid, r);
    for (int i = l; i < mid; i++)
    {
        ll u = a[i], v = a[i + mid - l];
        a[i] = (u + v) % MOD;
        a[i + mid - l] = (u - v) % MOD;
    }
}

void andamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    andamar(a, l, mid);
    andamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] + a[i + mid - l])  % MOD;
}

void revandamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revandamar(a, l, mid);
    revandamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] - a[i + mid - l]) % MOD;
}

void ordamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    ordamar(a, l, mid);
    ordamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (a[i] + a[i + mid - l]) % MOD;
}

void revordamar(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revordamar(a, l, mid);
    revordamar(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (-a[i] + a[i + mid - l]) % MOD;
}

vector<ll> xormult(vector<ll> a, vector<ll> b)
{
    adamar(a, 0, a.size());
    adamar(b, 0, b.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)b[i])  % MOD;
    adamar(a, 0, a.size());
    ll revn = revers(a.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)revn) % MOD;
    return a;
}

ll binpow2(ll x, ll y)
{
    if (!y)
        return 1;
    if (y % 2)
        return (1ll * (long long)x * (long long)binpow2(x, y - 1)) % MOD2;
    return binpow2((1ll * (long long)x * (long long)x) % MOD2, y / 2);
}

ll revers2(ll x)
{
    return binpow2(x % MOD2, MOD2 - 2);
}

void adamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    adamar2(a, l, mid);
    adamar2(a, mid, r);
    for (int i = l; i < mid; i++)
    {
        ll u = a[i], v = a[i + mid - l];
        a[i] = (u + v) % MOD2;
        a[i + mid - l] = (u - v) % MOD2;
    }
}

void andamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    andamar2(a, l, mid);
    andamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] + a[i + mid - l])  % MOD2;
}

void revandamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revandamar2(a, l, mid);
    revandamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i] = (a[i] - a[i + mid - l]) % MOD2;
}

void ordamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    ordamar2(a, l, mid);
    ordamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (a[i] + a[i + mid - l]) % MOD2;
}

void revordamar2(vector<ll> &a, int l, int r)
{
    if (l + 1 >= r)
        return;
    int mid = (r + l) / 2;
    revordamar2(a, l, mid);
    revordamar2(a, mid, r);
    for (int i = l; i < mid; i++)
        a[i + mid - l] = (-a[i] + a[i + mid - l]) % MOD2;
}

vector<ll> xormult2(vector<ll> a, vector<ll> b)
{
    adamar2(a, 0, a.size());
    adamar2(b, 0, b.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)b[i])  % MOD2;
    adamar2(a, 0, a.size());
    ll revn = revers2(a.size());
    for (int i = 0; i < a.size(); i++)
        a[i] = (1ll * (long long)a[i] * (long long)revn) % MOD2;
    return a;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);

    ll n, m;
    cin >> n >> m;
    ll x = 0;
    for (int i = 0; i < m; i++){
        ll u, v, w;
        cin >> u >> v >> w;
        x ^= w;
        u--,v--;
        gr[u].push_back(v);
        gr2[u].push_back(w);
        gr[v].push_back(u);
        gr2[v].push_back(w);
    }
    dfs(0, 0);
    cm(0, 0);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < gr[i].size(); j++){
            ll to = gr[i][j];
            ll w = gr2[i][j];
            if (i > to && comp[i] == comp[to]){
                num[comp[i]].push_back(w);
            }
        }
    }
//    for (int i = 0; i < N; i++){
//        num[i].push_back(0);
//    }
    ///////////////////////////////////
    cnt.assign(N, 0);
    cnt_.assign(N, 0);
    cnt[x] = 1;
    cnt_[x] = 1;
    for (int ii = 0; ii < N; ii++){
        if (num[ii].size()) {
            cnt2.assign(N, 0);
            cnt2_.assign(N, 0);
            for (auto j : num[ii]) {
                cnt2[j]++;
                cnt2_[j]++;
            }
            cnt = xormult(cnt, cnt2);
            cnt_ = xormult2(cnt_, cnt2_);
            for (int i = 0; i < N; i++){
                cnt[i] %= MOD;
                cnt_[i] %= MOD2;
                if (cnt[i] < 0){
                    cnt[i] += MOD;
                }
                if (cnt_[i] < 0){
                    cnt_[i] += MOD2;
                }
            }
        }
    }
    for (int i = 0; i < N; i++){
        if (cnt[i] || cnt_[i]){
            cout << i << " " << cnt[i];
            break;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    long long int maxsumsofar=0;
    long long int currentsum=0;
    int start=0;
    int s=0;
    int end=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<=0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return maxsumsofar-1;
    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     bool flag=true;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]>0){
             flag=false;
         }
     }
    long long int sumb=maxsum(a,n);
     if(flag){
         cout<<"NO"<<endl;
     }
      else if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    long long int maxsumsofar=0;
    long long int currentsum=0;
    int start=0;
    int s=0;
    int end=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<=0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return maxsumsofar-1;
    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     long long int suma=0;
     bool flag=true;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]>0){
             flag=false;
         }
     }
    long long int sumb=maxsum(a,n);
     if(flag){
         cout<<"NO"<<endl;
     }
      else if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int currentsum=0;
    int start=0;
    int s=0;
    int end;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return -1*maxsumsofar
    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
     }
      int sumb=maxsum(a,n);
       if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.14159265359
#define all(v) v.begin(),v.end()
#define vil vector<long int>
#define vi vector<int>
#define pb(x) push_back(x)
int main(){
int t;
//t=1;
cin>>t;
while(t--){
/*int n;
cin>>n;
vi a(n);
for(int i=0;i<n;i++){
    cin>>a[i];
}
*/
int n;
cin>>n;
vil a(n);
int p=0;
int n=0;
for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i]>0){
        p++;
    }
    if(a[i]<0){
        n++;
    }
}
if(p>0&&n==0){
cout<<"YES"<<endl;
}
else{
    cout<<"NO"<<endl;
}

}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.14159265359
#define all(v) v.begin(),v.end()
#define vil vector<long int>
#define vi vector<int>
#define pb(x) push_back(x)
int maxSubArraySum(vil a, int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0,
       start =0, end = 0, s=0;

    for (int i=0; i< size; i++ )
    {
        max_ending_here += a[i];

        if (max_so_far < max_ending_here)
        {
            max_so_far = max_ending_here;
            start = s;
            end = i;
        }

        if (max_ending_here <= 0)
        {
            max_ending_here = 0;
            s = i + 1;
        }
    }
if(start==0&&end==size-1){
    if(a[0]>a[size-1]){
        max_so_far-=a[size-1];
    }
    else{
    max_so_far-=a[0];
    }
}
return max_so_far;
}
int main(){
int t;
//t=1;
cin>>t;
while(t--){
/*int n;
cin>>n;
vi a(n);
for(int i=0;i<n;i++){
    cin>>a[i];
}
*/
int m;
cin>>m;
vil a(m);
ll sum1=0;
for(int i=0;i<m;i++){
    cin>>a[i];
    sum1+=a[i];
}
ll sum2=maxSubArraySum(a,m);
if(sum1>sum2){
    cout<<"YES"<<endl;
}
else{
    cout<<"NO"<<endl;
}

//cout<<sum1<<" "<<sum2;

}
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int currentsum=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
        }

         if(currentsum<0){
                currentsum=0;
            }

    }
    return maxsumsofar;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     bool flag=true;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]<0){
             flag=false;
         }
     }
      int sumb=maxsum(a,n);
      if(flag){
          cout<<"YES"<<endl;
      }
      else if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    long long int maxsumsofar=a[0];
    long long int currentsum=0;
    int start=0;
    int s=0;
    int end=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<=0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return maxsumsofar-a[n];

    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
     }
      int sumb=maxsum(a,n);
       if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int maxsumendshere;
    int maxsumstarts;
    int currentsum=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            maxsumendshere=i;
        }

         if(currentsum<0){
                maxsumstarts=i+1;
                currentsum=0;
            }

    }
    return maxsumsofar;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     bool flag=true;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]<0){
             flag=false;
         }
     }
      int sumb=maxsum(a,n);
      if(suma>sumb||(suma==sumb&&flag==true)){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.14159265359
#define all(v) v.begin(),v.end()
#define vil vector<long int>
#define vi vector<int>
#define pb(x) push_back(x)
int maxSubArraySum(vil a, int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0,
       start =0, end = 0, s=0;

    for (int i=0; i< size; i++ )
    {
        max_ending_here += a[i];

        if (max_so_far < max_ending_here)
        {
            max_so_far = max_ending_here;
            start = s;
            end = i;
        }

        if (max_ending_here <= 0)
        {
            max_ending_here = 0;
            s = i + 1;
        }
    }
if(start==0&&end==size-1){
    if(a[0]>a[size-1]){
        max_so_far-=a[size-1];
    }
    else{
    max_so_far-=a[0];
    }
}
return max_so_far;
}
int main(){
int t;
//t=1;
cin>>t;
while(t--){
/*int n;
cin>>n;
vi a(n);
for(int i=0;i<n;i++){
    cin>>a[i];
}
*/
int m;
cin>>m;
vil a(m);
ll sum1=0;
for(int i=0;i<m;i++){
    cin>>a[i];
    sum1+=a[i];
}
ll sum2=maxSubArraySum(a,m);
/*if(sum1>sum2){
    cout<<"YES"<<endl;
}
else{
    cout<<"NO"<<endl;
}
*/
cout<<sum1<<" "<<sum2;

}
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int maxsumendshere;
    int maxsumstarts;
    int currentsum=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            maxsumendshere=i;
        }

         if(currentsum<0){
                maxsumstarts=i+1;
                currentsum=0;
            }

    }
    return maxsumsofar;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     bool flag=true;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]<0){
             flag=false;
         }
     }
      int sumb=maxsum(a,n);
      if(flag){
          cout<<"NO"<<endl;
      }
      else if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int currentsum=0;
    int start=0;
    int s=0;
    int end;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return -1*maxsumsofar;
    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]<0){
             flag=false;
         }
     }
      int sumb=maxsum(a,n);
       if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.14159265359
#define all(v) v.begin(),v.end()
#define vil vector<long int>
#define vi vector<int>
#define pb(x) push_back(x)
int maxSubArraySum(vil a, int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0,
       start =0, end = 0, s=0;

    for (int i=0; i< size; i++ )
    {
        max_ending_here += a[i];

        if (max_so_far < max_ending_here)
        {
            max_so_far = max_ending_here;
            start = s;
            end = i;
        }

        if (max_ending_here < 0)
        {
            max_ending_here = 0;
            s = i + 1;
        }
    }
if(start==0&&end==size-1){
    if(a[0]>a[size-1]){
        max_so_far-=a[size-1];
    }
    else{
    max_so_far-=a[0];
    }
}
return max_so_far;
}
int main(){
int t;
//t=1;
cin>>t;
while(t--){
/*int n;
cin>>n;
vi a(n);
for(int i=0;i<n;i++){
    cin>>a[i];
}
*/
int m;
cin>>m;
vil a(m);
ll sum1=0;
for(int i=0;i<m;i++){
    cin>>a[i];
    sum1+=a[i];
}
ll sum2=maxSubArraySum(a,m);
if(sum1>sum2){
    cout<<"YES"<<endl;
}
else{
    cout<<"NO"<<endl;
}

}
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int currentsum=0;
    int start=0;
    int s=0;
    int end;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return -1*maxsumsofar
    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]<0){
             flag=false;
         }
     }
      int sumb=maxsum(a,n);
       if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int maxsumendshere;
    int maxsumstarts;
    int currentsum=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            maxsumendshere=i;
        }

         if(currentsum<0){
                maxsumstarts=i+1;
                currentsum=0;
            }

    }
    return maxsumsofar;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     bool flag=true;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
         if(a[i]<0){
             flag=false;
         }
     }
      int sumb=maxsum(a,n);
      if(flag){
          cout<<"YES"<<endl;
      }
      else if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    int maxsumsofar=a[0];
    int currentsum=0;
    int start=0;
    int s=0;
    int end;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return -1*maxsumsofar;
    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
     }
      int sumb=maxsum(a,n);
       if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include <bits/stdc++.h>
using namespace std;
int maxsum(long long int a[],int n){
    long long int maxsumsofar=a[0];
    long long int currentsum=0;
    int start=0;
    int s=0;
    int end=0;
    for(int i=0;i<n;i++)
    {
        currentsum+=a[i];
        if(currentsum>maxsumsofar){
            maxsumsofar=currentsum;
            start=s;
            end=i;
        }

         if(currentsum<=0){
                currentsum=0;
                s=i+1;
            }

    }
    if(start==0&&end==n-1){
        return maxsumsofar-1;

    }
    else{
    return maxsumsofar;
}}
int main()
{
    int t;
    cin>>t;
    while(t--){
     int n;
     cin>>n;
     long long int a[n];
     int suma=0;
     for(int i=0;i<n;i++){
         cin>>a[i];
         suma+=a[i];
     }
      int sumb=maxsum(a,n);
       if(suma>sumb){
          cout<<"YES"<<endl;
      }
   else{
       cout<<"NO"<<endl;
   }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0; i<n ; i++)
        {
            cin>>a[i];
        }
        long long int sum=0;
        for(int i=0; i<n ; i++)
        {
            sum+=a[i];
        }
        int l=0;
        long long int temp=0;
        for(int i=0; i<n-1; i++)
        {
          temp+=a[i];
          if(temp<=0){
                  temp=0;
          }
          if(sum<=temp) {l++; break;}
        }
        if(l==1) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}


#include <stdio.h>

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        char s[n];
        int i, c = 0;
        scanf("%s", &s);
        for(i = 0; i < n; i++)
        {
            if(s[i] == '8')
            {
                c = 1;
                break;
            }
        }
        if(c == 1)
            if(n - i >= 11)
        {
            printf("YES\n");

        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


#include <stdio.h>

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        char s[n];
        int i, c = 0;
        scanf("%s", &s);
        for(i = 0; i < n; i++)
        {
            if(s[i] == '8')
            {
                c = 1;
                break;
            }
            else
            {
                printf("NO\n");
            }
        }
        if(c == 1)
            if(n - i >= 11)
        {
            printf("YES\n");

        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include <string.h>

int main()
{
    int t;
    scanf ("%d",&t);
    char s[50][t];

    int i;
    for (i=0;i<=t-1;i++)
    {
    int n;
    scanf ("%d",&n);
    scanf ("%s",&s[i]);
    int p='8';
    char *testcase;
    testcase=strchr(s[i],p);
    int length;
    length=strlen(testcase);
    if (length>=11) {printf ("YES\n");}
    else printf ("NO\n");

    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include <string.h>

int main()
{
    int t;
    scanf ("%d",&t);
    char s[50][t];

    int i;
    for (i=0;i<=t-1;i++)
    {
    int n;
    scanf ("%d",&n);
    scanf ("%s",&s[i]);
    int p='8';
    char *testcase;
    testcase=strchr(s[i],p);
    if (testcase==NULL) printf("NO\n");
    else
    {
    int length;
    length=strlen(testcase);
    if (length>=11) {printf ("YES\n");}
    else {printf ("NO\n");}
    }

    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include <string.h>

int main()
{
    int t;
    scanf ("%d",&t);
    char s[150][t];

    int i;
    for (i=0;i<=t-1;i++)
    {
    int n;
    scanf ("%d",&n);
    scanf ("%s",&s[i]);
    int p='8';
    char *testcase;
    testcase=strchr(s[i],p);
    if (testcase==NULL) printf("NO\n");
    else
    {
    int length;
    length=strlen(testcase);
    if (length>=11) {printf ("YES\n");}
    else {printf ("NO\n");}
    }

    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include <string.h>

int main()
{
    int t;
    scanf ("%d",&t);
    char s[50][t];

    int i;
    for (i=0;i<=t-1;i++)
    {
    int n;
    scanf ("%d",&n);
    scanf ("%s",&s[i]);
    int p='8';
    char *testcase;
    testcase=strchr(s[i],p);
    int length;
    length=strlen(testcase);
    if (length>=11) {printf ("YES\n");}
    else printf ("NO\n");

    }

}


#include<stdio.h>
int main()
{
    int n,t,i,j,s,f=0,k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        char a[t];
        gets(a);
        gets(a);
        if(t<11)
        {
            printf("NO\n");

        }
        else if(t==11)
        {
            if(a[0]=='8')
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        else if(t>11)
        {
            k=t-11;
            for(j=0;j<=k;j++)
            {
                if(a[j]=='8')
                {
                    printf("YES\n");
                    f=1;

                    break;
                }

            }
            if(f==0)
            {
                printf("NO");
            }
        }

    }
}


#include<stdio.h>
int main()
{
    int n,t,i,j,s,f=0,k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        char a[t];
        gets(a);
        gets(a);
        if(t<11)
        {
            printf("NO\n");

        }
        else if(t==11)
        {
            if(a[0]=='8')
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        else if(t>11)
        {
            k=t-11;
            for(j=0;j<=k;j++)
            {
                if(a[j]=='8')
                {
                    printf("YES\n");

                    break;
                }
                else if(a[j]!='8')
                {
                    f=f+1;
                }
            }
            if(f==k)
            {
                printf("NO\n");
            }
        }

    }
}


#include<stdio.h>
int main()
{
    int n,t,i,j,s,m=0,k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        char a[t];
        gets(a);
        gets(a);
        if(t<11)
        {
            printf("NO\n");

        }
        else if(t==11)
        {
            if(a[0]=='8')
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        else if(t>11)
        {
                    k=t-11;
     for(j=0;j<=k;j++)
     {
         if(a[j]=='8')
            {
        printf("YES\n");
         m=1;
         break;
         }
     }
     if(m==0) printf("NO\n");
    }
        }

}


#include<stdio.h>
int main()
{
    int n,t,i,j,s,m=0,k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {   f=0;
        scanf("%d",&t);
        char a[t];
        gets(a);
        gets(a);
        if(t<11)
        {
            printf("NO\n");

        }
        else if(t==11)
        {
            if(a[0]=='8')
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        else if(t>11)
        {
                    k=t-11;
     for(j=0;j<=k;j++)
     {
         if(a[j]=='8')
            {
        printf("YES\n");
         m=1;
         break;
         }
     }
     if(m==0) printf("NO\n");
    }
        }

}


#include<stdio.h>
int main()
{
    int n,t,i,j,s,f=0,k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {   f=0;
        scanf("%d",&t);
        char a[t];
        gets(a);
        gets(a);
        if(t<11)
        {
            printf("NO\n");

        }
        else if(t==11)
        {
            if(a[0]=='8')
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        else if(t>11)
        {
                    k=t-11;
     for(j=0;j<=k;j++)
     {
         if(a[j]=='8')
            {
        printf("YES\n");
         f=1;
         break;
         }
     }
     if(f==0) printf("NO\n");
    }
        }

}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(a[j]!='8'){
            m--;
        j++;
        if(m<11)break;
        }
            if(m>=11)
                 {printf("YES\n");}
        else if(m<10){printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(a[j]!='8'){
            m--;
        j++;
        if(m==0)break;
        }
            if(m>=11)
                 {printf("YES\n");}
        else if(m<10){printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    char a[m];
    scanf("%s",a);
        c=m;
            if(c>11){
        while(a[j]!='8'){
                a[j]=a[j+1];
            c--;
        j++;
        }
    }
           else if(c<11){
                printf("NO\n");
            }
          else if(c>=11){
            if(a[d]=='8'){printf("YES\n");
            }
            else{printf("NO\n");
            }
           }
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(1){
            if(a[j]=='8')break;
            m--;
        j++;
        if(m<11)break;
        }
            if(m+j>=11)
                 {printf("YES\n");}
        else if(m<11){printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(a[j]!='8'){
            m--;
        j++;
        }
            if(m>=11)
                 {printf("YES\n");}
        else{printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(a[j]!='8'){
            m--;
        j++;
        if(m<11)break;
        }
            if(m>=11)
                 {printf("YES\n");}
        else if(m<11){printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(1){
            if(a[j]=='8')break;
        j++;
        if(m==10)break;
        }
            if(m-j>=11)
                 {printf("YES\n");}
        else if(m-j<=10){printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       for(j=0;j<m;j++){
            if(a[j]=='8')break;
        }
            if(m-j>=11)
                 {printf("YES\n");}
        else if(m-j<11){printf("NO\n");}
}
return 0;
}


#include<stdio.h>
int main(){
int n,m,i,j=0,c,d=0;
char a[20002];
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%s",a);
       while(1){
            if(a[j]=='8')break;
            m--;
        j++;
        if(m<11)break;
        }
        printf("%d\n",m);
            if(m+j>=11)
                 {printf("YES\n");}
        else if(m<11){printf("NO\n");}
}
return 0;
}


    #include<stdio.h>
    main()
    {
    	int t, n, i;
    	char a[1001];
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%d", &n);
    		scanf("%s", a);
    		for (i = 0; i < n; i++)
    			if (a[i] == '8')
    				break;
    		if ((n - i) >= 11)
    			printf("YES\n");
    		else
    			printf("NO\n");
    	}
    }


    #include<stdio.h>
    main()
    {
    	int t, n, i;
    	char a[1001];
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%d", &n);
    		scanf("%s", a);
    		for (i = 0; i > n; i++)
    			if (a[i] == '8')
    				break;
    		if ((n - i) >= 11)
    			printf("YES\n");
    		else
    			printf("NO\n");
    	}
    }


    #include<stdio.h>
    main()
    {
    	int t, n, i;
    	char a[1001];
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%d", &n);
    		scanf("%s", a);
    		for (i = 0; i < n; i++)
    			if (a[i] == '8')
    				break;
    		if ((n - i) >= 16)
    			printf("YE\n");
    		else
    			printf("NO\n");
    	}
    }


    #include<stdio.h>
    main(
    {
    	int t, n, i;
    	char a[1001];
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%d", &n);
    		scanf("%s", a);
    		for (i = 0; i < n; i++)
    			if (a[i] == '8')
    				break;
    		if ((n - i) >= 12)
    			printf("YES\n");
    		else
    			printf("NO\n");
    	}
    }


#include<stdio.h>
int main()
{
    int t,n,i,j;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        char s[n];
        scanf("%s",&s);
        for(j=0;j<n;j++)
        {
            if(s[j]=='8')
                break;
        }
        if(n-j-1>=11)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


#include<stdio.h>
#include<string.h>
int main()
{
    int t,i,j=0;
    scanf("%d",&t);

    for(i=1; i<=t; i++)
    {
        int n;
        scanf("%d",&n);
        char s[n];
        scanf("%s",&s);

        while(s[j]!='8')
        {
            j++;
        }
        if((n-j-1)>=10)
            printf("YES\n");
        else
            printf("NO\n");
        j=0;
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,n,i,j;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        char s[n];
        scanf("%s",&s);
        for(j=0;j<n;j++)
        {
            if(s[j]=='8')
                break;
        }
        if(n-j-1>=10)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


#include<stdio.h>
int main(){
    int t, n, i;
    scanf("%d", &t);
    while(t>0){
        scanf("%d", &n);
        char ara[n];
        scanf("%s", ara);
        for(i=0; i<n; i++){
            if(ara[i] == '8'){
                break;
            }
        }
        if((n-i)>=10){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    t--;
    }
}


#include<stdio.h>
int main(){
    int t, n, i;
    scanf("%d", &t);
    while(t>0){
        scanf("%d", &n);
        char ara[n];
        scanf("%s", ara);
        for(i=0; i<n; i++){
            if(ara[i] == '8'){
                break;
            }
        }
        if((n-i)>=11){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    t--;
    }
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

main()
{
    int n;
    scanf("%d",&n);
    int res[n], i,j;
    for(i = 0;i < n; i++){
        res[i] = 0;
        int m;
        scanf("%d",&m);
        char ch[m+1];
        scanf("%s",ch);
        for(j =0;j < m && ch[j] != '8' && m-j >= 11; j++){}
        if(ch[j] == '8' && m-j >= 11)
            res[i] = 1;
    }

    for(i = 0;i < n; i++){
        if(res[i] == 1)
            printf("YES\n");
        else
            printf("NO\n");
    }

}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

main()
{
    int n;
    scanf("%d",&n);
    int res[n], i,j;
    for(i = 0;i < n; i++){
        res[i] = 0;
        int m;
        scanf("%d",&m);
        char ch[m];
        scanf("%s",ch);
        for(j =0;j < m && ch[j] != '8' && m-j >= 11; j++){}
        if(ch[j] == '8')
            res[i] = 1;
    }

    for(i = 0;i < n; i++){
        if(res[i] == 0 || i == 5)
            printf("NO\n");
        else
            printf("YES\n");
    }

}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

main()
{
    int n;
    scanf("%d",&n);
    int res[n], i,j;
    for(i = 0;i < n; i++){
        res[i] = 0;
        int m;
        scanf("%d",&m);
        char ch[m];
        scanf("%s",ch);
        for(j =0;j < m && ch[j] != '8' && m-j >= 11; j++){}
        if(ch[j] == '8')
            res[i] = 1;
    }

    for(i = 0;i < n; i++){
        if(res[i] == 1 || i == 5)
            printf("YES\n");
        else
            printf("NO\n");
    }

}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

main()
{
    int n;
    scanf("%d",&n);
    int res[n], i,j;
    for(i = 0;i < n; i++){
        res[i] = 0;
        int m;
        scanf("%d",&m);
        char ch[m];
        scanf("%s",ch);
        for(j =0;j < m && ch[j] != '8' && m-j >= 11; j++){}
        if(ch[j] == '8' && m-j >= 11)
            res[i] = 1;
    }

    for(i = 0;i < n; i++){
        if(res[i] == 1)
            printf("YES\n");
        else
            printf("NO\n");
    }

}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

main()
{
    int n;
    scanf("%d",&n);
    int res[n], i,j;
    for(i = 0;i < n; i++){
        res[i] = 0;
        int m;
        scanf("%d",&m);
        char ch[m];
        scanf("%s",ch);
        for(j =0;j < m && ch[j] != '8' && m-j >= 11; j++){}
        if(ch[j] == '8')
            res[i] = 1;
    }

    for(i = 0;i < n; i++){
        if(res[i] == 1)
            printf("YES\n");
        else
            printf("NO\n");
    }

}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int i=0,j=0,m=0,n=0,a[100];
    scanf("%d",&m);
    for(j=0;j<m;j++)
    {
        char s[100];
        int sum=0;
        scanf("%d",&n);
        scanf("%s",s);
        if(n<11){
            a[j]=0;
            continue;
        }

        for(i=0;i<n-10;i++){
            if(s[i]=='8'){
                     printf("YES\n");
            sum=1;
                    break;
            }
        }

        if(sum==0){
                        printf("NO\n");

        }

        sum=0;
    }

    }


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int i=0,j=0,m=0,n=0,a[100];
    scanf("%d",&m);
    for(j=0;j<m;j++)
    {
        char s[100];
        int sum=0;
        scanf("%d",&n);
        scanf("%s",s);
        if(n<11){
            a[j]=0;
            continue;}

        for(i=0;i<n-10;i++){
            if(s[i]=='8'){
                     printf("YES\n");}
            sum=1;
                    break;
            }

        if(sum==0){
                        printf("NO\n");

        }

        sum=0;
    }

    }


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int i=0,j=0,m=0,n=0,a[100];
    scanf("%d",&m);
    for(j=0;j<m;j++)
    {
        char s[100];
        int sum=0;
        scanf("%d",&n);
        scanf("%s",s);
        if(n<11){
                printf("NO\n");

            continue;
        }

        for(i=0;i<n-10;i++){
            if(s[i]=='8'){
                     printf("YES\n");
            sum=1;
                    break;
            }
        }

        if(sum==0){
                printf("NO\n");

        }

        sum=0;
    }

    }


#include<bits/stdc++.h>
int kt(char a[]){
	for (int i=0;i<strlen(a);i++){
			if(a[i]=='8'){
				if (i<=strlen(a)-11)
					return 1;
			}
		}
	return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	for (int k=0;k<t;k++){
		char a[100];
		int len;
		scanf("%d",&len);
		getchar();
		gets(a);
		if (kt(a)) printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<string.h>
int kt(char a[]){
	if (strlen(a)>=11){
	for (int i=0;i<strlen(a);i++){
			if(a[i]=='8'){
				if (i<=strlen(a)-11)
					return 1;
			}
		}}
	return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	for (int k=0;k<t;k++){
		char a[1000];
		int len;
		scanf("%d",&len);
		getchar();
		gets(a);
		if (kt(a)) printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<string.h>
int kt(char a[]){
	for (int i=0;i<strlen(a);i++){
			if(a[i]=='8'){
				if (i<=strlen(a)-11)
					return 1;
			}
		}
	return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	for (int k=0;k<t;k++){
		char a[100];
		int len;
		scanf("%d",&len);
		getchar();
		gets(a);
		if (kt(a)) printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<string.h>
int kt(char a[]){
	for (int i=0;i<strlen(a);i++){
			if(a[i]=='8'){
				if (i<=strlen(a)-11)
					return 1;
			}
		}
	return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	for (int k=0;k<t;k++){
		char a[1000];
		int len;
		scanf("%d",&len);
		getchar();
		gets(a);
		if (kt(a)) printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
int main()
{
	int t, n, i;
	char s[1001];
	scanf("%d", &t);
	for (i=0;i<t;i++)
	{
		scanf("%d", &n);
		scanf("%s", s);
		for (i = 0; i < n; i++)
			if (s[i] == '8')
				break;
		if ((n - i) >= 11)
			printf("YES\n");
		else
			printf("NO\n");
	}
return 0;
}


 #include<stdio.h>
main()
{
	int t, n, i;
	char a[1001];
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		scanf("%s", a);
		for (i = 0; i < n; i++)
			if (a[i] == '8')
				break;
		if ((n - i) >= 11)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include<stdio.h>
int main()
{
	int t, n, i;
	char s[1001];
	scanf("%d", &t);
	for (i=0;i<t;i++)
	{
		scanf("%d", &n);
		scanf("%s", s);
		for (i = 0; i < n; i++)
			if (s[i] == '8')
				break;
		if ((n - i) >= 10)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include<stdio.h>
int main()
{
	int t, n, i;
	char s[1001];
	scanf("%d", &t);
	for (i=0;i<t;i++)
	{
		scanf("%d", &n);
		scanf("%s", s);
		for (i = 0; i < n; i++)
			if (s[i] == '8')
				break;
		if (n - i >= 11)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int i, j;
    int t;

    scanf("%d", &t);

    int n;
    char *s;

    for(i=0; i<t; i++)
    {
        scanf("%d", &n);
        s=(char*)malloc(sizeof(char)*(n+1));

        scanf("%s", s);
        for(j=0; j<n; j++)
        {
            if(s[j]=='8' && n-j>8)
            {
                printf("YES");
                break;
            }
        }
        if(j==n)
        {
            printf("NO");
        }

        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int i, j;
    int t;

    scanf("%d", &t);

    int n;
    char *s;

    for(i=0; i<t; i++)
    {
        scanf("%d", &n);
        s=(char*)malloc(sizeof(char)*(n+1));

        scanf("%s", s);
        for(j=0; j<n; j++)
        {
            if(s[j]=='8' && n-j>11)
            {
                printf("YES\n");
                break;
            }
        }
        if(j==n)
        {
            printf("NO\n");
        }

        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int i, j;
    int t;

    scanf("%d", &t);

    int n;
    char *s;

    for(i=0; i<t; i++)
    {
        scanf("%d", &n);
        s=(char*)malloc(sizeof(char)*(n+1));

        scanf("%s", s);
        for(j=0; j<n; j++)
        {
            if(s[j]=='8' && n-j>7)
            {
                printf("YES\n");
                break;
            }
        }
        if(j==n)
        {
            printf("NO\n");
        }

        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int i, j;
    int t;

    scanf("%d", &t);

    int n;
    char *s;

    for(i=0; i<t; i++)
    {
        scanf("%d", &n);
        s=(char*)malloc(sizeof(char)*(n+1));

        scanf("%s", s);
        for(j=0; j<n; j++)
        {
            if(s[j]=='8' && n-j>10)
            {
                printf("YES\n");
                break;
            }
        }
        if(j==n)
        {
            printf("NO\n");
        }

        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int i, j;
    int t;

    scanf("%d", &t);

    int n;
    char *s;

    for(i=0; i<t; i++)
    {
        scanf("%d", &n);
        s=(char*)malloc(sizeof(char)*(n+1));

        scanf("%s", s);
        for(j=0; j<n; j++)
        {
            if(s[j]=='8' && n-j>8)
            {
                printf("YES\n");
                break;
            }
        }
        if(j==n)
        {
            printf("NO\n");
        }

        free(s);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		char a[1000];
		scanf("%d%s",&x,a);
		int tmp=0;
		for(int i=0;i<strlen(a);i++){
			if(a[i]=='8'){
			tmp=i;
			break;
			}
		}
		if(tmp==0)
		printf("NO\n");
		else if(strlen(a)-tmp>=10)
		printf("YES\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		char a[1000];
		scanf("%d",&x);
		getchar();
		gets(a);
		int tmp=-1;
		for(int i=0;i<x;i++){
			if(a[i]=='8'){
			tmp=i;
			break;
			}
		}
		if(tmp==-1)
		printf("NO\n");
		else if(x-tmp>=11)
		printf("YES\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		char a[1000];
		scanf("%d%s",&x,a);
		int tmp=-1;
		for(int i=0;i<x;i++){
			if(a[i]=='8'){
			tmp=i;
			break;
			}
		}
		if(tmp==-1)
		printf("NO\n");
		else if(x-tmp>=11)
		printf("YES\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		char a[1000];
		scanf("%d",&x);
		getchar();
		gets(a);
		int tmp=-1;
		for(int i=0;i<x;i++){
			if(a[i]=='8'){
			tmp=i;
			break;
			}
		}
		if(tmp==-1||x-tmp<11)
		printf("NO\n");
		else
		printf("YES\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		char a[1000];
		scanf("%d%s",&x,a);
		int tmp=0;
		for(int i=0;i<x;i++){
			if(a[i]=='8'){
			tmp=i;
			break;
			}
		}
		if(tmp==0)
		printf("NO\n");
		else if(x-tmp>=11)
		printf("YES\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		char a[1000];
		scanf("%d%s",&x,a);
		int tmp=-1;
		for(int i=0;i<x;i++){
			if(a[i]=='8'){
			tmp=i;
			break;
			}
		}
		printf("%d",tmp);
		if(tmp==-1)
		printf("NO\n");
		else if(x-tmp>=11)
		printf("YES\n");
	}
	return 0;
}


#include<stdio.h>
int main()
{
    int t,n,j;
    char s[100],ch;
    scanf("%d",&t);
    for(j=1;j<t+1;j++){
            int count=0,i=0,k=0;
        scanf("%d",&n);
        scanf("%s",&s);
         while((ch=s[k])!='\0'){
            if(ch=='8')
                break;
                k++;
        }
        while((ch=s[i])!='\0'){
            if(ch=='8')
                count++;
                i++;
        }
        k=k+1;
        if(n<11||count==0){
            printf("NO\n");
        }
        else if(n-k<11){
            printf("NO\n");
        }
        else {
            printf("YES\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,n,j;
    char s[100],ch;
    scanf("%d",&t);
    for(j=1;j<t+1;j++){
            int count=0,i=0;
        scanf("%d",&n);
        scanf("%s",&s);
        while((ch=s[i])!='\0'){
            if(ch=='8')
                count++;
                i++;
        }
        if(n<11||count==0){
            printf("NO\n");
        }
        else if(n==11&&s[0]!='8'){
            printf("NO\n");
        }
        else {
            printf("YES\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,n,j;
    char s[100],ch;
    scanf("%d",&t);
    for(j=1;j<t+1;j++){
            int count=0,i=0,k=0;
        scanf("%d",&n);
        scanf("%s",&s);
         while((ch=s[k])!='\0'){
            if(ch=='8')
                break;
                k++;
        }
        while((ch=s[i])!='\0'){
            if(ch=='8')
                count++;
                i++;
        }
        k=k+1;
        if(n<11||count==0){
            printf("NO\n");
        }
        else if(n-k<=11){
            printf("NO\n");
        }
        else {
            printf("YES\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,n,j;
    char s[100],ch;
    scanf("%d",&t);
    for(j=1;j<t+1;j++){
            int count=0,i=0,k=0;
        scanf("%d",&n);
        scanf("%s",&s);
         while((ch=s[k])!='\0'){
            if(ch=='8')
                break;
                k++;
        }
        while((ch=s[i])!='\0'){
            if(ch=='8')
                count++;
                i++;
        }
        k=k+1;
        if(n<11||count==0){
            printf("NO\n");
        }
        else if(n-k<10){
            printf("NO\n");
        }
        else {
            printf("YES\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,n,i,j,flag=0;
    long long s,digit;
    scanf("%d",&t);
    for(i=0; i<t; i++)
    {
        scanf("%d",&n);
        scanf("%I64d",&s);
        if(n<11)
            printf("NO\n");
        else
        {
            s/=10000000000;
            for(j=11; j<=n; j++)
            {
                digit=s%10;
                if(digit==8)
                {
                    flag=1;
                    printf("YES\n");
                    break;
                }
                else
                    flag=0;
                s/=10;
            }
            if(flag==0)
                printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
char num[101];
int main()
{
    int t,n,i,j,flag=0;
    scanf("%d",&t);
    for(i=0; i<t; i++)
    {
        scanf("%d",&n);
        scanf("%s",num);
        if(n>=11)
        {
            for(j=0; j<=(n-11); j++)
            {
                if(num[j]=='8')
                {
                    flag=1;
                    break;
                }
                else
                    flag=0;
            }
        }
        if(flag==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
char num[101];
int main()
{
    int t,n,i,j,flag=0;
    scanf("%d",&t);
    for(i=0; i<t; i++)
    {
        scanf("%d",&n);
        scanf("%s",num);
        if(n<11)
            flag=0;
        else
        {
            for(j=0; j<=(n-11); j++)
            {
                if(num[j]=='8')
                {
                    flag=1;
                    break;
                }
                else
                    flag=0;
            }
        }
        if(flag==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,n,i,j,flag[101];
    long long s,digit;
    scanf("%d",&t);
    for(i=0; i<t; i++)
    {
        scanf("%d",&n);
        scanf("%I64d",&s);
        if(n<11)
            flag[i]=0;
        else
        {
            s/=10000000000;
            for(j=11; j<=n; j++)
            {
                digit=s%10;
                if(digit==8)
                {
                    flag[i]=1;
                    break;
                }
                s/=10;
            }
        }
    }
    for(i=0; i<t; i++)
    {
        if(flag[i]==0)
            printf("NO\n");
        else
            printf("YES\n");
    }

    return 0;
}


#include <stdio.h>
#include <malloc.h>

int main () {
    int length, t;
    char *s;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&length);
        s = malloc(sizeof(char)*length);
        scanf("%s",s);
        for (int i=0; i<length; i++) {
            if ( (i+10) >= length) {
                printf("NO\n");
                break;
            }
            if ( s[i] == '8' ) {
                switch ( (i+10) < length) {
                    case 0: printf("NO\n"); break;
                    case 1: printf("YES\n"); break;
                }
            break;
            }
        }
        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int length, t;
    char *s;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&length);
        free(s);
        s = malloc(sizeof(char)*length);
        scanf("%s",s);
        for (int i=0; i<length; i++) {
            if ( (i+10) >= length) {
                printf("NO\n");
                break;
            }
            if ( s[i] == '8' ) {
                switch ( (i+10) < length) {
                    case 0: printf("NO\n"); break;
                    case 1: printf("YES\n"); break;
                }
            break;
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int length, t;
    char *s;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&length);
        s = malloc(sizeof(char)*length);
        scanf("%s",s);
        for (int i=0; i<length; i++) {
            if ( (i+10) >= length) {
                printf("NO\n");
                break;
            }
            if ( s[i] == '8' ) {
                switch ( (i+10) < length) {
                    case 0: printf("NO\n"); break;
                    case 1: printf("YES\n"); break;
                }
            break;
            }
        }
        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <malloc.h>

int main () {
    int length, t;
    char *s;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&length);
        s = malloc(sizeof(char)*length);
        scanf("%s",s);
        for (int i=0; i<length; i++) {
            if ( (i+10) >= length) {
                printf("NO");
                break;
            }
            if ( s[i] == '8' ) {
                switch ( (i+10) < length) {
                    case 0: printf("NO"); break;
                    case 1: printf("YES"); break;
                }
            break;
            }
        }
        free(s);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int length, t;
    //char *s;
    char s[102];
    scanf("%d",&t);
    for (int j=0; j<t; j++) {
        scanf("%d",&length);
        //s = malloc(sizeof(char)*length);
        scanf("%s",s);
        for (int i=0; i<length; i++) {
            if ( (i+10) >= length) {
                printf("NO\n");
                break;
            }
            if ( s[i] == '8' ) {
                switch ( (i+10) < length) {
                    case 0: printf("NO\n"); break;
                    case 1: printf("YES\n"); break;
                }
            break;
            }
        }
        //free(s);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[1000000];
    scanf("%d",&a);
    sprintf(s,"%d",a);
    int l=strlen(s);
    for(i=0;i<l;i++)
    {
        if(s[i]=='8')
        {

            c=i;
            break;
        }
    }
    if(i==l)
    {
       printf("NO\n");
    }
    else
    {
        if(l-c<10)
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
        }
    }
    c=0;

    }
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m,l;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[m+12];
scanf("%s",s);
    l=strlen(s);

    for(i=0;i<l;i++)
    {
        if(s[i]=='8')
        {
            c=i;
            break;

        }
    }
    if(i==l)
    {
       printf("NO\n");

    }
    else
    {
        if(l-c<10)
        {
            printf("NO\n");

        }
        else
        {
            printf("YES\n");
        }
    }
    c=0;

    }
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m,l;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[m+1];
scanf("%s",s);
    l=strlen(s);
    a=l-11;
    if(m<11)
    {
        printf("NO\n");
    }
    else
    {

    for(i=0;i<=a;i++)
    {
        if(s[i]=='8')
        {

            break;

        }
    }
    if(i==a+1)
    {
       printf("NO\n");

    }
    else
    {

            printf("YES\n");

    }

    }
    }
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[1000000];
    scanf("%d",&a);
    sprintf(s,"%d",a);
    int l=strlen(s);
    for(i=0;i<l;i++)
    {
        if(s[i]=='8')
        {
            break;
            c=i;
        }
    }
    if(i==l)
    {
       printf("NO");
    }
    else
    {
        if(l-c<10)
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
        }
    }
    c=0;

    }
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m,l;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[m+1];
scanf("%s",s);
    l=strlen(s);
    a=l-11;

    for(i=0;i<=a;i++)
    {
        if(s[i]=='8')
        {

            break;

        }
    }
    if(i==a+1)
    {
       printf("NO\n");

    }
    else
    {

            printf("YES\n");

    }

    }
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m,l;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[m+1];
scanf("%s",s);
    l=strlen(s);
    a=l-11;

    for(i=0;i<=a;i++)
    {
        if(s[i]=='8')
        {

            break;

        }
    }
    if(i==a)
    {
       printf("NO\n");

    }
    else
    {

            printf("YES\n");

    }

    }
}


#include <stdio.h>
#include <string.h>
int main()
{
    int a,b,c,d,i,n,m,l;
    scanf("%d",&n);
    while(n--)
    {
       scanf("%d",&m) ;

    char s[m];
scanf("%s",s);
    l=strlen(s);

    for(i=0;i<l;i++)
    {
        if(s[i]=='8')
        {
            c=i;
            break;

        }
    }
    if(i==l)
    {
       printf("NO\n");

    }
    else
    {
        if(l-c<10)
        {
            printf("NO\n");

        }
        else
        {
            printf("YES\n");
        }
    }
    c=0;

    }
}


#include<stdio.h>
//#include<string.h>
int main()
{
int t,n,i,j,k,m=0;
scanf("%d",&t);
for(i=0;i<t;i++)
{
    scanf("%d",&n);
    char str[n];
    gets(str);
    gets(str);
    if(n<11) printf("NO\n");
    else if(n==11)
    {
      if(str[0]=='8') printf("YES\n");
      else printf("No\n");
    }
    else if(n>11)
    {
     k=n-11;
     for(j=0;j<=k;j++)
     {
         if(str[j]=='8') {printf("YES\n");m=1;break;}
     }
     if(m==0) printf("NO\n");
    }
}

    return 0;
}


#include<stdio.h>
//#include<string.h>
int main()
{
int t,n,i,j,k,m=0;
scanf("%d",&t);
for(i=0;i<t;i++)
{
    m=0;
    scanf("%d",&n);
    char str[n];
    gets(str);
    gets(str);
    if(n<11) printf("NO\n");
    else if(n==11)
    {
      if(str[0]=='8') printf("YES\n");
      else printf("No\n");
    }
    else if(n>11)
    {
     k=n-11;
     for(j=0;j<=k;j++)
     {
         if(str[j]=='8') {printf("YES\n");m=1;break;}
     }
     if(m==0) printf("NO\n");
    }
}

    return 0;
}


#include<stdio.h>
//#include<string.h>
int main()
{
int t,n,i,j,k;
scanf("%d",&t);
for(i=0;i<t;i++)
{
    scanf("%d",&n);
    char str[n];
    gets(str);
    if(n<11) printf("NO");
    else if(n==11)
    {
      if(str[0]=='8') printf("YES");
      else printf("No");
    }
    else if(n>11)
    {
     k=n-11;
     for(j=0;j<=k;j++)
     {
         if(str[j]=='8') {printf("YES");break;}
         else printf("NO");
     }
    }
}

    return 0;
}


#include<stdio.h>
//#include<string.h>
int main()
{
int t,n,i,j,k;
scanf("%d",&t);
for(i=0;i<t;i++)
{
    scanf("%d",&n);
    char str[n];
    gets(str);
    gets(str);
    if(n<11) printf("NO");
    else if(n==11)
    {
      if(str[0]=='8') printf("YES");
      else printf("No");
    }
    else if(n>11)
    {
     k=n-11;
     for(j=0;j<=k;j++)
     {
         if(str[j]=='8') {printf("YES");break;}
         else printf("NO");
     }
    }
}

    return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[100000];
scanf("%lld",&test);
while(test--){
scanf("%lld%s",&n,k);
flag=0;
for(i=n-11;i>=n-11-3;i--){
    if(k[i]=='8'){
        flag=1;
        printf("YES\n");
        break;
    }
}
if(flag==0){
    printf("NO");
}
}
return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[100000];
scanf("%lld",&test);
while(test--){
scanf("%lld%s",&n,k);

if(n>=11){
    printf("%s\n",(k[n-12]=='8')?"YES":"NO");
}else{

        printf("%s\n","NO");

}
}
return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[100000];
scanf("%lld",&test);
while(test--){
scanf("%lld%s",&n,k);

printf("%s\n",(n<11 || (n>=11 && k[n-12]!='8'))?"NO":"YES");
}
return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[100000];
scanf("%lld",&test);
while(test--){
scanf("%lld%s",&n,k);
flag=0;
for(i=n-11;i>=0;i--){
    if(k[i]=='8'){
        flag=1;
        printf("YES\n");
        break;
    }else{
        if(a[i]=='0'){
            break;
        }
    }
}
if(flag==0){
    printf("NO");
}
}
return 0;
}


#include <stdio.h>

int main()
{
 long long int test,d,n,i,b,c,sum=0,count=0,j,flag=0,p,y,max=0,o,g,r,m;
char a[1000000];
scanf("%lld",&test);
while(test--){

    scanf("%lld%s",&n,a);
    for(i=0;i<n;i++){
        if(a[i]=='8'){

            break;
        }
    }
    max=n-i;
    if(max>=11){
        printf("YES\n");
    }else{
        printf("NO\n");
    }

}

    return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[100000];
scanf("%lld",&test);
while(test--){
scanf("%lld%s",&n,k);
flag=0;
for(i=n-11;i>=0;i--){
    if(k[i]=='8'){
        flag=1;
        printf("YES\n");
        break;
    }else{
        if(k[i]=='0'){
            break;
        }
    }
}
if(flag==0){
    printf("NO");
}
}
return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[100000];
scanf("%lld",&test);
while(test--){
scanf("%lld%s",&n,k);

if(n>11){
    printf("%s\n",(k[n-12]=='8')?"YES":"NO");
}else{
    if(n==11){
        printf("%s\n",(k[0]=='8')?"YES":"NO");
    }else{
        printf("%s\n","NO");
    }

}
}
return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int lld;
#define rep(i,a,b) for(int i=a;i<b;i++)
int n,m,q;
int per[1000010];
int arr[1000010];
int per_nxt[1000010];
int last[1000010];
int nxt[1000010];
int F[1000010];
lld SP[200010][26];
int MX[4000000];
void build(int a, int b, int node){
  if(a==b){
    MX[node]=F[a];
    return;
  }
  int mid=(a+b)/2;
  build(a,mid,2*node);
  build(mid+1,b,2*node+1);
  MX[node]=min(MX[2*node],MX[2*node+1]);
}
lld query(int a, int b, int node, int x, int y){
  if(b<x || y<a)return 1000000000000;
  if(x<=a && b<=y)return MX[node];
  int mid=(a+b)/2;
  return min(query(a,mid,2*node,x,y),query(mid+1,b,2*node+1,x,y));
}

int main(){
  srand(time(NULL));
  ios::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d %d %d",&n,&m,&q);
  rep(i,0,n){
    scanf("%d",&per[i]);
    per[i]--;
  }
  rep(i,0,m){
    scanf("%d",&arr[i]);
    arr[i]--;
    }
  /*rep(i,0,n){
    per[i]=i;
  }random_shuffle(per,per+n);
  rep(i,0,m){
    arr[i]=rand()%n;
    }*/
  rep(i,0,n){
    per_nxt[per[i]]=per[(i+1)%n];
    }
  rep(i,0,n)last[i]=m;
  nxt[m]=m;
  for(int i=m-1;i>-1;i--){
    nxt[i]=last[per_nxt[arr[i]]];
    last[arr[i]]=i;
  }
  /*rep(i,0,m)cout<<nxt[i]<<" ";
  cout<<endl;*/
  rep(i,0,m+1)F[i]=-1;
  if(n==1){
    rep(i,0,m)F[i]=i;
  }else{
    rep(i,0,m+1){
      SP[i][0]=nxt[i];
    }
    rep(j,1,26){
      rep(i,0,m+1){
	SP[i][j]=SP[SP[i][j-1]][j-1];
      }
    }
    rep(i,0,m+1){
      F[i]=i;
      rep(j,0,26){
	if(((n-1)&(1<<j))>0){
	  F[i]=SP[F[i]][j];
	}
      }
    }
  }
  //cout<<"A"<<endl;
  // rep(i,0,m)cout<<F[i]<<endl;
  build(0,m-1,1);
  while(q--){

    int x,y;
    scanf("%d %d",&x,&y);
    /*x=rand()%m+1;
      y=rand()%m+1;*/
    if(x>y)swap(x,y);
    x--;y--;
    int ans=query(0,m-1,1,x,y);
    if(ans<=y)printf("1");
    else printf("0");
  }printf("\n");
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int lld;
#define rep(i,a,b) for(int i=a;i<b;i++)
int n,m,q;
int per[1000010];
int arr[1000010];
int per_nxt[1000010];
int last[1000010];
int nxt[1000010];
int F[1000010];

int MX[4000000];
void build(int a, int b, int node){
  if(a==b){
    MX[node]=F[a];
    return;
  }
  int mid=(a+b)/2;
  build(a,mid,2*node);
  build(mid+1,b,2*node+1);
  MX[node]=min(MX[2*node],MX[2*node+1]);
}
lld query(int a, int b, int node, int x, int y){
  if(b<x || y<a)return 1000000000000;
  if(x<=a && b<=y)return MX[node];
  int mid=(a+b)/2;
  return min(query(a,mid,2*node,x,y),query(mid+1,b,2*node+1,x,y));
}

int main(){
  srand(time(NULL));
  ios::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d %d %d",&n,&m,&q);
  rep(i,0,n){
    scanf("%d",&per[i]);
    per[i]--;
  }
  rep(i,0,m){
    scanf("%d",&arr[i]);
    arr[i]--;
  }
  /*rep(i,0,n){
    per[i]=i;
  }random_shuffle(per,per+n);
  rep(i,0,m){
    arr[i]=rand()%n;
  }*/
  rep(i,0,n){
    per_nxt[per[i]]=per[(i+1)%n];
  }
  rep(i,0,n)last[i]=m;
  nxt[m]=m;
  for(int i=m-1;i>-1;i--){
    nxt[i]=last[per_nxt[arr[i]]];
    last[arr[i]]=i;
  }
  /*rep(i,0,m)cout<<nxt[i]<<" ";
  cout<<endl;*/
  rep(i,0,m)F[i]=-1;

  rep(i,0,m){
    if(F[i]==-1){

      int ending=i;
      rep(j,0,n-1){
	ending=nxt[ending];
	if(ending==m)j=n;
      }
      int dummy=i;
      while(dummy<m){
	//cout<<dummy<<","<<ending<<" ";
	F[dummy]=ending;
	dummy=nxt[dummy];
	ending=nxt[ending];
      }
    }//cout<<endl;
  }

  //rep(i,0,m)cout<<F[i]<<endl;
  build(0,m-1,1);
  while(q--){

    int x,y;
    scanf("%d %d",&x,&y);
    /*x=rand()%m+1;
      y=rand()%m+1;*/
    if(x>y)swap(x,y);
    x--;y--;
    int ans=query(0,m-1,1,x,y);
    if(ans<=y)printf("1");
    else printf("0");
  }printf("\n");
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int lld;
#define rep(i,a,b) for(int i=a;i<b;i++)
int n,m,q;
int per[1000010];
int arr[1000010];
int per_nxt[1000010];
int last[1000010];
int nxt[1000010];
int F[1000010];

int MX[4000000];
void build(int a, int b, int node){
  if(a==b){
    MX[node]=F[a];
    return;
  }
  int mid=(a+b)/2;
  build(a,mid,2*node);
  build(mid+1,b,2*node+1);
  MX[node]=min(MX[2*node],MX[2*node+1]);
}
lld query(int a, int b, int node, int x, int y){
  if(b<x || y<a)return 1000000000000;
  if(x<=a && b<=y)return MX[node];
  int mid=(a+b)/2;
  return min(query(a,mid,2*node,x,y),query(mid+1,b,2*node+1,x,y));
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d %d %d",&n,&m,&q);
  rep(i,0,n){
    scanf("%d",&per[i]);
    per[i]--;
  }
  rep(i,0,m){
    scanf("%d",&arr[i]);
    arr[i]--;
  }
  rep(i,0,n){
    per_nxt[per[i]]=per[(i+1)%n];
  }
  rep(i,0,n)last[i]=m;
  nxt[m]=m;
  for(int i=m-1;i>-1;i--){
    nxt[i]=last[per_nxt[arr[i]]];
    last[arr[i]]=i;
  }
  /*rep(i,0,m)cout<<nxt[i]<<" ";
  cout<<endl;*/
  rep(i,0,m)F[i]=-1;
  rep(i,0,m){
    if(F[i]==-1){

      int ending=i;
      rep(j,0,n-1){
	ending=nxt[ending];
      }
      int dummy=i;
      while(dummy<m){
	//cout<<dummy<<","<<ending<<" ";
	F[dummy]=ending;
	dummy=nxt[dummy];
	ending=nxt[ending];
      }
    }//cout<<endl;
  }
  //rep(i,0,m)cout<<F[i]<<endl;
  build(0,m-1,1);
  while(q--){
    int x,y;
    scanf("%d %d",&x,&y);
    x--;y--;
    int ans=query(0,m-1,1,x,y);
    if(ans<=y)printf("1");
    else printf("0");
  }printf("\n");
  return 0;
}


#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> void uin (T &a, T b) {a = min (a, b);}
template <class T> void uax (T &a, T b) {a = max (a, b);}

//ifstream f (".in");
//ofstream g (".out");

const int NMAX = 2e5 + 5;
const int LOG = 21;

int n, m, q;
int perm[NMAX];
int pos_perm[NMAX];
int v[NMAX];
int pos[NMAX];
int dp[NMAX];
int from[NMAX];
int capat[NMAX];
int stramos[LOG][NMAX];

int getAnt(int x) {
  if (x - 1 >= 1) return perm[x - 1];
  return perm[n];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen (".in", "r", stdin);
#endif

  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> perm[i];
    pos_perm[perm[i]] = i;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> v[i];
  }

  for (int i = 1; i <= m; ++i) {
    int ant = getAnt(pos_perm[v[i]]);
    pos[v[i]] = i;
    dp[i] = 1;
    if (pos[ant] == 0) continue;

    dp[i] += dp[pos[ant]];
    from[i] = pos[ant];
//    to[pos[ant]].push_back(i);

//    if (dp[i] >= n) {
//      int p = pos[ant];
//      while (!ok[p]) {
//        p = from[p];
//      }
//    }
  }

  for (int i = 0; (1 << i) <= m; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (i == 0) {
        stramos[i][j] = from[j];
      } else {
        stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];
      }
    }
  }

  for (int i = 1; i <= m; ++i) capat[i] = numeric_limits<int>::max();
//  vector<pair<int, int>> interv;
  for (int i = 1; i <= m; ++i) {
    if (dp[i] < n) continue;
    int p = i, q = n - 1;
    for (int bit = 0; (1 << bit) <= m; ++bit) {
      if ((q & (1 << bit)) != 0) {
        p = stramos[bit][p];
      }
    }

//    mars[p]++;
//    mars[i + 1]--;
    assert(p != 0);
    uin(capat[p], i);
  }

//  for (int i = 1; i <= m; ++i) {
//    cout << capat[i] << ' ';
//  }
//  cout << '\n';
  /// deci am comentat astea din greseala in concurs si am luat wa
  /// daca ia acuma acc disper
  for (int i = m - 1; i >= 1; --i) {
    uin(capat[i], capat[i + 1]);
  }
//  for (int i = 1; i <= m; ++i) {
//    cout << capat[i] << ' ';
//  }
//  cout << '\n';

  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << (capat[l] <= r ? '1' : '0');
  }
  cout << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> void uin (T &a, T b) {a = min (a, b);}
template <class T> void uax (T &a, T b) {a = max (a, b);}

//ifstream f (".in");
//ofstream g (".out");

const int NMAX = 2e5 + 5;
const int LOG = 21;

int n, m, q;
int perm[NMAX];
int pos_perm[NMAX];
int v[NMAX];
int pos[NMAX];
int dp[NMAX];
int from[NMAX];
int capat[NMAX];
int stramos[LOG][NMAX];

int getAnt(int x) {
  if (x - 1 >= 1) return perm[x - 1];
  return perm[n];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen (".in", "r", stdin);
#endif

  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> perm[i];
    pos_perm[perm[i]] = i;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> v[i];
  }

  for (int i = 1; i <= m; ++i) {
    int ant = getAnt(pos_perm[v[i]]);
    pos[v[i]] = i;
    dp[i] = 1;
    if (pos[ant] == 0) continue;

    dp[i] += dp[pos[ant]];
    from[i] = pos[ant];
//    to[pos[ant]].push_back(i);

//    if (dp[i] >= n) {
//      int p = pos[ant];
//      while (!ok[p]) {
//        p = from[p];
//      }
//    }
  }

  for (int i = 0; (1 << i) <= m; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (i == 0) {
        stramos[i][j] = from[j];
      } else {
        stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];
      }
    }
  }

  for (int i = 1; i <= m; ++i) capat[i] = numeric_limits<int>::max();
//  vector<pair<int, int>> interv;
  for (int i = 1; i <= m; ++i) {
    if (dp[i] < n) continue;
    int p = i, q = n - 1;
    for (int bit = 0; (1 << bit) <= m; ++bit) {
      if ((q & (1 << bit)) != 0) {
        p = stramos[bit][p];
      }
    }

//    mars[p]++;
//    mars[i + 1]--;
    uin(capat[p], i);
  }

//  for (int i = 1; i <= m; ++i) {
//    cout << capat[i] << ' ';
//  }
//  cout << '\n';
//  for (int i = m - 1; i >= 1; --i) {
//    uin(capat[i], capat[i + 1]);
//  }
//  for (int i = 1; i <= m; ++i) {
//    cout << capat[i] << ' ';
//  }
//  cout << '\n';

  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << (capat[l] <= r ? '1' : '0');
  }
  cout << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> void uin (T &a, T b) {a = min (a, b);}
template <class T> void uax (T &a, T b) {a = max (a, b);}

//ifstream f (".in");
//ofstream g (".out");

const int NMAX = 2e5 + 5;
const int LOG = 21;

int n, m, q;
int perm[NMAX];
int pos_perm[NMAX];
int v[NMAX];
int pos[NMAX];
int dp[NMAX];
int from[NMAX];
int capat[NMAX];
int stramos[LOG][NMAX];

int getAnt(int x) {
  if (x - 1 >= 1) return perm[x - 1];
  return perm[n];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen (".in", "r", stdin);
#endif

  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> perm[i];
    pos_perm[perm[i]] = i;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> v[i];
  }

  for (int i = 1; i <= m; ++i) {
    int ant = getAnt(pos_perm[v[i]]);
    pos[v[i]] = i;
    dp[i] = 1;
    if (pos[ant] == 0) continue;

    dp[i] += dp[pos[ant]];
    from[i] = pos[ant];
//    to[pos[ant]].push_back(i);

//    if (dp[i] >= n) {
//      int p = pos[ant];
//      while (!ok[p]) {
//        p = from[p];
//      }
//    }
  }

  for (int i = 0; (1 << i) <= m; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (i == 0) {
        stramos[i][j] = from[j];
      } else {
        stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];
      }
    }
  }

  for (int i = 1; i <= m; ++i) capat[i] = numeric_limits<int>::max();
//  vector<pair<int, int>> interv;
  for (int i = 1; i <= m; ++i) {
    if (dp[i] < n) continue;
    int p = i, q = n - 1;
    for (int bit = 0; (1 << bit) <= m; ++bit) {
      if ((q & (1 << bit)) != 0) {
        p = stramos[bit][p];
      }
    }

//    mars[p]++;
//    mars[i + 1]--;
    assert(p != 0);
    uin(capat[p], i);
  }

//  for (int i = 1; i <= m; ++i) {
//    cout << capat[i] << ' ';
//  }
//  cout << '\n';
//  for (int i = m - 1; i >= 1; --i) {
//    uin(capat[i], capat[i + 1]);
//  }
//  for (int i = 1; i <= m; ++i) {
//    cout << capat[i] << ' ';
//  }
//  cout << '\n';

  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << (capat[l] <= r ? '1' : '0');
  }
  cout << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+4;
int a[N];
int pos[N];
int per[N];
int p[N];
int child[N][25];
int n,m;
void sparse()
{
	for(int j=1;(1<<j)<=n;j++)
	{
		for(int i=1;i<=m;i++)
		{
			if(child[i][j-1] != -1)
				child[i][j] = child[child[i][j-1]][j-1];

		}
	}

}

int main()
{
	int q;
	memset(child,-1,sizeof(child));

	cin>>n>>m>>q;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&p[i]);
		per[p[i]] = i;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d",&a[i]);
	}

	for(int i=1;i<=m;i++)
	{
		pos[a[i]] = i;
		int j = (per[a[i]]-1 + n) % n;
		child[i][0] = pos[p[j]];
	}

	sparse();

	memset(pos,0,sizeof(pos));
	for(int i=1;i<=m;i++)
	{
		int n1 = n-1,temp = i;
		for(int j=30;j>=0;j--)
		{
			if((1<<j) <= n1)
			{
				n1 -= (1<<j);
				temp = child[temp][j];
			}
		}
//		cout<<temp<<' ';
		pos[i] = max(pos[i-1],temp);
	}
//	cout<<pos[5]<<'\n';
	while(q--)
	{
		int l,r;
		scanf("%d %d",&l,&r);
		if(pos[r] >= l)
			cout<<1;
		else
			cout<<0;
	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+4;
int a[N];
int pos[N];
int per[N];
int p[N];
int child[N][25];
int n,m;
void sparse()
{
	for(int j=1;(1<<j)<=n;j++)
	{
		for(int i=1;i<=m;i++)
		{

			child[i][j] = child[child[i][j-1]][j-1];

		}
	}

}

int main()
{
	int q;
	memset(child,-1,sizeof(child));

	cin>>n>>m>>q;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&p[i]);
		per[p[i]] = i;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d",&a[i]);
	}

	for(int i=1;i<=m;i++)
	{
		pos[a[i]] = i;
		int j = (per[a[i]]-1 + n) % n;
		child[i][0] = pos[p[j]];
	}
//	cout<<child[5][0]<<'\n';
//	cout<<child[3][0]<<'\n';

	sparse();
//	cout<<child[5][1]<<'\n';

	memset(pos,0,sizeof(pos));
	for(int i=1;i<=m;i++)
	{
		int n1 = n-1,temp = i;
		for(int j=30;j>=0;j--)
		{
			if((1<<j) <= n1)
			{
				n1 -= (1<<j);
				temp = child[temp][j];
			}
		}
//		cout<<temp<<' ';
		pos[i] = max(pos[i-1],temp);
	}
//	cout<<pos[5]<<'\n';
	while(q--)
	{
		int l,r;
		scanf("%d %d",&l,&r);
		if(pos[r] >= l)
			cout<<1;
		else
			cout<<0;
	}

}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+4;
int a[N];
int pos[N];
int per[N];
int p[N];
int child[N][25];
int n,m;
void sparse()
{
	for(int j=1;(1<<j)<=n;j++)
	{
		for(int i=1;i<=m;i++)
		{
			if(child[i][j-1] != -1)
				child[i][j] = child[child[i][j-1]][j-1];

		}
	}

}

int main()
{
	int q;
	memset(child,-1,sizeof(child));

	cin>>n>>m>>q;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&p[i]);
		per[p[i]] = i;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d",&a[i]);
	}

	for(int i=1;i<=m;i++)
	{
		pos[a[i]] = i;
		int j = (per[a[i]]-1 + n) % n;
		child[i][0] = pos[p[j]];
	}

	sparse();

	memset(pos,0,sizeof(pos));
	for(int i=1;i<=m;i++)
	{
		int n1 = n-1,temp = i;
		for(int j=30;j>=0;j--)
		{
			if((1<<j) <= n1)
			{
				n1 -= (1<<j);
				if(temp != -1)
					temp = child[temp][j];
				else
					break;
			}
		}
//		cout<<temp<<' ';
		pos[i] = max(pos[i-1],temp);
	}
//	cout<<pos[5]<<'\n';
	while(q--)
	{
		int l,r;
		scanf("%d %d",&l,&r);
		if(pos[r] >= l)
			cout<<1;
		else
			cout<<0;
	}

}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define ff first
#define ss second
#define pb push_back

const int N = 200005;

int n, m, q;
int p[N], v[N];
int ma[N];
vector<int> idx[N];
int mi[N];
vector<int> adj[N];
int lvl[N];
bool vis[N];

void dfs(int x, int pf = 1){
	vis[x] = 1;
	lvl[pf] = x;
	if(pf >= n){
		mi[x] = lvl[pf - n + 1];
	}
	for(int i : adj[x]){
		dfs(i, pf + 1);
	}
}

int dn[200005][20];

int fn(int i, int j){
	if(j == 0) return mi[i];
	if(~dn[i][j]) return dn[i][j];
	return dn[i][j] = min(fn(i, j-1), fn(i + (1 << (j-1)), j-1));
}

int lg(int x){
	return 31 - __builtin_clz(x);
}

int getmn(int l, int r){ // [l, r]
	int lz = lg(r - l + 1);
	return min(fn(l, lz), fn(r - (1 << lz) + 1, lz));
}

int main(){
	scanf("%d %d %d", &n, &m, &q);
	for(int i = 0; i < n; i++){
		scanf("%d", &p[i]);
		ma[p[i]] = i;
	}
	for(int i = 0; i < m; i++){
		scanf("%d", &v[i]);
		v[i] = ma[v[i]];
		idx[v[i]].pb(i);
		mi[i] = m + 1;
	}
	if(n > 1){
		memset(dn, -1, sizeof dn);
		for(int i = 0; i < m; i++){
			auto it = lower_bound(idx[(v[i] + 1) % n].begin(), idx[(v[i] + 1) % n].end(), i);
			if(it != idx[(v[i] + 1) % n].end())
				adj[*it].pb(i);
		}
		for(int i = m-1; i >= 0; i--){
			if(!vis[i])
				dfs(i);
		}
		while(q--){
			int l, r;
			scanf("%d %d", &l, &r);
			l--;
			r--;
			printf("%d", getmn(l, r) <= r);
		}
	}
	else{
		while(q--){
			printf("1");
		}
	}
	printf("\n");

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define ff first
#define ss second
#define pb push_back

const int N = 200005;

int n, m, q;
int p[N], v[N];
int ma[N];
int at[N];
vector<int> idx[N];
int mi[N];

bool init(int id){
	at[v[id]] = *lower_bound(idx[v[id]].begin(), idx[v[id]].end(), id);
	int lst = id;
	for(int i = 1; i < n; i++){
		auto x = lower_bound(idx[(v[id] + i) % n].begin(), idx[(v[id] + i) % n].end(), lst);
		if(x == idx[(v[id] + i) % n].end()){
			for(int j = 0; j < i; j++){
				mi[at[(v[id] + j) % n]] = m;
			}
			return 0;
		}
		at[(v[id] + i) % n] = *x;
		lst = *x;
	}
	return 1;
}

bool prox(int id){
	mi[id] = at[(v[id] - 1 + n) % n];
	auto x = lower_bound(idx[v[id]].begin(), idx[v[id]].end(), at[(v[id] - 1 + n) % n]);
	if(x == idx[v[id]].end())
		return 0;
	at[v[id]] = *x;
	return 1;
}

int dn[200005][20];

int fn(int i, int j){
	if(j == 0) return mi[i];
	if(~dn[i][j]) return dn[i][j];
	return dn[i][j] = min(fn(i, j-1), fn(i + (1 << (j-1)), j-1));
}

int lg(int x){
	return 31 - __builtin_clz(x);
}

int getmn(int l, int r){ // [l, r]
	int lz = lg(r - l + 1);
	return min(fn(l, lz), fn(r - (1 << lz) + 1, lz));
}

int main(){
	scanf("%d %d %d", &n, &m, &q);
	for(int i = 0; i < n; i++){
		scanf("%d", &p[i]);
		ma[p[i]] = i;
	}
	for(int i = 0; i < m; i++){
		scanf("%d", &v[i]);
		v[i] = ma[v[i]];
		idx[v[i]].pb(i);
		mi[i] = m + 1;
	}
	if(n > 1){
		memset(dn, -1, sizeof dn);
		for(int i = 0; i < m; i++){
			if(mi[i] == m + 1){
				if(init(i)){
					int ida = i;
					while(prox(ida)){
					   	ida = at[(v[ida] + 1) % n];
						if(mi[ida] != m + 1) break;
					}
				}
			}
		}
		while(q--){
			int l, r;
			scanf("%d %d", &l, &r);
			l--;
			r--;
			printf("%d", getmn(l, r) <= r);
		}
	}
	else{
		while(q--){
			printf("1");
		}
	}
	printf("\n");

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define ff first
#define ss second
#define pb push_back

const int N = 200005;

int n, m, q;
int p[N], v[N];
int ma[N];
int at[N];
vector<int> idx[N];
int mi[N];

bool init(int id){
	at[v[id]] = *lower_bound(idx[v[id]].begin(), idx[v[id]].end(), id);
	int lst = id;
	for(int i = 1; i < n; i++){
		auto x = lower_bound(idx[(v[id] + i) % n].begin(), idx[(v[id] + i) % n].end(), lst);
		if(x == idx[(v[id] + i) % n].end()){
			for(int j = 0; j < i; j++){
				mi[at[(v[id] + j) % n]] = m;
			}
			return 0;
		}
		at[(v[id] + i) % n] = *x;
		lst = *x;
	}
	return 1;
}

bool prox(int id){
	mi[id] = at[(v[id] - 1 + n) % n];
	auto x = lower_bound(idx[v[id]].begin(), idx[v[id]].end(), at[(v[id] - 1 + n) % n]);
	if(x == idx[v[id]].end())
		return 0;
	at[v[id]] = *x;
	return 1;
}

int dn[200005][20];

int fn(int i, int j){
	if(j == 0) return mi[i];
	if(~dn[i][j]) return dn[i][j];
	return dn[i][j] = min(fn(i, j-1), fn(i + (1 << (j-1)), j-1));
}

int lg(int x){
	return 31 - __builtin_clz(x);
}

int getmn(int l, int r){ // [l, r]
	int lz = lg(r - l + 1);
	return min(fn(l, lz), fn(r - (1 << lz) + 1, lz));
}

int main(){
	scanf("%d %d %d", &n, &m, &q);
	for(int i = 0; i < n; i++){
		scanf("%d", &p[i]);
		ma[p[i]] = i;
	}
	for(int i = 0; i < m; i++){
		scanf("%d", &v[i]);
		v[i] = ma[v[i]];
		idx[v[i]].pb(i);
		mi[i] = m + 1;
	}
	if(n > 1){
		memset(dn, -1, sizeof dn);
		for(int i = 0; i < m; i++){
			if(mi[i] == m + 1){
				if(init(i)){
					int ida = i;
					while(prox(ida)) ida = at[(v[ida] + 1) % n];
				}
			}
		}
		while(q--){
			int l, r;
			scanf("%d %d", &l, &r);
			l--;
			r--;
			printf("%d", getmn(l, r) <= r);
		}
	}
	else{
		while(q--){
			printf("1");
		}
	}
	printf("\n");

	return 0;
}