#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[y].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            while(adj[tah].size() == dsu[g[tah]].size() - 1)
                tah++ , tah %= n;
            int v , u;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    v = tah;
                    u = j;
                    adj[tah].insert(j);
                    adj[j].insert(tah);
                    break;
                }
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[edge[i].second] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            adj[u].insert(v);
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, cmp[maxn] , tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[x].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    cmp[0] = 1;
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            while(adj[tah].size() == dsu[g[tah]].size() - 1 || cmp[tah] == n - 1)
                tah++ , tah %= n;
            int v , u;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    v = tah;
                    u = j;
                    adj[v].insert(j);
                    adj[j].insert(tah);
                }
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[edge[i].second] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            adj[u].insert(v);
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> edge[maxn] , ans[maxn];
int t, cmp[maxn] , tah;
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            dsu[y].pop_back();
        }
    }
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        cin >> edge[i].first >> edge[i].second;
    sort(edge , edge + m);
    cmp[0] = 1;
    for(int i = 0; i < m; i++)
    {
        if(edge[i].second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(t)
        {
            t--;
            while(cmp[tah] == n)
                tah++ , tah %= n;
            adj[tah].insert(cmp[tah]);
            adj[cmp[tah]].insert(tah);
            int v = tah;
            int u = cmp[tah];
            if(adj[tah].size() == n - 1)
                cmp[tah] = n;
            if(adj[cmp[tah]].size() == n - 1)
                cmp[cmp[tah]] = n;
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[i] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin() -> second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[i].first = v;
            ans[i].second = u;
            adj[v].insert(u);
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            adj[u].insert(v);
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({k , dsu[k].size() + dsu[k1].size()});
            else
                sizes.insert({k1 , dsu[k].size() + dsu[k1].size()});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, cmp[maxn] , tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[x].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    cmp[0] = 1;
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            while(adj[tah].size() == dsu[g[tah]].size() - 1)
                tah++ , tah %= n;
            int v , u;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    v = tah;
                    u = j;
                    adj[v].insert(u);
                    adj[u].insert(v);
                }
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[edge[i].second] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            adj[u].insert(v);
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[y].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            while(adj[tah].size() == dsu[g[tah]].size() - 1)
                tah++ , tah %= n;
            int v , u;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    v = tah;
                    u = j;
                    adj[tah].insert(j);
                    adj[j].insert(tah);
                    break;
                }
            ans[edge[i].second] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            adj[u].insert(v);
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, cmp[maxn] , tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[x].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    cmp[0] = 1;
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            while(cmp[tah] == n || adj[tah].size() == dsu[g[tah]].size() - 1)
                tah++ , tah %= n;
            int v , u;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    v = tah;
                    u = j;
                    adj[v].insert(u);
                    adj[u].insert(v);
                }
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[edge[i].second] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            adj[u].insert(v);
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[y].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , sizes.insert({1 , i}) , g[i] = i;
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            tah = rand() % n;
            while(adj[tah].size() == dsu[g[tah]].size() - 1)
                tah++ , tah %= n;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    adj[tah].insert(j);
                    adj[j].insert(tah);
                    ans[edge[i].second] = {j , tah};
                    break;
                }
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            adj[u].insert(v);
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> edge[maxn] , ans[maxn];
int t, cmp[maxn] , tah;
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<int , int> a , pair<int , int> b)
{
    return (a.first == b.first && a.second > b.second) || a.first < b.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        cin >> edge[i].first >> edge[i].second;
    sort(edge , edge + m , camp);
    cmp[0] = 1;
    for(int i = 0; i < m; i++)
    {
        if(edge[i].second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(t)
        {
            t--;
            while(cmp[tah] == n)
                tah++ , tah %= n;
            adj[tah].insert(cmp[tah]);
            adj[cmp[tah]].insert(tah);
            int v = tah;
            int u = cmp[tah];
            if(adj[tah].size() == n - 1)
                cmp[tah] = n;
            if(adj[cmp[tah]].size() == n - 1)
                cmp[cmp[tah]] = n;
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[i] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin() -> second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[i].first = v;
            ans[i].second = u;
            adj[v].insert(u);
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            adj[u].insert(v);
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({k , dsu[k].size() + dsu[k1].size()});
            else
                sizes.insert({k1 , dsu[k].size() + dsu[k1].size()});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> edge[maxn] , ans[maxn];
int t, cmp[maxn] , tah;
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            dsu[y].pop_back();
        }
    }
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , cmp[i] = i , sizes.insert({1 , i});
    for(int i = 0; i < m; i++)
        cin >> edge[i].first >> edge[i].second;
    sort(edge , edge + m);
    cmp[0] = 1;
    for(int i = 0; i < m; i++)
    {
        if(edge[i].second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(t)
        {
            t--;
            while(cmp[tah] == n)
                tah++ , tah %= n;
            adj[tah].insert(cmp[tah]);
            adj[cmp[tah]].insert(tah);
            if(adj[tah].size() == n - 1)
                cmp[tah] = n;
            if(adj[cmp[tah]].size() == n - 1)
                cmp[cmp[tah]] = n;
            int v = tah;
            int u = cmp[tah];
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            ans[i] = {v , u};
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin() -> second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[i].first = v;
            ans[i].second = u;
            adj[v].insert(u);
            while(adj[v].find(cmp[v]) != adj[v].end() || cmp[v] == v)
                cmp[v]++;
            adj[u].insert(v);
            while(adj[u].find(cmp[u]) != adj[u].end() || cmp[u] == u)
                cmp[u]++;
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({k , dsu[k].size() + dsu[k1].size()});
            else
                sizes.insert({k1 , dsu[k].size() + dsu[k1].size()});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e5 + 20;
pair<int , int> ans[maxn];
pair< pair<int , int> , int> edge[maxn];
int t, tah , g[maxn];
vector<int> dsu[maxn];
set<pair<int , int> > sizes;
set<int> adj[maxn];
void merg(int x , int y)
{
    t += dsu[x].size() * dsu[y].size();
    if(dsu[x].size() < dsu[y].size())
    {
        while(!dsu[x].empty())
        {
            dsu[y].pb(dsu[x].back());
            g[dsu[x].back()] = y;
            dsu[x].pop_back();
        }
    }
    else
    {
        while(!dsu[y].empty())
        {
            dsu[x].pb(dsu[y].back());
            g[dsu[y].back()] = x;
            dsu[y].pop_back();
        }
    }
}
bool camp(pair<pair<int , int> , int> a , pair<pair<int , int> , int> b)
{
    return (a.first.first == b.first.first && a.first.second > b.first.second) || a.first.first < b.first.first;
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        dsu[i].pb(i) , sizes.insert({1 , i}) , g[i] = i;
    for(int i = 0; i < m; i++)
        scanf("%d%d", &edge[i].first.first, &edge[i].first.second) , edge[i].second = i;
    sort(edge , edge + m , camp);
    for(int i = 0; i < m; i++)
    {
        if(edge[i].first.second == 0 && !t)
        {
            cout << -1;
            return 0;
        }
        else if(!edge[i].first.second && t)
        {
            t--;
            while(adj[tah].size() == dsu[g[tah]].size() - 1)
                tah++ , tah %= n;
            for(int j : dsu[g[tah]])
                if(j != tah && adj[tah].find(j) == adj[tah].end())
                {
                    adj[tah].insert(j);
                    adj[j].insert(tah);
                    ans[edge[i].second] = {j , tah};
                    break;
                }
        }
        else
        {
            int k = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int k1 = sizes.rbegin()->second;
            sizes.erase(*sizes.rbegin());
            int v = dsu[k][0] , u = dsu[k1][0];
            ans[edge[i].second] = {v , u};
            adj[v].insert(u);
            adj[u].insert(v);
            if(dsu[k].size() < dsu[k1].size())
                sizes.insert({dsu[k].size() + dsu[k1].size() , k1});
            else
                sizes.insert({dsu[k].size() + dsu[k1].size() , k});
            t--;
            merg(k , k1);
        }
    }
    for(int i = 0; i < m; i++)
        printf("%d %d\n" , ans[i].first + 1 , ans[i].second + 1);
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;
int b[MM];

struct Edge
{
	int weight;
	int bj;
	int idx;
};

pii EVV[MM];
vector< Edge > E;

bool cmp( const Edge &a, const Edge &b)
{
	if( a.weight < b.weight )
	{
		return true;
	}
	else if(a.weight > b.weight)
	{
		return false;
	}
	else
	{
		if(a.bj > b.bj) return true;
		else if(a.bj < b.bj) return false;
		else return true;
	}
}

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n,m;
	cin>>n>>m;
	int aj,bj;
	for(int i = 1 ; i <= m ; i++)
	{
		cin>>aj>>bj;
		b[i] = bj;
		Edge e;
		e.weight = aj;
		e.bj = bj;
		e.idx = i;
		E.pb(e);
	}

	sort(E.begin(), E.end(), cmp);

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;
	bool success = true;
	for(int i = 0; i < E.size() ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		int here = E[i].idx;
		if(E[i].bj)
		{
			EVV[here] = pii(1, iso);
			iso++;
		}
		else
		{
			if(iso <= to) {success = false; break;	}
			EVV[here] = pii(from, to);
			from++;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 1; i <= m ; i++)
		{
			cout<<EVV[i].first<< ' ' << EVV[i].second<<newline;
		}
	}
	else
	{
		cout<<-1<<newline;
	}

	return 0;
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;
int b[MM];

struct Edge
{
	int weight;
	int bj;
	int idx;
};

pii EVV[MM];
vector< Edge > E;

bool cmp( const Edge &a, const Edge &b)
{
	if( a.weight < b.weight )
	{
		return true;
	}
	else if(a.weight > b.weight)
	{
		return false;
	}
	else
	{
		if(a.bj < b.bj) return false;
		else return true;
	}
}

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n,m;
	cin>>n>>m;
	int aj,bj;
	Edge e;
	for(int i = 1 ; i <= m ; i++)
	{
		cin>>aj>>bj;
		b[i] = bj;
		e.weight = aj;
		e.bj = bj;
		e.idx = i;
		E.pb(e);
	}

	sort(E.begin(), E.end(), cmp);

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;
	bool success = true;
	for(int i = 0; i < E.size() ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		int here = E[i].idx;
		if(E[i].bj)
		{
			EVV[here] = pii(1, iso);
			iso++;
		}
		else
		{
			if(iso <= to) {success = false; break;	}
			EVV[here] = pii(from, to);
			from++;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 1; i <= m ; i++)
		{
			cout<<EVV[i].first<< ' ' << EVV[i].second<<newline;
		}
	}
	else
	{
		cout<<-1<<newline;
	}

	return 0;
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;

int ansA[MM];
int ansB[MM];
vector<tuple<int, int, int> > E;

int main(int argc, char **argv)
{
	int n,m;
	int aj,bj;
	scanf("%d%d",&n,&m);

	for(int i = 1 ; i <= m ; i++)
	{
		scanf("%d%d",&aj, &bj);
		E.pb(make_tuple(aj, 2-bj, i));
	}

	sort(E.begin(), E.end());

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;

	bool success = true;
	int here;
	for(int i = 0; i < m ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		tie(aj, bj, here) = E[i];

		if(bj == 1)
		{
			ansA[here] = 1;
			ansB[here] = iso++;
		}
		else
		{
			if(iso <= to) {success = false; break; }
			ansA[here] = from++;
			ansB[here] = to;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 1; i <= m ; i++)
		{
			printf("%d %d\n",ansA[i], ansB[i]);
		}
	}
	else
	{
		puts("-1");
	}

	return 0;
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;

struct Edge
{
	int weight;
	int bj;
	int idx;
};

int ansA[MM];
int ansB[MM];
Edge E[MM];

bool cmp( const Edge &a, const Edge &b)
{
	if(a.weight != b.weight) return a.weight < b.weight;
	else
	{
		if(a.bj < b.bj) return false;
		else return true;
	}
}

int main(int argc, char **argv)
{
	int n,m;
	scanf("%d%d",&n,&m);

	for(int i = 1 ; i <= m ; i++)
	{
		scanf("%d%d",&E[i-1].weight,&E[i-1].bj);
		E[i-1].idx = i;
	}

	sort(E, E + m, cmp);

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;

	bool success = true;
	int here;
	for(int i = 0; i < m ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		here = E[i].idx;
		if(E[i].bj)
		{
			ansA[i] = 1;
			ansB[i] = iso++;
		}
		else
		{
			if(iso <= to) {success = false; break; }
			ansA[i] = from++;
			ansB[i] = to;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 0; i < m ; i++)
		{
			printf("%d %d\n",ansA[i], ansB[i]);
		}
	}
	else
	{
		puts("-1");
	}

	return 0;
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;

struct Edge
{
	int weight;
	int bj;
	int idx;
};

int ansA[MM];
int ansB[MM];
Edge E[MM];

bool cmp( const Edge &a, const Edge &b)
{
	if(a.weight != b.weight) return a.weight < b.weight;
	else
	{
		if(a.bj < b.bj) return false;
		else return true;
	}
}

int main(int argc, char **argv)
{
	int n,m;
	scanf("%d%d",&n,&m);

	for(int i = 1 ; i <= m ; i++)
	{
		scanf("%d%d",&E[i-1].weight,&E[i-1].bj);
		E[i-1].idx = i;
	}

	sort(E, E + m, cmp);

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;

	bool success = true;
	int here;
	for(int i = 0; i < m ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		here = E[i].idx;
		if(E[i].bj)
		{
			ansA[here] = 1;
			ansB[here] = iso++;
		}
		else
		{
			if(iso <= to) {success = false; break; }
			ansA[here] = from++;
			ansB[here] = to;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 1; i <= m ; i++)
		{
			printf("%d %d\n",ansA[i], ansB[i]);
		}
	}
	else
	{
		puts("-1");
	}

	return 0;
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;
int b[MM];

struct Edge
{
	int weight;
	int bj;
	int idx;
};

pii EVV[MM];
vector< Edge > E;

bool cmp( const Edge &a, const Edge &b)
{
	if( a.weight < b.weight )
	{
		return true;
	}
	else if(a.weight > b.weight)
	{
		return false;
	}
	else
	{
		if(a.bj > b.bj) return true;
		else if(a.bj < b.bj) return false;
		else return true;
	}
}

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n,m;
	cin>>n>>m;
	int aj,bj;
	for(int i = 1 ; i <= m ; i++)
	{
		cin>>aj>>bj;
		b[i] = bj;
		Edge e;
		e.weight = aj;
		e.bj = bj;
		e.idx = i;
		E.pb(e);
	}

	sort(E.begin(), E.end(), cmp);

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;
	bool success = true;
	for(int i = 0; i < E.size() ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		int here = E[i].idx;
		if(E[i].bj)
		{
			EVV[here] = pii(1, iso);
			iso++;
		}
		else
		{
			if(iso < to) {success = false; break;	}
			EVV[here] = pii(from, to);
			from++;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 1; i <= m ; i++)
		{
			cout<<EVV[i].first<< ' ' << EVV[i].second<<newline;
		}
	}
	else
	{
		cout<<-1<<newline;
	}

	return 0;
}


#include <iostream> // standard input & output cout cin
#include <cstdio> // scanf printfsssss
#include <vector> // std::vector
#include <algorithm>
#include <string> // std::string
#include <queue> // std::queue
#include <cstring> // memset
#include <set> // std::set
#include <utility> // std::pair, std::make_pair
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <cmath>
#include <ctime>
#include <chrono>
#include <climits>
#include <cassert>
using namespace std;

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

const char newline = '\n';

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,1,0};
#define pb(x) push_back(x)
#define forn(i,n) for(int i = 0 ; i < int(n); ++i)
const int mod = 1000000007;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int MN = 100000 + 10;
const int MM = 100000 + 10;

struct Edge
{
	int weight;
	int bj;
	int idx;
};

int ansA[MM];
int ansB[MM];
Edge E[MM];

bool cmp( const Edge &a, const Edge &b)
{
	if(a.weight != b.weight) return a.weight < b.weight;
	else
	{
		if(a.bj < b.bj) return false;
		else return true;
	}
}

int main(int argc, char **argv)
{
	int n,m;
	scanf("%d%d",&n,&m);

	for(int i = 1 ; i <= m ; i++)
	{
		scanf("%d%d",E[i-1]->weight,E[i-1]->bj);
		E[i-1].idx = i;
	}

	sort(E, E + m, cmp);

	int from , to;
	int iso = 2;
	from = 2;
	to = 3;

	memset(ansA, 1, sizeof(ansA));
	bool success = true;
	int here;
	for(int i = 0; i < m ; i++)
	{
	//	cout<<'('<<E[i].weight<<','<<E[i].bj<<')'<<newline;
		here = E[i].idx;
		if(E[i].bj)
		{
			ansB[i] = iso++;
		}
		else
		{
			if(iso <= to) {success = false; break; }
			ansA[i] = from++;
			ansB[i] = to;
			if(from == to) { to++; from = 2;}
		}
	}

	if(success)
	{
		for(int i = 1; i <= m ; i++)
		{
			printf("%d %d\n",ansA[i], ansB[i]);
		}
	}
	else
	{
		puts("-1");
	}

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
#define F first
#define S second

pii Good[100000];
pii  Bad[100000];

pii Ans[100001];

int main(){
    iostream::sync_with_stdio(false);
    cin.tie();
    int N,M;
    cin >> N >> M;
    int PG = 0;
    int PB = 0;
    for (int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        if (b)
            Good[PG++]=pii(a,i);
        else Bad[PB++]=pii(a,i);
    }
    sort(Good,Good+PG);
    sort( Bad, Bad+PB);
    int PPB = PB;

    for (int i=0;i<PG;i++)
        Ans[ Good[i].S ] = pii(i+1,i+2);
    for (int i=N-1;i> 1;i--)
        for (int j=i-2;j>=0;j--){
            //cout <<".";
            if (PB == 0){
                for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;
                return 0;
            }
            PB--;
            if (Bad[PB].F < Good[i-1].F){
                //cout << Bad[PB].F<<endl;
                cout <<"-1"<<endl;
                 /*               for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl*/
                return 0;
            }
            Ans[ Bad[PB].S ].F = i + 1;
            Ans[ Bad[PB].S ].S = j + 1;
        }
    for (int K=0;K<PG+PPB;K++)
        cout << Ans[K].F<<" "<<Ans[K].S<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
#define F first
#define S second

pii Good[100000];
pii  Bad[100000];

pii Ans[100001];

int main(){
    iostream::sync_with_stdio(false);
    cin.tie();
    int N,M;
    cin >> N >> M;
    int PG = 0;
    int PB = 0;
    for (int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        if (b)
            Good[PG++]=pii(a,i);
        else Bad[PB++]=pii(a,i);
    }
    sort(Good,Good+PG);
    sort( Bad, Bad+PB);
    int PPB = PB;

    for (int i=0;i<PG;i++)
        Ans[ Good[i].S ] = pii(i+1,i+2);
    for (int i=0;i < N;i++)
        for (int j=i+2;j<N;j++){
            //cout <<".";
            if (PB == 0){
                for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;
                return 0;
            }
            PB--;
            if (Bad[PB].F < Good[i].F){
                //cout << Bad[PB].F<<" "<<Good[i].F<<" "<<Good[i-1].F<<endl;
                cout <<"-1"<<endl;
                /*for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;*/
                return 0;
            }
            Ans[ Bad[PB].S ].F = i + 1;
            Ans[ Bad[PB].S ].S = j + 1;
        }
    for (int K=0;K<PG+PPB;K++)
        cout << Ans[K].F<<" "<<Ans[K].S<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
#define F first
#define S second

pii Good[100000];
pii  Bad[100000];

pii Ans[100001];

int main(){
    iostream::sync_with_stdio(false);
    cin.tie();
    int N,M;
    cin >> N >> M;
    int PG = 0;
    int PB = 0;
    for (int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        if (b)
            Good[PG++]=pii(a,i);
        else Bad[PB++]=pii(a,i);
    }
    sort(Good,Good+PG);
    sort( Bad, Bad+PB);
    int PPB = PB;

    for (int i=0;i<PG;i++)
        Ans[ Good[i].S ] = pii(i+1,i+2);
    for (int i=1;i < N-1;i++)
        for (int j=i-2;j>=0;j--){
            //cout <<".";
            if (PB == 0){
                for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;
                return 0;
            }
            PB--;
            if (Bad[PB].F < Good[i-1].F){
                //cout << Bad[PB].F<<" "<<Good[i].F<<" "<<Good[i-1].F<<endl;
                cout <<"-1"<<endl;
                /*for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;*/
                return 0;
            }
            Ans[ Bad[PB].S ].F = i + 1;
            Ans[ Bad[PB].S ].S = j + 1;
        }
    for (int K=0;K<PG+PPB;K++)
        cout << Ans[K].F<<" "<<Ans[K].S<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
#define F first
#define S second

pii Good[100000];
pii  Bad[100000];

pii Ans[100001];

int main(){
    iostream::sync_with_stdio(false);
    cin.tie();
    int N,M;
    cin >> N >> M;
    int PG = 0;
    int PB = 0;
    for (int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        if (b)
            Good[PG++]=pii(a,i);
        else Bad[PB++]=pii(a,i);
    }
    sort(Good,Good+PG);
    sort( Bad, Bad+PB);
    int PPB = 0;

    for (int i=0;i<PG;i++)
        Ans[ Good[i].S ] = pii(i+1,i+2);
    for (int i=2;i < N;i++)
        for (int j=0;j<i-1;j++){
            //cout <<".";
            if (PPB == PB){
                for (int K=0;K<PG+PB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;
                return 0;
            }
            if (Bad[PPB].F < Good[i-1].F){
                cout <<"-1";
                return 0;
            }
            Ans[ Bad[PPB].S ].F = i + 1;
            Ans[ Bad[PPB].S ].S = j + 1;
            PPB++;
        }
    if (PB!=PPB){
        cout <<"-1";
        return 0;
    }
    for (int K=0;K<PG+PPB;K++)
        cout << Ans[K].F<<" "<<Ans[K].S<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
#define F first
#define S second

pii Good[100000];
pii  Bad[100000];

pii Ans[100001];

int main(){
    iostream::sync_with_stdio(false);
    cin.tie();
    int N,M;
    cin >> N >> M;
    int PG = 0;
    int PB = 0;
    for (int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        if (b)
            Good[PG++]=pii(a,i);
        else Bad[PB++]=pii(a,i);
    }
    sort(Good,Good+PG);
    sort( Bad, Bad+PB);
    int PPB = PB;

    for (int i=0;i<PG;i++)
        Ans[ Good[i].S ] = pii(i+1,i+2);
    for (int i=0;i < N;i++)
        for (int j=i+2;j<N;j++){
            //cout <<".";
            if (PB == 0){
                for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;
                return 0;
            }
            PB--;
            if (Bad[PB].F < Good[i-1].F){
                //cout << Bad[PB].F<<" "<<Good[i].F<<" "<<Good[i-1].F<<endl;
                cout <<"-1"<<endl;
                /*for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;*/
                return 0;
            }
            Ans[ Bad[PB].S ].F = i + 1;
            Ans[ Bad[PB].S ].S = j + 1;
        }
    for (int K=0;K<PG+PPB;K++)
        cout << Ans[K].F<<" "<<Ans[K].S<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
#define F first
#define S second

pii Good[100000];
pii  Bad[100000];

pii Ans[100001];

int main(){
    iostream::sync_with_stdio(false);
    cin.tie();
    int N,M;
    cin >> N >> M;
    int PG = 0;
    int PB = 0;
    for (int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        if (b)
            Good[PG++]=pii(a,i);
        else Bad[PB++]=pii(a,i);
    }
    sort(Good,Good+PG);
    sort( Bad, Bad+PB);
    int PPB = PB;

    for (int i=0;i<PG;i++)
        Ans[ Good[i].S ] = pii(i+1,i+2);
    for (int i=0;i < N;i++)
        for (int j=i+2;j<N;j++){
            //cout <<".";
            if (PB == 0){
                for (int K=0;K<PG+PPB;K++)
                    cout << Ans[K].F<<" "<<Ans[K].S<<endl;
                return 0;
            }
            PB--;
            if (Bad[PB].F < Good[i].F || Bad[PB].F < Good[j-1].F){
                PB++;
            } else {
                Ans[ Bad[PB].S ].F = i + 1;
                Ans[ Bad[PB].S ].S = j + 1;
            }
        }
    if (PB!=0){
        cout <<"-1";
        return 0;
    }
    for (int K=0;K<PG+PPB;K++)
        cout << Ans[K].F<<" "<<Ans[K].S<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    iOS;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>arr[i].ff.ff>>arr[i].ff.ss;
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y;
            set<pair<ll,int> >::iterator it;
            if(s.size()==1)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            x.ff++;y.ff++;
            s.erase(it);
            it--;
            s.erase(it);
            s.ins(x);s.ins(y);
        }
    }
    for(int i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<"\n";
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    iOS;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>arr[i].ff.ff>>arr[i].ff.ss;
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,1));
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(ln-1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y,z,t1,t2;
            set<pair<ll,int> >::iterator it;
            if(s.size()==2)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;it++;
            z=*it;
            if(abs(y.ss-x.ss)==1)
                swap(y,z);
            t1=x;t2=y;
            if(x.ff==(ln*(ln+1))/2)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            s.erase(s.find(x));
            s.erase(s.find(y));
            s.ins(t1);
            s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<"\n";
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    return (x.ff.ff<y.ff.ff||(x.ff.ff==y.ff.ff && x.ff.ss>=y.ff.ss));
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&arr[i].ff.ff,&arr[i].ff.ss);
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y,t1,t2;
            set<pair<ll,int> >::iterator it,itt;
            if(s.size()==1)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;
            t1=x;t2=y;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            itt=s.begin();
           // cout<<i<<" "<<s.size()<<endl;
            s.erase(s.begin());
            s.erase(s.begin());
            s.ins(t1);s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        printf("%d %d\n",ans[i].ff,ans[i].ss);
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    iOS;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>arr[i].ff.ff>>arr[i].ff.ss;
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,1));
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(ln-1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y,z,t1,t2;
            set<pair<ll,int> >::iterator it;
            if(s.size()==2)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;it++;
            z=*it;
            if(abs(y.ss-x.ss)==1)
                swap(y,z);
            t1=x;t2=y;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            s.erase(s.find(x));
            s.erase(s.find(y));
            s.ins(t1);
            s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<"\n";
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&arr[i].ff.ff,&arr[i].ff.ss);
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y,t1,t2;
            set<pair<ll,int> >::iterator it;
            if(s.size()==1)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;
            t1=x;t2=y;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            s.erase(s.find(x));
            s.erase(s.find(y));
            s.ins(t1);s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        printf("%d %d\n",ans[i].ff,ans[i].ss);
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    iOS;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>arr[i].ff.ff>>arr[i].ff.ss;
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y,t1,t2;
            set<pair<ll,int> >::iterator it;
            if(s.size()==1)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;
            t1=x;t2=y;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            s.erase(s.find(x));
            s.erase(s.find(y));
            s.ins(t1);s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<"\n";
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<ll,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&arr[i].ff.ff,&arr[i].ff.ss);
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<ll,int> x,y,t1,t2;
            set<pair<ll,int> >::iterator it,itt;
            if(s.size()==1)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;
            t1=x;t2=y;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            itt=s.begin();
           // cout<<i<<" "<<s.size()<<endl;
            s.erase(s.begin());
            s.erase(s.begin());
            s.ins(t1);s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        printf("%d %d\n",ans[i].ff,ans[i].ss);
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;

struct cc{
	int v,mk;
	bool h;
};

cc a[N];

bool cmp(cc b,cc c)
{
	return((b.v<c.v)||(b.v==c.v && b.h>c.h));
}

int x[N],y[N];

int main()
{
	ios_base::sync_with_stdio(0);

	int n,m;
	cin >> n >> m;
	for(int i=1;i<=m;i++)
	{
		cin >> a[i].v >> a[i].h;
		a[i].mk=i;
	}
	sort(a+1,a+m+1,cmp);
	long long int mak=2,tedad=0;
	for(int i=1;i<=m;i++)
	{
		if(a[i].h==1)
		{
			x[a[i].mk]=mak-1;
			y[a[i].mk]=mak;
			tedad--;
			tedad+=(mak-1);
			mak++;
		}
		else
		{
			tedad--;
			if(tedad<0)
			{
				cout<<-1;
				return 0;
			}
		}
	}
	int l=1,r=3;
	for(int i=1;i<=m;i++)
	{
		if(a[i].h==0)
		{
			if(r-l==1)
			{
				r++;
				l=1;
			}
			x[a[i].mk]=l;
			y[a[i].mk]=r;
			l++;
		}
	}
	for(int i=1;i<=m;i++)
		cout<<x[i]<<" "<<y[i]<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define ins insert
#define mp make_pair
#define iOS ios::sync_with_stdio(false)
using namespace std;
pair<pair<int,int> ,int> arr[100005];
pair<int,int> ans[100005];
set<pair<int,int> > s;
int n,m;
bool cmp(pair<pair<int,int> ,int> x,pair<pair<int,int> ,int> y)
{
    if(x.ff.ff<y.ff.ff)
        return true;
    else if(x.ff.ff==y.ff.ff)
    {
        if(x.ff.ss>=y.ff.ss)
            return true;
    }
    return false;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&arr[i].ff.ff,&arr[i].ff.ss);
        arr[i].ss=i;
    }
    sort(arr,arr+m,cmp);
    if(arr[0].ff.ss==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans[arr[0].ss]=mp(1,2);
    ll ln=2;
    s.ins(mp(1,2));
    for(int i=1;i<m;i++)
    {
        if(arr[i].ff.ss==1)
        {
            ln++;
            ans[arr[i].ss]=mp(1,ln);
            s.ins(mp(1,ln));
        }
        else
        {
            pair<int,int> x,y,t1,t2;
            set<pair<int,int> >::iterator it,itt;
            if(s.size()==1)
            {
                cout<<-1<<endl;
                return 0;
            }
            it=s.begin();
            x=*it;it++;
            y=*it;
            t1=x;t2=y;
            if(x.ff==ln-1)
            {
                cout<<-1<<endl;
                return 0;
            }
            ans[arr[i].ss]=mp(x.ss,y.ss);
            t1.ff++;t2.ff++;
            itt=s.begin();
            s.erase(itt);itt++;
            s.erase(itt);
            s.ins(t1);s.ins(t2);
        }
    }
    for(int i=0;i<m;i++)
        printf("%d %d\n",ans[i].ff,ans[i].ss);
}


// program : 606/B
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, int> EDGE;

vector< pair<int, pair<int, int> > > edges;
vector< pair<int, EDGE> > adj;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	for (int k = 0; k < m; k++) {
		int w, pres;
		cin >> w >> pres;
		edges.push_back({w, {k, pres}});
	}
	sort(edges.begin(), edges.end());
	int i = 2, j = 3, s = 2;
	for (int k = 0; k < m; k++) {
		if (edges[k].second.second == 1) {
			adj.push_back({edges[k].second.first, {1, s}});
			s++;
		} else {
			adj.push_back({edges[k].second.first, {i, j}});
			j++;
			if (j >= n) i++, j = i + 1;
			if (i >= s) {
				cout << -1;
				return 0;
			}
		}
	}

	sort(adj.begin(), adj.end());
	for (int k = 0; k < m; k++) {
		EDGE& ed = adj[k].second;
		cout << ed.first << ' '  << ed.second << endl;
	}
	return 0;
}


// program : 606/B
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, int> EDGE;

vector< pair<int, pair<int, int> > > edges;
vector< pair<int, EDGE> > adj;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	for (int k = 0; k < m; k++) {
		int w, pres;
		cin >> w >> pres;
		edges.push_back({w, {k, !pres}});
	}
	sort(edges.begin(), edges.end());
	int i = 2, j = 3, s = 2;
	for (int k = 0; k < m; k++) {
		if (edges[k].second.second == 0) {
			adj.push_back({edges[k].second.first, {1, s}});
			s++;
		} else {
			adj.push_back({edges[k].second.first, {i, j}});
			j++;
			if (j > n) i++, j = i + 1;
			if (i >= s) {
				cout << -1;
				return 0;
			}
		}
	}

	sort(adj.begin(), adj.end());
	for (int k = 0; k < m; k++) {
		EDGE& ed = adj[k].second;
		cout << ed.first << ' '  << ed.second << endl;
	}
	return 0;
}


// program : 606/B
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, int> EDGE;

vector< pair<int, pair<bool, int> > > edges;
vector< pair<int, EDGE> > adj;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	for (int k = 0; k < m; k++) {
		int w; bool pres;
		cin >> w >> pres;
		edges.push_back({w, {!pres, k}});
	}
	sort(edges.begin(), edges.end());
	int i = 2, j = 3, s = 2;
	for (int k = 0; k < m; k++) {
		if (edges[k].second.first == 0) {
			//cerr << s << endl;
			adj.push_back({edges[k].second.second, {1, s}});
			s++;
		} else {
			//cerr << i << ' ' << j << endl;
			if (j >= s) {
				cout << -1;
				return 0;
			}
			adj.push_back({edges[k].second.second, {i, j}});
			i++;
			if (i >= j) j++, i = 2;
		}
	}

	sort(adj.begin(), adj.end());
	for (int k = 0; k < m; k++) {
		EDGE& ed = adj[k].second;
		cout << ed.first << ' '  << ed.second << endl;
	}
	return 0;
}


// program : 606/B
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, int> EDGE;

vector< pair<int, pair<int, int> > > edges;
vector< pair<int, EDGE> > adj;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	for (int k = 0; k < m; k++) {
		int w, pres;
		cin >> w >> pres;
		edges.push_back({w, {k, pres}});
	}
	sort(edges.begin(), edges.end());
	int i = 2, j = 3, s = 2;
	for (int k = 0; k < m; k++) {
		if (edges[k].second.second == 1) {
			adj.push_back({edges[k].second.first, {1, s}});
			s++;
		} else {
			adj.push_back({edges[k].second.first, {i, j}});
			j++;
			if (j >= n) i++, j = i + 1;
			if (i > s) {
				cout << -1;
				return 0;
			}
		}
	}

	sort(adj.begin(), adj.end());
	for (int k = 0; k < m; k++) {
		EDGE& ed = adj[k].second;
		cout << ed.first << ' '  << ed.second << endl;
	}
	return 0;
}


// program : 606/B
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, int> EDGE;

vector< pair<int, pair<bool, int> > > edges;
vector< pair<int, EDGE> > adj;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	for (int k = 0; k < m; k++) {
		int w; bool pres;
		cin >> w >> pres;
		edges.push_back({w, {!pres, k}});
	}
	sort(edges.begin(), edges.end());
	int i = 2, j = 3, s = 2;
	for (int k = 0; k < m; k++) {
		if (edges[k].second.first == 0) {
			//cerr << s << endl;
			adj.push_back({edges[k].second.second, {1, s}});
			s++;
		} else {
			//cerr << i << ' ' << j << endl;
			adj.push_back({edges[k].second.second, {i, j}});
			j++;
			if (j > n) i++, j = i + 1;
			if (i >= s) {
				cout << -1;
				return 0;
			}
		}
	}

	sort(adj.begin(), adj.end());
	for (int k = 0; k < m; k++) {
		EDGE& ed = adj[k].second;
		cout << ed.first << ' '  << ed.second << endl;
	}
	return 0;
}


// program : 606/B
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, int> EDGE;

vector< pair<int, pair<int, int> > > edges;
vector< pair<int, EDGE> > adj;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	for (int k = 0; k < m; k++) {
		int w, pres;
		cin >> w >> pres;
		edges.push_back({w, {k, !pres}});
	}
	sort(edges.begin(), edges.end());
	int i = 2, j = 3, s = 2;
	for (int k = 0; k < m; k++) {
		if (edges[k].second.second == 0) {
			adj.push_back({edges[k].second.first, {1, s}});
			s++;
		} else {
			adj.push_back({edges[k].second.first, {i, j}});
			j++;
			if (j >= n) i++, j = i + 1;
			if (i >= s) {
				cout << -1;
				return 0;
			}
		}
	}

	sort(adj.begin(), adj.end());
	for (int k = 0; k < m; k++) {
		EDGE& ed = adj[k].second;
		cout << ed.first << ' '  << ed.second << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector<int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(st>cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                int v=*(M[st].rbegin());
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                while(st<=n && M[st].size()==0)
                {
                    st++;
                }
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector <int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur++;
        }
        else
        {
            if(st>cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                int v=M[st][M[st].size()-1];
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if(M[st].size()==0)
                    st++;
            }
        }
    }
    if(f==1)
    {
        printf("-1\n");
        return 0;
    }
    for(i=0;i<m;i++)
    {
        printf("%d %d\n",gr[i].f,gr[i].s);
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector <int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur++;
        }
        else
        {
            if(st>cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                if(M[st].size()==0)
                {
                    f=1;
                    break;
                }
                int v=M[st][M[st].size()-1];
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if(M[st].size()==0)
                    st++;
            }
        }
    }
    if(f==1)
    {
        printf("-1\n");
        return 0;
    }
    for(i=0;i<m;i++)
    {
        printf("%d %d\n",gr[i].f,gr[i].s);
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector <int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else if(a.f.s==1 && b.f.s==1)
        return a.s<b.s;
    else if(a.f.s==1)
        return 1;
    else if(b.f.s==1)
        return 0;
    else
        return a.s<b.s;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur++;
        }
        else
        {
            if(st>cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                int v=M[st][M[st].size()-1];
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if(M[st].size()==0)
                    st++;
            }
        }
    }
    if(f==1)
    {
        cout<<"-1\n";
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    piii e[m];
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    map <int,vector<int> > M;
    map <int,vector <int> >:: iterator it;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    pii gr[m];
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(M.empty())
            {
                f=1;
                break;
            }
            it=M.begin();
            if(it->f > cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=it->f;
                int v=(*((it->s).rbegin()));
                gr[e[i].s]=pii(u,v);
                (it->s).pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if((it->s).size()==0)
                    M.erase(it);
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    piii e[m];
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    map <int,vector<int> > M;
    map <int,vector <int> >:: iterator it;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    pii gr[m];
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(M.empty())
            {
                f=1;
                break;
            }
            it=M.begin();
            if(it->f > cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=it->f;
                int v=(*(it->s).rbegin());
                gr[e[i].s]=pii(u,v);
                (it->s).pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if((it->s).size()==0)
                    M.erase(it);
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector <int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur++;
        }
        else
        {
            if(st>cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                int v=M[st][M[st].size()-1];
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                st++;
            }
        }
    }
    if(f==1)
    {
        printf("-1\n");
        return 0;
    }
    for(i=0;i<m;i++)
    {
        printf("%d %d\n",gr[i].f,gr[i].s);
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector<int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(st>n)
            {
                f=1;
                break;
            }
            if(st > cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                int v=*(M[st].rbegin());
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if(M[st].size()==0)
                    st++;
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
vector<int> M[100005];
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    int st=3;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(st>cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=st;
                int v=*(M[st].rbegin());
                gr[e[i].s]=pii(u,v);
                M[st].pop_back();
                if(u<n)
                    M[u+1].pb(v);
                while(st<=n && M[st].size()==0)
                {
                    st++;
                }
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

bool comp(pii a,pii b)
{
    if(a.f!=b.f)
        return a.f<b.f;
    else
        return a.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    pii e[m];
    for(i=0;i<m;i++)
    {
        cin>>e[i].f>>e[i].s;
    }
    sort(e,e+m,comp);
    vector <int> gr[n+1];
    int cur=1,j;
    map <int,vector<int> > M;
    map <int,vector <int> >:: iterator it;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].s==1)
        {
            gr[cur].pb(cur+1);
            cur=cur+1;
        }
        else
        {
            if(M.empty())
            {
                f=1;
                break;
            }
            it=M.begin();
            if(it->f > cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=it->f;
                int v=(*(it->s).rbegin());
                gr[u].pb(v);
                (it->s).pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if((it->s).size()==0)
                    M.erase(it);
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        for(j=0;j<gr[i].size();j++)
            cout<<i<<" "<<gr[i][j]<<"\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else if(a.f.s==1)
        return 1;
    else
        return 0;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    piii e[m];
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    map <int,vector<int> > M;
    map <int,vector <int> >:: iterator it;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    pii gr[m];
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(M.empty())
            {
                f=1;
                break;
            }
            it=M.begin();
            if(it->f > cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=it->f;
                int v=(*((it->s).rbegin()));
                gr[e[i].s]=pii(u,v);
                (it->s).pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if((it->s).size()==0)
                    M.erase(it);
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define pb(x) push_back(x)
#define pii pair <int,int>
#define piii pair < pair<int,int>,int>
#define f first
#define s second
#define MOD 1000000007
#define INF 1000000000000000

using namespace std;

piii e[100005];
pii gr[100005];
map <int,vector<int> > M;
bool comp(piii a,piii b)
{
    if(a.f.f!=b.f.f)
        return a.f.f<b.f.f;
    else
        return a.f.s==1;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>e[i].f.f>>e[i].f.s;
        e[i].s=i;
    }
    sort(e,e+m,comp);
    int cur=1,j;
    map <int,vector <int> >:: iterator it;
    for(i=3;i<=n;i++)
    {
        M[i].pb(i-2);
    }
    bool f=0;
    for(i=0;i<m;i++)
    {
        if(e[i].f.s==1)
        {
            gr[e[i].s]=pii(cur,cur+1);
            cur=cur+1;
        }
        else
        {
            if(M.empty())
            {
                f=1;
                break;
            }
            it=M.begin();
            if(it->f > cur)
            {
                f=1;
                break;
            }
            else
            {
                int u=it->f;
                int v=(*((it->s).rbegin()));
                gr[e[i].s]=pii(u,v);
                (it->s).pop_back();
                if(u<n)
                    M[u+1].pb(v);
                if((it->s).size()==0)
                    M.erase(it);
            }
        }
    }
    if(f==1)
    {
        cout<<-1;
        return 0;
    }
    for(i=0;i<m;i++)
    {
        cout<<gr[i].f<<" "<<gr[i].s<<"\n";
    }
}


#include <bits/stdc++.h>
#define maxv 10000
#define INF maxv
#define iOS ios::sync_with_stdio(false);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define e1 get<0>
#define e2 get<1>
#define e3 get<2>
#define ins insert
#define ms memset
#define so sizeof
#define itr ::iterator
#define in_an for(i=0;i<n;i++) cin>>a[i]
#define in_bn for(i=0;i<n;i++) cin>>b[i]
#define fin for(i=0;i<n;i++)
#define fim for(i=0;i<m;i++)
#define fjn for(j=0;j<n;j++)
#define fjm for(j=0;j<m;j++)
#define bi back_inserter
#define v1 a,b,c,d;
#define v2 i,j,k,l;
#define v3 p,q,r,s;
#define pll pair<long,long>

using namespace std;

//MiCr0-!cE

int main()
{
    iOS
    long n,m,p,q;
    cin>>n>>m;
    pair<pair<long,bool>,long> a[m];
    for(long i=0;i<m;i++)
    {
        cin>>p>>q;
        a[i].ff=mp(p,1-q);
        a[i].ss=i;
    }
    sort(a,a+m);
    pair<long,long> ans[m];
    long t1,t2,t3;
    bool xx=true;t1=2;t2=2; t3=3;
    for(long i=0;i<m;i++)
    {
        if(a[i].ff.ss)
        {
            ans[a[i].ss]=mp(1,t1++);
        }
        else
        {
            if(t3>=t1) xx=false;
            ans[a[i].ss]=mp(t2++,t3);
            if(t2>=t3)
            {
                t3++;
                t2=2;
            }
        }
    }
    if(t1!=n+1) xx=false;
    if(!xx) cout<<"-1";
    else
    {
        for(long i=0;i<m;i++)
            cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
#define maxv 10000
#define INF maxv
#define iOS ios::sync_with_stdio(false);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define e1 get<0>
#define e2 get<1>
#define e3 get<2>
#define ins insert
#define ms memset
#define so sizeof
#define itr ::iterator
#define in_an for(i=0;i<n;i++) cin>>a[i]
#define in_bn for(i=0;i<n;i++) cin>>b[i]
#define fin for(i=0;i<n;i++)
#define fim for(i=0;i<m;i++)
#define fjn for(j=0;j<n;j++)
#define fjm for(j=0;j<m;j++)
#define bi back_inserter
#define v1 a,b,c,d;
#define v2 i,j,k,l;
#define v3 p,q,r,s;
#define pll pair<long,long>

using namespace std;

//MiCr0-!cE

int main()
{
    iOS
    long n,m; cin>>n>>m;
    tuple<long,bool,long> a[m];
    for(long i=0;i<m;i++)
    {
        cin>>e1(a[i])>>e2(a[i]); e3(a[i])=i;
    }
    sort(a,a+m);
    pair<long,long> ans[m];
    long t1,t2,t3;
    bool xx=true;t1=2;t2=2;t3=3;
    for(long i=0;i<m;i++)
    {
        if(e2(a[i]))
        {
            ans[e3(a[i])]=mp(1,t1++);
        }
        else
        {
            if(t1<=t3) xx=false;
            ans[e3(a[i])]=mp(t2,t3++);
            if(t3>t1)
            {
                t2++;
                t3=t2+1;
            }
        }
    }
    if(t1!=n+1) xx=false;
    if(!xx) cout<<"-1";
    else
    {
        for(long i=0;i<m;i++)
            cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
#define maxv 10000
#define INF maxv
#define iOS ios::sync_with_stdio(false);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define e1 get<0>
#define e2 get<1>
#define e3 get<2>
#define ins insert
#define ms memset
#define so sizeof
#define itr ::iterator
#define in_an for(i=0;i<n;i++) cin>>a[i]
#define in_bn for(i=0;i<n;i++) cin>>b[i]
#define fin for(i=0;i<n;i++)
#define fim for(i=0;i<m;i++)
#define fjn for(j=0;j<n;j++)
#define fjm for(j=0;j<m;j++)
#define bi back_inserter
#define v1 a,b,c,d;
#define v2 i,j,k,l;
#define v3 p,q,r,s;
#define pll pair<long,long>

using namespace std;

//MiCr0-!cE

bool mycomp(tuple<long,bool,long> a,tuple<long,bool,long> b)
{
    if(e1(a)<e1(b)) return true;
    else if(e1(a)>e1(b)) return false;
    else
    {
        if(e2(a)) return true;
        else return false;
    }
}

int main()
{
    iOS
    long n,m; cin>>n>>m;
    tuple<long,bool,long> a[m];
    for(long i=0;i<m;i++)
    {
        cin>>e1(a[i])>>e2(a[i]); e3(a[i])=i;
    }
    sort(a,a+m,mycomp);
    pair<long,long> ans[m];
    long t1,t2,t3;
    bool xx=true;t1=2;t2=2; t3=3;
    for(long i=0;i<m;i++)
    {
        if(e2(a[i]))
        {
            ans[e3(a[i])]=mp(1,t1++);
        }
        else
        {
            if(t3>=t1) xx=false;
            ans[e3(a[i])]=mp(t2++,t3);
            if(t2>=t3)
            {
                t3++;
                t2=2;
            }
        }
    }
    if(t1!=n+1) xx=false;
    if(!xx) cout<<"-1";
    else
    {
        for(long i=0;i<m;i++)
            cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
#define maxv 10000
#define INF maxv
#define iOS ios::sync_with_stdio(false);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define e1 get<0>
#define e2 get<1>
#define e3 get<2>
#define ins insert
#define ms memset
#define so sizeof
#define itr ::iterator
#define in_an for(i=0;i<n;i++) cin>>a[i]
#define in_bn for(i=0;i<n;i++) cin>>b[i]
#define fin for(i=0;i<n;i++)
#define fim for(i=0;i<m;i++)
#define fjn for(j=0;j<n;j++)
#define fjm for(j=0;j<m;j++)
#define bi back_inserter
#define v1 a,b,c,d;
#define v2 i,j,k,l;
#define v3 p,q,r,s;
#define pll pair<long,long>

using namespace std;

//MiCr0-!cE

bool mycomp(tuple<long,bool,long> a,tuple<long,bool,long> b)
{
    if(e1(a)<e1(b)) return true;
    else if(e1(a)>e1(b)) return false;
    else
    {
        if(e2(a)) return true;
        else return false;
    }
}

int main()
{
    iOS
    long n,m; cin>>n>>m;
    tuple<long,bool,long> a[m];
    for(long i=0;i<m;i++)
    {
        cin>>e1(a[i])>>e2(a[i]); e3(a[i])=i;
    }
    sort(a,a+m,mycomp);
    pair<long,long> ans[m];
    long t1,t2,t3; long t[m]; for(long i=0;i<m;i++) t[i]=max(3L,i+1);
    bool xx=true;t1=2;t2=2;
    for(long i=0;i<m;i++)
    {
        if(e2(a[i]))
        {
            ans[e3(a[i])]=mp(1,t1++);
            t2=2;
            while(t[t2]>=t1&&t2<t1-1) t2++;
        }
        else
        {
            while(t[t2]>=t1&&t2<t1-1) t2++;
            if(t2>=t1-1){xx=false;break;}
            ans[e3(a[i])]=mp(t2,t[t2]++);
         }
    }
    if(t1!=n+1) xx=false;
    if(!xx) cout<<"-1";
    else
    {
        for(long i=0;i<m;i++)
            cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
#define maxv 10000
#define INF maxv
#define iOS ios::sync_with_stdio(false);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define e1 get<0>
#define e2 get<1>
#define e3 get<2>
#define ins insert
#define ms memset
#define so sizeof
#define itr ::iterator
#define in_an for(i=0;i<n;i++) cin>>a[i]
#define in_bn for(i=0;i<n;i++) cin>>b[i]
#define fin for(i=0;i<n;i++)
#define fim for(i=0;i<m;i++)
#define fjn for(j=0;j<n;j++)
#define fjm for(j=0;j<m;j++)
#define bi back_inserter
#define v1 a,b,c,d;
#define v2 i,j,k,l;
#define v3 p,q,r,s;
#define pll pair<long,long>

using namespace std;

//MiCr0-!cE

int main()
{
    iOS
    long n,m; cin>>n>>m;
    tuple<long,bool,long> a[m];
    for(long i=0;i<m;i++)
    {
        cin>>e1(a[i])>>e2(a[i]); e3(a[i])=i;
    }
    sort(a,a+m);
    pair<long,long> ans[m];
    long t1,t2,t3;
    bool xx=true;t1=2;t2=2;t3=3;
    for(long i=0;i<m;i++)
    {
        if(e2(a[i]))
        {
            ans[e3(a[i])]=mp(1,t1++);
        }
        else
        {
            if(t1<t3) xx=false;
            ans[e3(a[i])]=mp(t2,t3++);
            if(t3>t1)
            {
                t2++;
                t3=t2+1;
            }
        }
    }
    if(t1!=n+1) xx=false;
    if(t2==n) xx=false;
    if(!xx) cout<<"-1";
    else
    {
        for(long i=0;i<m;i++)
            cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
#define maxv 10000
#define INF maxv
#define iOS ios::sync_with_stdio(false);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define e1 get<0>
#define e2 get<1>
#define e3 get<2>
#define ins insert
#define ms memset
#define so sizeof
#define itr ::iterator
#define in_an for(i=0;i<n;i++) cin>>a[i]
#define in_bn for(i=0;i<n;i++) cin>>b[i]
#define fin for(i=0;i<n;i++)
#define fim for(i=0;i<m;i++)
#define fjn for(j=0;j<n;j++)
#define fjm for(j=0;j<m;j++)
#define bi back_inserter
#define v1 a,b,c,d;
#define v2 i,j,k,l;
#define v3 p,q,r,s;
#define pll pair<long,long>

using namespace std;

//MiCr0-!cE

int main()
{
    iOS
    long n,m,p,q;
    cin>>n>>m;
    pair<pair<long,bool>,long> a[m];
    for(long i=0;i<m;i++)
    {
        cin>>p>>q;
        a[i].ff=mp(p,1-q);
        a[i].ss=i;
    }
    sort(a,a+m);
    pair<long,long> ans[m];
    long t1,t2,t3;
    bool xx=true;t1=2;t2=2; t3=3;
    for(long i=0;i<m;i++)
    {
        if(a[i].ff.ss==0)
        {
            ans[a[i].ss]=mp(1,t1++);
        }
        else
        {
            if(t3>=t1) xx=false;
            ans[a[i].ss]=mp(t2++,t3);
            if(t2>=t3)
            {
                t3++;
                t2=2;
            }
        }
    }
    if(t1!=n+1) xx=false;
    if(!xx) cout<<"-1";
    else
    {
        for(long i=0;i<m;i++)
            cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    }
    return 0;
}