#include <bits/stdc++.h>
#define NN 10000
using namespace std;
typedef long long INT;

int p[15];
bool vst[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		memset(vst, 0, sizeof(vst));
		int n, cnt = 0;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		for(int i = 0; i < n; i++){
			if(vst[p[i]] == 0) {vst[p[i]]++; continue;}
			while(1){
				int t = 1;
				cnt++;
				if(vst[p[i] + t * 1000] == 0 && p[i] + t * 1000 < 10000) {p[i] = p[i] + t * 1000; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 100] == 0 && p[i] % 1000 + t * 100 < 1000) {p[i] = p[i] + t * 100; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 10] == 0 && p[i] % 100 + t * 10 < 100){p[i] = p[i] + t * 10; vst[p[i]] = 1; break;}
				if(vst[p[i] + t] == 0 && p[i] % 10 + t < 10) {p[i] = p[i] + t; vst[p[i]] = 1; break;}
				t++;
			}
		}
		cout<<cnt<<endl;
		for(int i = 0; i < n; i++){
			if(p[i] < 1000) cout<<'0';
			if(p[i] < 100) cout<<'0';
			if(p[i] < 10) cout<<'0';
			cout<<p[i]<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>

#define NN 11000

using namespace std;

typedef long long INT;

string s[10];
int cnt[NN],vst[10];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s[i];
		int k=0;
		for(int j=0;j<4;j++)k+=(s[i][j]-'0')*pow(10,3-j);
		cnt[k]++;
		vst[s[i][0]-'0']++;
	}
	int ans=0;
	for(int i=0;i<1e4;i++){
		ans+=max(0,cnt[i]-1);
	}
	cout<<ans<<endl;

	for(int i=0;i<n;i++){
		int k=0;
		for(int j=0;j<4;j++)k+=(s[i][j]-'0')*pow(10,3-j);
		if(cnt[k]==1)cout<<s[i]<<endl;
		else if(cnt[k]>1){
			for(int j=0;j<=9;j++){
				if(vst[j]==0){
					s[i][0]='0'+j;
					vst[j]++;
					cnt[k]--;
					break;
				}
			}
			cout<<s[i]<<endl;
		}
	}

	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long INT;

vector<int> res[1111];
map<vector<int>, int > mp;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int T;
	cin >> T;

	while(T--) {
		int n;
		cin >> n;
		for(int i=1; i<=n; i++) res[i].clear();
		mp.clear();

		for(int i=1; i<=n; i++) {
			res[i].clear();
			for(int j=1; j<=4; j++) {
				int u;
				scanf("%1d", &u);
				res[i].push_back(u);
			}
			mp[res[i]]++;
		}

		int ans= 0;

		for(int i=1; i<=n; i++) {
			if(mp[res[i]] == 1) continue;
			mp[res[i]]--; ans++;
			for(int j=0; j<=9; j++) {
				res[i][res[i].size()-1] = j;
				if(!mp[res[i]]) {
					mp[res[i]] = 1;
					break;
				}
			}
		}

		cout << ans << endl;
		for(int i=1; i<=n; i++) {
			for(auto x : res[i]) printf("%d", x);
			puts("");
		}

	}
	return 0;
}


#include <bits/stdc++.h>
#define NN 10000
using namespace std;
typedef long long INT;

int p[15];
bool vst[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		memset(vst, 0, sizeof(vst));
		int n, cnt = 0;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		for(int i = 0; i < n; i++){
			if(vst[p[i]] == 0) {vst[p[i]]++; continue;}
			while(1){
				int t = 1;
				cnt++;
				if(vst[p[i] + t * 1000] == 0) {p[i] = p[i] + t * 1000; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 100] == 0 && p[i] % 1000 + t * 100 < 1000) {p[i] = p[i] + t * 100; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 10] == 0 && p[i] % 100 + t * 10 < 100){p[i] = p[i] + t * 10; vst[p[i]] = 1; break;}
				if(vst[p[i] + t] == 0 && p[i] % 10 + t < 10) {p[i] = p[i] + t; vst[p[i]] = 1; break;}
				t++;
			}
		}
		cout<<cnt<<endl;
		for(int i = 0; i < n; i++){
			if(p[i] < 1000) cout<<'0';
			if(p[i] < 100) cout<<'0';
			if(p[i] < 10) cout<<'0';
			cout<<p[i]<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int p[20];
bool vst[20];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		memset(vst, 0, sizeof(vst));
		int n, cnt = 0;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", &p[i]);
			vst[p[i] / 1000] = 1;
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < i; j++){
				if(p[i] == p[j]) break;
			}
			cnt++;
		}
		cout<<cnt<<endl;
		for(int i = 0; i < n; i++){
			int flag = 0;
			for(int j = 0; j < i; j++){
				if(p[i] == p[j]) {
					for(int k = 0; k < 10; k++){
						if(vst[k]) continue;
						vst[k] = 1;
						flag = 1;
						int t = p[i] % 1000 + k * 1000;
						cout<<t / 1000<<(t/100)%10<<(t/10)%10<<t%10<<endl;
						break;
					}
				}
				if(flag) break;
			}
			if(flag) continue;
			cout<<p[i]/1000<<(p[i]/100)%10<<(p[i]/10)%10<<p[i]%10<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define NN 10000
using namespace std;
typedef long long INT;

int p[100];
bool vst[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n, cnt = 0;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		for(int i = 0; i < n; i++){
			if(vst[p[i]] == 0) {vst[p[i]]++; continue;}
			while(1){
				int t = 1;
				cnt++;
				if(vst[p[i] + t] == 0 && p[i] % 10 + t < 10) {p[i] = p[i] + t; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 10] == 0 && p[i] % 100 + t * 10 < 100){p[i] = p[i] + t * 10; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 100] == 0 && p[i] % 1000 + t * 100 < 1000) {p[i] = p[i] + t * 100; vst[p[i]] = 1; break;}
				if(vst[p[i] + t * 1000] == 0) {p[i] = p[i] + t * 1000; vst[p[i]] = 1; break;}
				t++;
			}
		}
		cout<<cnt<<endl;
		for(int i = 0; i < n; i++){
			if(p[i] < 1000) cout<<'0';
			if(p[i] < 100) cout<<'0';
			if(p[i] < 10) cout<<'0';
			cout<<p[i]<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<string, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

string s[22];
int vst[22], flag[22];
pii p[22];
string ans[22];

inline void solve() {
	int n; scanf("%d", &n);
	for(int i=1; i<=n; i++) cin>>p[i].x, p[i].y=i, vst[i]=0;
	sort(p+1, p+n+1);

	for(int i=1, j=1; i<=n; i=j) {
		for(; j<=n && p[i].x==p[j].x; j++);
		for(int k=i+1; k<j; k++) vst[k]=1;
	}
	memset(flag, 0, sizeof flag);
	for(int i=1; i<=n; i++) {
		int c=p[i].x[0]-'0';
		if(vst[i]==0) flag[c]=1;
	}

	vector<int> vec;
	for(int i=0; i<=9; i++) if(!flag[i]) vec.pb(i);

	int num=0;
	for(int i=1; i<=n; i++) if(vst[i]) {
		p[i].x[0]=vec.back()+'0'; vec.pop_back();
		num++;
	}
	printf("%d\n", num);
	for(int i=1; i<=n; i++) ans[p[i].y]=p[i].x;
	for(int i=1; i<=n; i++) {
		for(int j=0; j<4; j++) cout<<ans[i][j]; puts("");
	}
}

int main() {
#ifdef kong
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) solve();
}


#include<bits/stdc++.h>
#define bclz(x) (__builtin_clz(x))
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int,int> pii;
char a[100][10];
set <int> st;
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i=0;i<=9;i++) st.insert(i);
		for(int i=1;i<=n;i++) scanf("%s",a+i),st.erase(a[i][0]-'0');
		int ans=0;
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				if(a[i][0]==a[j][0] and a[i][1]==a[j][1] and a[i][2]==a[j][2] and a[i][3]==a[j][3]) {
					a[i][0]=char('0'+*st.begin());
					st.erase(st.begin());
					ans++;
				}
			}
		}
		cout<<ans<<endl;
		for(int i=1;i<=n;i++) cout<<a[i]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 100010
using namespace std;
typedef long long INT;
typedef pair<int,int> pii;
char s[15][5];
int ff[5][15];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<4;i++)for(int j=0;j<=9;j++) ff[i][j]=0;
		for(int i=0;i<n;i++){
			cin>>s[i];
			for(int j=0;j<4;j++)ff[j][s[i][j]-'0']++;
		}
		int cnt=0;
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				int f=0;
				for(int k=0;k<4;k++) if(s[i][k]!=s[j][k]) f=1;
				if(!f){
					cnt++;
					for(int r=0;r<=9;r++) if(ff[0][r]==0) {ff[0][r]=1;s[i][0]=r+'0';break;}
				}
			}
		}
		cout<<cnt<<endl;
		for(int i=0;i<n;i++) cout<<s[i]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define x first
#define y second

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

set<int> st;
string s[11];
int n, flag[11];

int solve() {
	cin>>n;
	for (int i=0; i<n; i++) cin>>s[i];
	memset (flag, 0, sizeof (flag));
	st.clear();
	for (int i=0; i<10; i++) st.insert(i);
	for (int i=0; i<n; i++) if (st.count(s[i][0]-'0')) st.erase(s[i][0]-'0');
	int ans=0;
	for (int i=0; i<n; i++)
		for (int j=i+1; j<n; j++)
			if (s[i]==s[j]) {
				ans++;
				s[j][0]=*st.begin()+'0';
				st.erase(st.begin());
			}
	cout<<ans<<endl;
	for (int i=0; i<n; i++) cout<<s[i]<<endl;
}

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);
	int T;
	for (cin>>T; T--;) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define NN 11111
string a[NN];
map<string,int> cnt;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	scanf("%d",&T);

	while(T--){
		cnt.clear();
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			cin>>a[i];
			cnt[a[i]]++;
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			if(cnt[a[i]]>1){
				ans++;
				cnt[a[i]]--;
				for(int j=0;j<=9;j++){
					string aa;
					for(int k=0;k<3;k++) aa+=a[i][k];
					aa+=('0'+j);
					if(cnt[aa]>0) continue;
					a[i]=aa;
					cnt[aa]=1;
					break;
				}
			}
		}
		printf("%d\n",ans);
		for(int i=1;i<=n;i++) cout<<a[i]<<endl;
	}
    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 100010
using namespace std;
typedef long long INT;
typedef pair<int,int> pii;
char s[15][5];
int ff[5][15];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>s[i];
			for(int j=0;j<4;j++)ff[j][s[i][j]-'0']++;
		}
		int cnt=0;
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				int f=0;
				for(int k=0;k<4;k++) if(s[i][k]!=s[j][k]) f=1;
				if(!f){
					cnt++;
					for(int r=0;r<=9;r++) if(ff[0][r]==0) {ff[0][r]=1;s[i][0]=r+'0';break;}
				}
			}
		}
		cout<<cnt<<endl;
		for(int i=0;i<n;i++) cout<<s[i]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int p[20];
bool vst[20];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n, cnt = 0;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", &p[i]);
			vst[p[i] / 1000] = 1;
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < i; j++){
				if(p[i] == p[j]) break;
			}
			cnt++;
		}
		cout<<cnt<<endl;
		for(int i = 0; i < n; i++){
			int flag = 0;
			for(int j = 0; j < i; j++){
				if(p[i] == p[j]) {
					for(int k = 0; k < 10; k++){
						if(vst[k]) continue;
						vst[k] = 1;
						flag = 1;
						int t = p[i] % 1000 + k * 1000;
						cout<<t / 1000<<(t/100)%10<<(t/10)%10<<t%10<<endl;
						break;
					}
				}
				if(flag) break;
			}
			if(flag) continue;
			cout<<p[i]/1000<<(p[i]/100)%10<<(p[i]/10)%10<<p[i]%10<<endl;
		}
	}
	return 0;
}


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

int p[20];
bool vst[20];
int m[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		memset(vst, 0, sizeof(vst));
		int n, cnt = 0;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", &p[i]);
			vst[p[i] / 1000] = 1;
			m[p[i]]++;
		}
		for(int i = 0; i < n; i++){
			if(m[p[i]] == 0) continue;
			cnt += m[p[i]] - 1;
			m[p[i]] = 0;
		}
		cout<<cnt<<endl;
		for(int i = 0; i < n; i++){
			int flag = 0;
			for(int j = 0; j < i; j++){
				if(p[i] == p[j]) {
					for(int k = 0; k < 10; k++){
						if(vst[k]) continue;
						vst[k] = 1;
						flag = 1;
						int t = p[i] % 1000 + k * 1000;
						cout<<t / 1000<<(t/100)%10<<(t/10)%10<<t%10<<endl;
						break;
					}
				}
				if(flag) break;
			}
			if(flag) continue;
			cout<<p[i]/1000<<(p[i]/100)%10<<(p[i]/10)%10<<p[i]%10<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
typedef vector<int> VI;
int power(int a, int b, int m, int ans=1) {
	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;
	return ans;
}

string s[11];
int flag[11];

int solve() {
	int n;
	cin >> n;
	for(int i=0; i<n; i++) cin >> s[i], flag[i] = 0;
	int ans = 0;
	set<int> ss;

	for(int i=0; i<10; i++) ss.insert(i);
	for(int i=0; i<10; i++) {
		int u = s[i][0]-'0';
		if(ss.count(u)) ss.erase(u);
	}

	for(int i=0; i<n; i++) if(flag[i] == 0) {
		for(int j=i+1; j<n; j++) if(s[i] == s[j]) {
			flag[j] = 1;
			ans++;
			s[j][0] = *ss.begin() + '0';
			ss.erase(ss.begin());
		}
	}
	cout << ans << endl;
	for(int i=0; i<n; i++) cout << s[i] << endl;
}

int main() {
#ifndef	ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	cin >> T;
	while(T--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define NN 11111
string a[NN];
map<string,int> cnt;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	scanf("%d",&T);

	while(T--){
		cnt.clear();
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			cin>>a[i];
			cnt[a[i]]++;
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			if(cnt[a[i]]>1){
				ans++;
				cnt[a[i]]--;
				for(int j=0;j<9;j++){
					string aa;
					for(int k=0;k<3;k++) aa+=a[i][k];
					aa+=('0'+j);
					if(cnt[aa]>0) continue;
					a[i]=aa;
					cnt[aa]=1;
					break;
				}
			}
		}
		printf("%d\n",ans);
		for(int i=1;i<=n;i++) cout<<a[i]<<endl;
	}
    return 0;
}


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

int main() {
#ifdef KIMNAM
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int T;
  cin>>T;
  char s[11][11];
  int n, m;
  while(T--) {
    cin>>n;
    for(int i=0; i<n; i++) {
      cin>>s[i];
    }
    for(int i=m=0; i<n; i++) {
      for(int j=i+1; j<n; j++) {
        if(strcmp(s[i], s[j]) == 0) {
          for(int c='0'; c<='9'; c++) {
            int ok=1;
            for(int r=0; r<n; r++) {
              if(r!=i && s[r][0] == c) {
                ok=0;
                break;
              }
            }
            if(ok) {
              s[i][0]=c;
              m++;
              break;
            }
          }
        }
      }
    }
    cout<<m<<endl;
    for(int i=0; i<n; i++) {
      cout<<s[i]<<endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
//#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x,m) (((x)%m*(x)%m)%m)%m
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll mod           = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
#define N 10001
const ll N2      = 70;
const ld PI      = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main(){
    fastio();
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      unordered_multiset<string>p;
      set<string>f;
      string s,r[n];
      for(ll i=0;i<n;i++){
        cin>>s;
        r[i]=s;
        p.insert(s);
        f.insert(s);
      }
      ll cnt=0;
      for(auto i:f){
        if(p.count(i)!=1){
          ll ans=p.count(i)-1;
          while(ans--){
           for(ll j=0;j<4;j++){
             string u=i;
             for(char k='0';k<='9';k++){
               u[j]=k;
               if(p.find(u)==p.end()){
                 cnt++;
                 p.insert(u);
                 p.erase(p.find(i));
                 goto str;
               }
             }
           }
           str:
             if(0){}
          }//ans
        }//p.count(i)!=1
      }
      /*cout<<"+++++++++++++++++++++"<<endl;
      for(ll i=0;i<n;i++){
        cout<<r[i]<<endl;
      }
      cout<<"---------------------"<<endl;

      for(auto i:p){
        cout<<i<<endl;
      }
      cout<<"?????????????????????"<<endl;*/
      cout<<cnt<<endl;
      vector<string>q;
      for(ll i=0;i<n;i++){
        q.pb(r[i]);
      }
      for(auto i:p){
        bool ok=0;
        string uu=i;
        for(ll j=0;j<n;j++){
          if(uu==r[j]){
            ok=1;
            r[j]='@';
            break;
          }
        }
        if(ok==1){
          continue;
        }
        string u=i;
        for(ll j=0;j<n;j++){
          ll cnt=0;
          for(ll k=0;k<4;k++){
            if(r[j][k]!=u[k]){
              cnt++;
            }
          }
          //cout<<cnt<<" cnt "<<endl;
          if(cnt==1){
            q[j]=i;
            r[j]="@";
            break;
          }
        }
      }
      for(ll i=0;i<n;i++){
        cout<<q[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
//#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x,m) (((x)%m*(x)%m)%m)%m
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll mod           = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
#define N 10001
const ll N2      = 70;
const ld PI      = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main(){
    fastio();
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      multiset<string>p;
      set<string>f;
      string s;
      for(ll i=1;i<=n;i++){
        cin>>s;
        p.insert(s);
        f.insert(s);
      }
      ll cnt=0;
      for(auto i:f){
        if(p.count(i)!=1){
          ll ans=p.count(i)-1;
          while(ans--){
           for(ll j=0;j<4;j++){
             string u=i;
             for(char k='0';k<='9';k++){
               u[j]=k;
               if(p.find(u)==p.end()){
                 cnt++;
                 p.insert(u);
                 p.erase(p.find(i));
                 goto str;
               }
             }
           }
           str:
             if(0){}
          }//ans
        }//p.count(i)!=1
      }
      cout<<cnt<<endl;
      for(auto i:p){
        cout<<i<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
//#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x,m) (((x)%m*(x)%m)%m)%m
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll mod           = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
#define N 10001
const ll N2      = 70;
const ld PI      = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main(){
    fastio();
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      unordered_multiset<string>p;
      set<string>f;
      string s,r[n];
      for(ll i=0;i<n;i++){
        cin>>s;
        r[i]=s;
        p.insert(s);
        f.insert(s);
      }
      ll cnt=0;
      for(auto i:f){
        if(p.count(i)!=1){
          ll ans=p.count(i)-1;
          while(ans--){
           for(ll j=0;j<4;j++){
             string u=i;
             for(char k='0';k<='9';k++){
               u[j]=k;
               if(p.find(u)==p.end()){
                 cnt++;
                 p.insert(u);
                 p.erase(p.find(i));
                 goto str;
               }
             }
           }
           str:
             if(0){}
          }//ans
        }//p.count(i)!=1
      }
      /*cout<<"+++++++++++++++++++++"<<endl;
      for(ll i=0;i<n;i++){
        cout<<r[i]<<endl;
      }
      cout<<"---------------------"<<endl;

      for(auto i:p){
        cout<<i<<endl;
      }
      cout<<"?????????????????????"<<endl;*/
      cout<<cnt<<endl;
      vector<string>q;
      for(auto i:p){
        bool ok=0;
        string uu=i;
        for(ll j=0;j<n;j++){
          if(uu==r[j]){
            ok=1;
            r[j]='@';
            break;
          }
        }
        if(ok==1){
          q.pb(uu);
          continue;
        }
        string u=i;
        for(ll j=0;j<n;j++){
          ll cnt=0;
          for(ll k=0;k<4;k++){
            if(r[j][k]!=u[k]){
              cnt++;
            }
          }
          //cout<<cnt<<" cnt "<<endl;
          if(cnt==1){
            q.pb(u);
            r[j]="@";
            break;
          }
        }
      }
      for(ll i=0;i<n;i++){
        cout<<q[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
//#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x,m) (((x)%m*(x)%m)%m)%m
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll mod           = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
#define N 10001
const ll N2      = 70;
const ld PI      = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main(){
    fastio();
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        string s[n];
        for (int i = 0; i < n; i++)
        {
            cin >> s[i];
        }
        bool vis[10];
        int cnt = 0;
        for (int i = 0; i <n-1; i++)
        {
            string t = s[i];
            fill(vis,0);
            for (int j = i + 1; j < n; j++)
            {
                if (s[j][0] != t[0] || s[j][1] != t[1] || s[j][2] != t[2] || s[j][3] != t[3])
                {
                    continue;
                }
                if (s[j][0] == t[0])
                {
                    for (int k = 0; k < n; k++)
                    {
                        if (k != j)
                        {
                            vis[s[k][0] - '0']++;
                        }
                    }

                    for (int k = 0; k < 10; k++)
                    {
                        if (!vis[k])
                        {
                            s[j][0] = k + '0';
                            break;
                        }
                    }
                    cnt++;
                    continue;
                }

            }
        }
        cout<<cnt<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<s[i]<<endl;
        }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      vector< pair<string,ll> >p(n);
      map<string,ll>mp;
      map<string,ll>p2;
      set<string>s;
      for(ll i=0;i<n;i++){
        string ss;
        cin>>ss;
        p[i].ff=ss;
        p[i].ss=i;
        mp[p[i].ff]++;
        p2[p[i].ff]=0;
      }
      for(auto i:p2){
        s.insert(i.first);
      }
      ll cnt=0;
      ll o=3;
      for(ll i=0;i<n;i++){
        if(mp[p[i].ff]>=2){
          cnt++;
          mp[p[i].ff]--;
          p2[p[i].ff]++;
          string u=p[i].ff;
          ll ans=p2[p[i].ff]-1;
          char k='0';
          while(ans--){
            k++;
          }
          if(k>'9'){
              o--;
              k='0';
          }
          u[o]=k;
          if(s.find(u)!=s.end() or u==p[i].ff){
            k+=2;
            if(k>'9'){
              o--;
              k=0;
            }
            p2[p[i].ff]+=2;
          }
          u[o]=k;
          p[i].ff=u;
        }
      }
      string a[n];
      cout<<cnt<<endl;
      for(ll i=0;i<n;i++){
        a[p[i].ss]=p[i].ff;
      }
      for(ll i=0;i<n;i++){
        cout<<a[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      vector< pair<string,ll> >p(n);
      map<string,ll>mp;
      map<string,ll>p2;
      set<string>s;
      for(ll i=0;i<n;i++){
        cin>>p[i].ff;
        s.insert(p[i].ff);
        p[i].ss=i;
        mp[p[i].ff]++;
        p2[p[i].ff]=0;
      }
      ll cnt=0;
      for(ll i=0;i<n;i++){
        if(mp[p[i].ff]>=2){
          cnt++;
          mp[p[i].ff]--;
          p2[p[i].ff]++;
          string u=p[i].ff;
          ll ans=p2[p[i].ff]-1;
          char k='0';
          while(ans--){
            k++;
          }
          u[3]=k;
          if(s.find(u)!=s.end()){
            k++;
            p2[p[i].ff]++;
          }
          u[3]=k;
          p[i].ff=u;
        }
      }
      string a[n];
      cout<<cnt<<endl;
      for(ll i=0;i<n;i++){
        a[p[i].ss]=p[i].ff;
      }
      for(ll i=0;i<n;i++){
        cout<<a[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
//#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x,m) (((x)%m*(x)%m)%m)%m
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll mod           = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
#define N 10001
const ll N2      = 70;
const ld PI      = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main(){
    fastio();
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      unordered_multiset<string>p;
      set<string>f;
      string s,r[n];
      for(ll i=0;i<n;i++){
        cin>>s;
        r[i]=s;
        p.insert(s);
        f.insert(s);
      }
      ll cnt=0;
      for(auto i:f){
        if(p.count(i)!=1){
          ll ans=p.count(i)-1;
          while(ans--){
           for(ll j=0;j<4;j++){
             string u=i;
             for(char k='0';k<='9';k++){
               u[j]=k;
               if(p.find(u)==p.end()){
                 cnt++;
                 p.insert(u);
                 p.erase(p.find(i));
                 goto str;
               }
             }
           }
           str:
             if(0){}
          }//ans
        }//p.count(i)!=1
      }
      /*cout<<"+++++++++++++++++++++"<<endl;
      for(auto i:p){
        cout<<i<<endl;
      }
      cout<<"*********************"<<endl;*/
      cout<<cnt<<endl;
      for(auto i:p){
        bool ok=0;
        for(ll j=0;j<n;j++){
          if(i==r[j]){
            ok=1;
            break;
          }
        }
        if(ok==1){
          continue;
        }
        const string u=i;
        for(ll j=0;j<n;j++){
          ll cnt=0;
          for(ll k=0;k<4;k++){
            if(r[j][k]!=u[k]){
              cnt++;
            }
          }
          //cout<<cnt<<" cnt "<<endl;
          if(cnt==1){
            r[j]=u;
            break;
          }
        }
      }
      for(ll i=0;i<n;i++){
        cout<<r[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      vector< pair<string,ll> >p(n);
      map<string,ll>mp;
      map<string,ll>p2;
      for(ll i=0;i<n;i++){
        cin>>p[i].ff;
        p[i].ss=i;
        mp[p[i].ff]++;
        p2[p[i].ff]=0;
      }
      ll cnt=0;
      for(ll i=0;i<n;i++){
        if(mp[p[i].ff]>=2){
          cnt++;
          mp[p[i].ff]--;
          p2[p[i].ff]++;
          string u=p[i].ff;
          ll ans=p2[p[i].ff]-1;
          char k='0';
          while(ans--){
            k++;
          }
          u[3]=k;
          p[i].ff=u;
        }
      }
      string a[n];
      cout<<cnt<<endl;
      for(ll i=0;i<n;i++){
        a[p[i].ss]=p[i].ff;
      }
      for(ll i=0;i<n;i++){
        cout<<a[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
//#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x,m) (((x)%m*(x)%m)%m)%m
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll mod           = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
#define N 10001
const ll N2      = 70;
const ld PI      = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main(){
    fastio();
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        string s[n];
        for (int i = 0; i < n; i++)
        {
            cin >> s[i];
        }
        bool vis[10];
        int cnt = 0;
        for (int i = 0; i <n-1; i++)
        {
            string t = s[i];
            fill(vis, vis + 10, 0);
            for (int j = i + 1; j < n; j++)
            {
                if (s[j][0] != t[0] || s[j][1] != t[1] || s[j][2] != t[2] || s[j][3] != t[3])
                {
                    continue;
                }
                if (s[j][0] == t[0])
                {
                    for (int k = 0; k < n; k++)
                    {
                        if (k != j)
                        {
                            vis[s[k][0] - '0']++;
                        }
                    }

                    for (int k = 0; k < 10; k++)
                    {
                        if (!vis[k])
                        {
                            s[j][0] = k + '0';
                            break;
                        }
                    }
                    cnt++;
                    continue;
                }

            }
        }
        cout<<cnt<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<s[i]<<endl;
        }
    }
}
    //printclock;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T>using onset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
void solve(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  #endif
}
signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf
    ll t;
    cin>>t;
    while(t--){
      ll n;
      cin>>n;
      vector< pair<string,ll> >p(n);
      map<string,ll>mp;
      map<string,ll>p2;
      for(ll i=0;i<n;i++){
        cin>>p[i].ff;
        p[i].ss=i;
        mp[p[i].ff]++;
        p2[p[i].ff]=0;
      }
      for(ll i=0;i<n;i++){
        if(mp[p[i].ff]>=2){
          mp[p[i].ff]--;
          p2[p[i].ff]++;
          string u=p[i].ff;
          ll ans=p2[p[i].ff]-1;
          char k='0';
          while(ans--){
            k++;
          }
          u[3]=k;
          p[i].ff=u;
        }
      }
      string a[n];
      for(ll i=0;i<n;i++){
        a[p[i].ss]=p[i].ff;
      }
      for(ll i=0;i<n;i++){
        cout<<a[i]<<endl;
      }
    }
    //printclock;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
#define INF 514514514
#define FOR(it,c) for ( __typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++ )
using namespace std;
struct P {
	int x,y;
	void read() { scanf("%d%d",&x,&y); }
} p[N];
struct Rec {
	int x1,y1,x2,y2,z;
	Rec(){}
	Rec( P q ):x1(q.x),y1(q.y),x2(q.x),y2(q.y){}
	void read() { scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&z); }
	inline bool cover( const Rec &r ) const { return x1<=r.x1 && r.x2<=x2 && y1<=r.y1 && r.y2<=y2; }
	inline bool jiao( const Rec &r ) const { return !(x2<r.x1||x1>r.x2) && !(y2<r.y1||y1>r.y2); }
} rec[N];
inline bool cpz( int a, int b ) { return rec[a].z<rec[b].z; }
inline bool cpx( int a, int b ) { return p[a].x!=p[b].x?p[a].x<p[b].x:p[a].y<p[b].y; }
inline bool cpy( int a, int b ) { return p[a].y!=p[b].y?p[a].y<p[b].y:p[a].x<p[b].x; }
inline int key( int id, int lv ) { return lv%2==0?p[id].x:p[id].y; }
struct KD_T {
	Rec val[8*N];
	bool leaf[8*N];
	void pull( int id ) {
		val[id].x1=min(val[id*2].x1,val[id*2+1].x1);
		val[id].y1=min(val[id*2].y1,val[id*2+1].y1);
		val[id].x2=max(val[id*2].x2,val[id*2+1].x2);
		val[id].y2=max(val[id*2].y2,val[id*2+1].y2);
		val[id].z=min(val[id*2].z,val[id*2+1].z);
	}
	void init( int id, int n, int *lst, int lv ) {
		if ( n==1 ) {
			val[id]=p[lst[0]];
			val[id].z=lst[0];
			leaf[id]=1;
			return;
		}
		sort(lst,lst+n,lv%2==0?cpx:cpy);
		init(id*2,n/2,lst,lv+1);
		init(id*2+1,n-n/2,lst+n/2,lv+1);
		pull(id);
	}
	void del_min( int id ) {
		if ( leaf[id] ) {
			val[id].z=INF;
			return;
		}
		if ( val[id*2].z==val[id].z ) del_min(id*2);
		else del_min(id*2+1);
		pull(id);
	}
	void up( int id ) { do pull(id); while ( id/=2 ); }
	pair<int,int> Q( int id, const Rec &r ) {
		if ( !r.jiao(val[id]) ) return make_pair(INF,-1);
		if ( r.cover(val[id]) ) return make_pair(val[id].z,id);
		return min(Q(id*2,r),Q(id*2+1,r));
	}
} kdt;
int n,m;
void input() {
	scanf("%d",&n);
	for ( int i=0; i<n; i++ ) rec[i].read();
	scanf("%d",&m);
	for ( int i=0; i<m; i++ ) p[i].read();
}
int ans[N],lst[N],srt[N];
void build() {
	for ( int i=0; i<m; i++ ) lst[i]=i;
	kdt.init(1,m,lst,0);
}
void solve() {
	for ( int i=0; i<n; i++ ) srt[i]=i;
	sort(srt,srt+n,cpz);
	for ( int i=0; i<n; i++ ) {
		pair<int,int> ret=kdt.Q(1,rec[srt[i]]);
		if ( ret.first==INF ) continue;
		kdt.del_min(ret.second);
		kdt.up(ret.second/2);
		ans[ret.first]=srt[i]+1;
	}
	for ( int i=0; i<m; i++ ) printf("%d\n",ans[i]);
}
int main()
{
	input();
	build();
	solve();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
#define INF 514514514
#define FOR(it,c) for ( __typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++ )
using namespace std;
struct P {
	int x,y;
	void read() { scanf("%d%d",&x,&y); }
} p[N];
struct Rec {
	int x1,y1,x2,y2,z;
	Rec(){}
	Rec( P q ):x1(q.x),y1(q.y),x2(q.x),y2(q.y){}
	void read() {
		scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&z);
	}
	bool cover( Rec r ) { return x1<=r.x1 && r.x2<=x2 && y1<=r.y1 && r.y2<=y2; }
	bool jiao( Rec r ) { return !(x2<r.x1||x1>r.x2) && !(y2<r.y1||y1>r.y2); }
} rec[N];
inline bool cpz( int a, int b ) { return rec[a].z<rec[b].z; }
inline bool cpx( int a, int b ) { return p[a].x!=p[b].x?p[a].x<p[b].x:p[a].y<p[b].y; }
inline bool cpy( int a, int b ) { return p[a].y!=p[b].y?p[a].y<p[b].y:p[a].x<p[b].x; }
int key( int id, int lv ) { return lv%2==0?p[id].x:p[id].y; }
struct KD_T {
	Rec val[64*N];
	bool leaf[64*N];
	void pull( int id ) {
		if (leaf[id]) return;
		val[id].x1=min(val[id*2].x1,val[id*2+1].x1);
		val[id].y1=min(val[id*2].y1,val[id*2+1].y1);
		val[id].x2=max(val[id*2].x2,val[id*2+1].x2);
		val[id].y2=max(val[id*2].y2,val[id*2+1].y2);
		val[id].z=min(val[id*2].z,val[id*2+1].z);
	}
	void init( int id, int n, int *lst, int lv ) {
		if (id >= 32*N) while(1);
		if ( n==0 ) {
			val[id].x1=val[id].y1=+INF;
			val[id].x2=val[id].y2=-INF;
			val[id].z=INF;
			leaf[id]=1;
		//	printf("%d: n=%d, (%d,%d) ~ (%d,%d), %d\n",id,n,val[id].x1,val[id].y1,val[id].x2,val[id].y2,val[id].z);
			return;
		}
		if ( n==1 ) {
			val[id]=p[lst[0]];
			val[id].z=lst[0];
			leaf[id]=1;
		//	printf("%d: n=%d, (%d,%d) ~ (%d,%d), %d\n",id,n,val[id].x1,val[id].y1,val[id].x2,val[id].y2,val[id].z);
			return;
		}
		sort(lst,lst+n,lv%2==0?cpx:cpy);
		int pos=n/2;
		if ( key(lst[0],lv)!=key(lst[n-1],lv) ) while ( pos<n && key(pos,lv)==key(pos-1,lv) ) pos++;
		init(id*2,pos,lst,lv+1);
		init(id*2+1,n-pos,lst+pos,lv+1);
		pull(id);
	//	printf("%d: n=%d, (%d,%d) ~ (%d,%d), %d\n",id,n,val[id].x1,val[id].y1,val[id].x2,val[id].y2,val[id].z);
	}
	void del_min( int id ) {
		if ( leaf[id] ) {
			val[id].z=INF;
			return;
		}
		if ( val[id*2].z==val[id].z ) del_min(id*2);
		else del_min(id*2+1);
		pull(id);
	}
	void up( int id ) {
		while ( id ) {
			pull(id);
			id/=2;
		}
	}
	pair<int,int> Q( int id, Rec r ) {
	//	printf("at %d\n",id);
		if ( !r.jiao(val[id]) ) return make_pair(INF,-1);
		if ( r.cover(val[id]) ) {
			//printf("cover %d\n",id);
			return make_pair(val[id].z,id);
		}
		return min(Q(id*2,r),Q(id*2+1,r));
	}
} kdt;
int n,m;
void input() {
	scanf("%d",&n);
	for ( int i=0; i<n; i++ ) rec[i].read();
	scanf("%d",&m);
	for ( int i=0; i<m; i++ ) p[i].read();
}
int ans[N],lst[N],srt[N];
void build() {
	for ( int i=0; i<m; i++ ) lst[i]=i;
	kdt.init(1,m,lst,0);
}
void solve() {
	for ( int i=0; i<n; i++ ) srt[i]=i;
	sort(srt,srt+n,cpz);
	for ( int i=0; i<n; i++ ) {
		pair<int,int> ret=kdt.Q(1,rec[srt[i]]);
		//printf("ret = (%d,%d)\n",ret.first,ret.second);
		if ( ret.first!=INF ) {
			kdt.del_min(ret.second);
			kdt.up(ret.second/2);
			ans[ret.first]=srt[i]+1;
		}
	}
	for ( int i=0; i<m; i++ ) printf("%d\n",ans[i]);
}
int main()
{
	input();
	build();
	solve();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
#define INF 514514514
#define FOR(it,c) for ( __typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++ )
using namespace std;
struct P {
	int x,y;
	void read() { scanf("%d%d",&x,&y); }
} p[N];
struct Rec {
	int x1,y1,x2,y2,z;
	Rec(){}
	Rec( P q ):x1(q.x),y1(q.y),x2(q.x),y2(q.y){}
	void read() {
		scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&z);
	}
	bool cover( Rec r ) { return x1<=r.x1 && r.x2<=x2 && y1<=r.y1 && r.y2<=y2; }
	bool jiao( Rec r ) { return !(x2<r.x1||x1>r.x2) && !(y2<r.y1||y1>r.y2); }
} rec[N];
inline bool cpz( int a, int b ) { return rec[a].z<rec[b].z; }
inline bool cpx( int a, int b ) { return p[a].x!=p[b].x?p[a].x<p[b].x:p[a].y<p[b].y; }
inline bool cpy( int a, int b ) { return p[a].y!=p[b].y?p[a].y<p[b].y:p[a].x<p[b].x; }
int key( int id, int lv ) { return lv%2==0?p[id].x:p[id].y; }
struct KD_T {
	Rec val[128*N];
	bool leaf[128*N];
	void pull( int id ) {
		val[id].x1=min(val[id*2].x1,val[id*2+1].x1);
		val[id].y1=min(val[id*2].y1,val[id*2+1].y1);
		val[id].x2=max(val[id*2].x2,val[id*2+1].x2);
		val[id].y2=max(val[id*2].y2,val[id*2+1].y2);
		val[id].z=min(val[id*2].z,val[id*2+1].z);
	}
	void init( int id, int n, int *lst, int lv ) {
		if ( n==0 ) {
			val[id].x1=val[id].y1=+INF;
			val[id].x2=val[id].y2=-INF;
			val[id].z=INF;
			leaf[id]=1;
		//	printf("%d: n=%d, (%d,%d) ~ (%d,%d), %d\n",id,n,val[id].x1,val[id].y1,val[id].x2,val[id].y2,val[id].z);
			return;
		}
		if ( n==1 ) {
			val[id]=p[lst[0]];
			val[id].z=lst[0];
			leaf[id]=1;
		//	printf("%d: n=%d, (%d,%d) ~ (%d,%d), %d\n",id,n,val[id].x1,val[id].y1,val[id].x2,val[id].y2,val[id].z);
			return;
		}
		sort(lst,lst+n,lv%2==0?cpx:cpy);
		int pos=n/2;
		/*if ( key(lst[0],lv)!=key(lst[n-1],lv) ) {
			if (lv/2%2)
			while ( pos<n && key(pos,lv)==key(pos-1,lv) ) pos++;
			else
				while ( pos>0 && key(pos,lv)==key(pos-1,lv) ) pos--;
		}*/

		init(id*2,pos,lst,lv+1);
		init(id*2+1,n-pos,lst+pos,lv+1);
		pull(id);
	//	printf("%d: n=%d, (%d,%d) ~ (%d,%d), %d\n",id,n,val[id].x1,val[id].y1,val[id].x2,val[id].y2,val[id].z);
	}
	void del_min( int id ) {
		if ( leaf[id] ) {
			val[id].z=INF;
			return;
		}
		if ( val[id*2].z==val[id].z ) del_min(id*2);
		else del_min(id*2+1);
		pull(id);
	}
	void up( int id ) {
		while ( id ) {
			pull(id);
			id/=2;
		}
	}
	pair<int,int> Q( int id, Rec r ) {
	//	printf("at %d\n",id);
		if ( !r.jiao(val[id]) ) return make_pair(INF,-1);
		if ( r.cover(val[id]) ) {
			//printf("cover %d\n",id);
			return make_pair(val[id].z,id);
		}
		return min(Q(id*2,r),Q(id*2+1,r));
	}
} kdt;
int n,m;
void input() {
	scanf("%d",&n);
	for ( int i=0; i<n; i++ ) rec[i].read();
	scanf("%d",&m);
	for ( int i=0; i<m; i++ ) p[i].read();
}
int ans[N],lst[N],srt[N];
void build() {
	for ( int i=0; i<m; i++ ) lst[i]=i;
	kdt.init(1,m,lst,0);
}
void solve() {
	for ( int i=0; i<n; i++ ) srt[i]=i;
	sort(srt,srt+n,cpz);
	for ( int i=0; i<n; i++ ) {
		pair<int,int> ret=kdt.Q(1,rec[srt[i]]);
		//printf("ret = (%d,%d)\n",ret.first,ret.second);
		if ( ret.first!=INF ) {
			kdt.del_min(ret.second);
			kdt.up(ret.second/2);
			ans[ret.first]=srt[i]+1;
		}
	}
	for ( int i=0; i<m; i++ ) printf("%d\n",ans[i]);
}
int main()
{
	input();
	build();
	solve();
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int nn=100005;
struct mrx{int x1,x2,y1,y2,z,p;}a[nn];
int n,m,l[nn],r[nn];
void read(int &x){
	char ch=getchar();for(;ch<48;ch=getchar());
	for(x=0;ch>=48;ch=getchar())x=x*10+ch-48;
}
inline bool cmp(const mrx &x,const mrx &y){
	return x.z<y.z;
}
int main(){
	read(n);
	for(int i=1;i<=n;++i){
		read(a[i].x1);read(a[i].x2);
		read(a[i].y1);read(a[i].y2);
		read(a[i].z);a[i].p=i;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;++i)l[i]=i-1,r[i]=i+1;
	r[0]=1;l[0]=n;r[n]=0;
	read(m);
	for(int i=1;i<=m;++i){
		int x,y,j=0;
		read(x);read(y);
		for(j=r[0];j;j=r[j])
			if(x>=a[j].x1&&x<=a[j].x2&&y>=a[j].y1&&y<=a[j].y2){
				r[l[j]]=r[j],l[r[j]]=l[j];
				break;
			}
		printf("%d\n",a[j].p);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int nn=100005;
struct mrx{int x1,x2,y1,y2,z,p;}a[nn];
int n,m,l[nn],r[nn];
void read(int &x){
	char ch=getchar();for(;ch<48;ch=getchar());
	for(x=0;ch>=48;ch=getchar())x=x*10+ch-48;
}
inline bool cmp(const mrx &x,const mrx &y){
	return x.z<x.z;
}
int main(){
	read(n);
	for(int i=1;i<=n;++i){
		read(a[i].x1);read(a[i].x2);
		read(a[i].y1);read(a[i].y2);
		read(a[i].z);a[i].p=i;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;++i)l[i]=i-1,r[i]=i+1;
	r[0]=1;l[0]=n;r[n]=0;
	read(m);
	for(int i=1;i<=m;++i){
		int x,y,j=0;
		read(x);read(y);
		for(j=r[0];j;j=r[j])
			if(x>=a[j].x1&&x<=a[j].x2&&y>=a[j].y1&&y<=a[j].y2){
				r[l[j]]=r[j],l[r[j]]=l[j];
				break;
			}
		printf("%d\n",a[j].p);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int nn=100005;
int n,m,p[nn],l[nn],r[nn];
int x1[nn],x2[nn],y1[nn],y2[nn],z[nn];
void read(int &x){
	char ch=getchar();for(;ch<48;ch=getchar());
	for(x=0;ch>=48;ch=getchar())x=x*10+ch-48;
}
inline bool cmp(int x,int y){
	return z[x]<z[y];
}
int main(){
	read(n);
	for(int i=1;i<=n;++i){
		read(x1[i]);read(x2[i]);
		read(y1[i]);read(y2[i]);
		read(z[i]);p[i]=i;
	}
	sort(p+1,p+n+1,cmp);
	for(int i=1;i<=n;++i)
		l[p[i]]=p[i-1],r[p[i]]=p[i+1];
	r[0]=p[1];l[0]=p[n];
	read(m);
	for(int i=1;i<=m;++i){
		int x,y,j=0;
		read(x);read(y);
		for(j=r[0];j;j=r[j])
			if(x>=x1[j]&&x<=x2[j]&&y>=y1[j]&&y<=y2[j])break;
		printf("%d\n",j);
		if(j)r[l[j]]=r[j],l[r[j]]=l[j];
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 100010;
const int maxt = 3000000;
const int inf = 1000000000;

int tt;
int res[maxn];
int p[maxt], key[maxt], opt[maxt], val[maxt], ch[maxt][2];
vector<int> ID[maxn];

struct Point
{
	int x, y, id;

	bool operator < (const Point &o) const
	{
		return x < o.x;
	}
};
Point a[maxn];
void Rotate(int t, int x)
{
	int k = ch[t][x];
	ch[t][x] = ch[k][1-x];
	ch[k][1-x] = t;
	if (p[t]) ch[p[t]][t == ch[p[t]][1]] = k;
	p[k] = p[t];
	p[t] = k;
	p[ch[t][x]] = t;
	opt[k] = opt[t];
	opt[t] = min(min(opt[ch[t][0]], opt[ch[t][1]]), val[t]);
}
void Splay(int t, int gnd)
{
	if (t == gnd) return;
	for ( ; ; ) {
		if (p[t] == gnd) {
			Rotate(p[t], t == ch[p[t]][1]);
			return;
		}
		int flag = p[p[t]] == gnd, k = p[t];
		if ((t == ch[p[t]][1]) ^ (k == ch[p[k]][1])) {
			Rotate(p[t], t == ch[p[t]][1]);
			Rotate(p[t], t == ch[p[t]][1]);
		}
		else {
			Rotate(p[k], k == ch[p[k]][1]);
			Rotate(p[t], t == ch[p[t]][1]);
		}
		if (flag) return;
	}
}
struct Tree
{
	int l, r, t;
	Tree *lc, *rc;

	void Insert(int x, int y)
	{
		int k;
		for (k = t; ch[k][x > key[k]]; k = ch[k][x > key[k]])
			opt[k] = min(opt[k], y);
		ch[k][x > key[k]] = ++tt;
		p[tt] = k; ch[tt][0] = ch[tt][1] = 0;
		key[tt] = x; val[tt] = opt[tt] = y;
		if (y != inf) ID[y].pb(tt);
		Splay(tt, ch[t][0]);
	}
	int Lower(int x)
	{
		int k;
		for (k = t; ; )
			if (key[k] >= x)
				k = ch[k][0];
			else {
				//assert(ch[0][0]+ch[0][1] == 0);
				int kk;
				for (kk = ch[k][1]; ch[kk][0]; kk = ch[kk][0]);
				if (kk == 0 || key[kk] >= x) break;
				k = ch[k][1];
			}
		return k;
	}
	int Upper(int x)
	{
		int k;
		for (k = t; ; )
			if (key[k] <= x)
				k = ch[k][1];
			else {
				//assert(ch[0][0]+ch[0][1] == 0);
				int kk;
				for (kk = ch[k][0]; ch[kk][1]; kk = ch[kk][1]);
				if (kk == 0 || key[kk] <= x) break;
				k = ch[k][0];
			}
		return k;
	}
	int Search(int yl, int yr)
	{
		int lt = Lower(yl), rt = Upper(yr);
		Splay(lt, ch[t][0]);
		Splay(rt, ch[ch[t][0]][1]);
		return opt[ch[ch[ch[t][0]][1]][0]];
	}
	void Init()
	{
		t = ++tt;
		opt[0] = inf;
		p[0] = ch[0][0] = ch[0][1] = 0;
		p[t] = 0; ch[t][0] = 0; ch[t][1] = 0; key[t] = inf*2; val[t] = opt[t] = inf;
		Insert(-inf, inf);
		Insert(inf, inf);
		for (int i = l; i < r; i++) Insert(a[i].y, a[i].id);
	}
	Tree(int l_, int r_) : l(l_), r(r_)
	{
		Init();
		lc = l+1 < r ? new Tree(l, (l+r) >> 1) : NULL;
		rc = l+1 < r ? new Tree((l+r) >> 1, r) : NULL;
	}
	int Query(int xl, int xr, int yl, int yr)
	{
		// l..r-1
		// a[l].x, a[r-1].x
		if (xr < a[l].x || xl > a[r-1].x) return inf;
		if (xl <= a[l].x && xr >= a[r-1].x) return Search(yl, yr);
		int res = inf, m = (l+r) >> 1;
		if (xl <= a[m-1].x) res = min(res, lc->Query(xl, xr, yl, yr));
		if (xr >= a[m].x) res = min(res, rc->Query(xl, xr, yl, yr));
		return res;
	}
};
int Prev(int x)
{
	int k;
	if (ch[x][0]) {
		for (k = ch[x][0]; ch[k][1]; k = ch[k][1]);
	}
	else {
		for (k = x; k == ch[p[k]][0]; k = p[k]);
		k = p[k];
	}
	return k;
}
int Succ(int x)
{
	int k;
	if (ch[x][1]) {
		for (k = ch[x][1]; ch[k][0]; k = ch[k][0]);
	}
	else {
		for (k = x; k == ch[p[k]][1]; k = p[k]);
		k = p[k];
	}
	return k;
}
void Fresh(int t)
{
	opt[t] = min(min(opt[ch[t][0]], opt[ch[t][1]]), val[t]);
}
void Delete(int k)
{
	int t, pre = Prev(k), suc = Succ(k);
	for (t = k; p[t]; t = p[t]);
	Splay(pre, ch[t][0]);
	Splay(suc, ch[ch[t][0]][1]);
	ch[ch[ch[t][0]][1]][0] = 0;
	Fresh(ch[ch[t][0]][1]);
	Fresh(ch[t][0]);
	Fresh(t);
	assert(ch[k][0]+ch[k][1] == 0);
}
Tree *root;
struct Quest
{
	int id, xl, xr, yl, yr, z;

	bool operator < (const Quest &o) const
	{
		return z < o.z;
	}
};
Quest q[maxn];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m;
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%d%d%d%d%d", &q[i].xl, &q[i].xr, &q[i].yl, &q[i].yr, &q[i].z);
		q[i].id = i;
	}
	sort(q, q+m);
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &a[i].x, &a[i].y);
		a[i].id = i;
	}
	sort(a, a+n);
	tt = 0;
	root = new Tree(0, n);
	memset(res, 0, sizeof(res));
	for (int i = 0; i < m; i++) {
		int cnt = root->Query(q[i].xl, q[i].xr, q[i].yl, q[i].yr);
		if (cnt != inf) {
			res[cnt] = q[i].id+1;
			// cnt = id
			foreach(e, ID[cnt]) Delete(*e);
		}
	}
	for (int i = 0; i < n; i++)
		cout << res[i] << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 100010;
const int maxt = 2000000;
const int inf = 1000000000;

int tt;
int b[maxn], res[maxn];
int p[maxt], key[maxt], opt[maxt], val[maxt], ch[maxt][2];
vector<int> ID[maxn];

struct Point
{
	int x, y, id;

	bool operator < (const Point &o) const
	{
		return x < o.x;
	}
};
Point a[maxn];
void Rotate(int t, int x)
{
	int k = ch[t][x];
	ch[t][x] = ch[k][1-x];
	ch[k][1-x] = t;
	if (p[t]) ch[p[t]][t == ch[p[t]][1]] = k;
	p[k] = p[t];
	p[t] = k;
	p[ch[t][x]] = t;
	opt[k] = opt[t];
	opt[t] = min(min(opt[ch[t][0]], opt[ch[t][1]]), val[t]);
}
void Splay(int t, int gnd)
{
	if (t == gnd) return;
	for ( ; ; ) {
		if (p[t] == gnd) {
			Rotate(p[t], t == ch[p[t]][1]);
			return;
		}
		int flag = p[p[t]] == gnd, k = p[t];
		if ((t == ch[p[t]][1]) ^ (k == ch[p[k]][1])) {
			Rotate(p[t], t == ch[p[t]][1]);
			Rotate(p[t], t == ch[p[t]][1]);
		}
		else {
			Rotate(p[k], k == ch[p[k]][1]);
			Rotate(p[t], t == ch[p[t]][1]);
		}
		if (flag) return;
	}
}
struct Tree
{
	int l, r, t;
	Tree *lc, *rc;

	void Insert(int x, int y)
	{
		int k;
		for (k = t; ch[k][x > key[k]]; k = ch[k][x > key[k]])
			opt[k] = min(opt[k], y);
		ch[k][x > key[k]] = ++tt;
		p[tt] = k; ch[tt][0] = ch[tt][1] = 0;
		key[tt] = x; val[tt] = opt[tt] = y;
		if (y != inf) ID[y].pb(tt);
		Splay(tt, ch[t][0]);
	}
	int Lower(int x)
	{
		int k;
		for (k = t; ; )
			if (key[k] >= x)
				k = ch[k][0];
			else {
				int kk;
				for (kk = ch[k][1]; ch[kk][0]; kk = ch[kk][0]);
				if (kk == 0 || key[kk] >= x) break;
				k = ch[k][1];
			}
		return k;
	}
	int Upper(int x)
	{
		int k;
		for (k = t; ; )
			if (key[k] <= x)
				k = ch[k][1];
			else {
				int kk;
				for (kk = ch[k][0]; ch[kk][1]; kk = ch[kk][1]);
				if (kk == 0 || key[kk] <= x) break;
				k = ch[k][0];
			}
		return k;
	}
	int Search(int yl, int yr)
	{
		int lt = Lower(yl), rt = Upper(yr);
		Splay(lt, ch[t][0]);
		Splay(rt, ch[ch[t][0]][1]);
		return opt[ch[ch[ch[t][0]][1]][0]];
	}
	void Init()
	{
		t = ++tt;
		opt[0] = inf;
		p[0] = ch[0][0] = ch[0][1] = 0;
		p[t] = 0; ch[t][0] = 0; ch[t][1] = 0; key[t] = inf*2; val[t] = opt[t] = inf;
		Insert(-inf, inf);
		Insert(inf, inf);
		for (int i = l; i < r; i++) Insert(a[i].y, a[i].id);
	}
	Tree(int l_, int r_) : l(l_), r(r_)
	{
		Init();
		lc = l+1 < r ? new Tree(l, (l+r) >> 1) : NULL;
		rc = l+1 < r ? new Tree((l+r) >> 1, r) : NULL;
	}
	int Query(int xl, int xr, int yl, int yr)
	{
		// l..r-1
		// a[l].x, a[r-1].x
		if (xr < a[l].x || xl > a[r-1].x) return inf;
		if (xl <= a[l].x && xr >= a[r-1].x) return Search(yl, yr);
		int res = inf, m = (l+r) >> 1;
		if (xl <= a[m].x) res = min(res, lc->Query(xl, xr, yl, yr));
		if (xr >= a[m+1].x) res = min(res, rc->Query(xl, xr, yl, yr));
		return res;
	}
};
int Prev(int x)
{
	int k;
	if (ch[x][0]) {
		for (k = ch[x][0]; ch[k][1]; k = ch[k][1]);
	}
	else {
		for (k = x; k == ch[p[k]][0]; k = p[k]);
		k = p[k];
	}
	return k;
}
int Succ(int x)
{
	int k;
	if (ch[x][1]) {
		for (k = ch[x][1]; ch[k][0]; k = ch[k][0]);
	}
	else {
		for (k = x; k == ch[p[k]][1]; k = p[k]);
		k = p[k];
	}
	return k;
}
void Fresh(int t)
{
	opt[t] = min(min(opt[ch[t][0]], opt[ch[t][1]]), val[t]);
}
void Delete(int k)
{
	int t, pre = Prev(k), suc = Succ(k);
	for (t = k; p[t]; t = p[t]);
	Splay(pre, ch[t][0]);
	Splay(suc, ch[ch[t][0]][1]);
	ch[ch[ch[t][0]][1]][0] = 0;
	Fresh(ch[ch[t][0]][1]);
	Fresh(ch[t][0]);
	Fresh(t);
}
Tree *root;
struct Quest
{
	int id, xl, xr, yl, yr, z;

	bool operator < (const Quest &o) const
	{
		return z < o.z;
	}
};
Quest q[maxn];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> q[i].xl >> q[i].xr >> q[i].yl >> q[i].yr >> q[i].z;
		q[i].id = i;
	}
	sort(q, q+m);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i].x >> a[i].y;
		a[i].id = i;
	}
	sort(a, a+n);
	for (int i = 0; i < n; i++) b[a[i].id] = i;
	tt = 0;
	root = new Tree(0, n);
	memset(res, 0, sizeof(res));
	for (int i = 0; i < m; i++) {
		int cnt = root->Query(q[i].xl, q[i].xr, q[i].yl, q[i].yr);
		if (cnt != inf) {
			res[cnt] = q[i].id+1;
			// cnt = id
			foreach(e, ID[cnt]) Delete(*e);
		}
	}
	for (int i = 0; i < n; i++)
		cout << res[i] << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 100010;
const int maxt = 2000000;
const int inf = 1000000000;

int tt;
int res[maxn];
int p[maxt], key[maxt], opt[maxt], val[maxt], ch[maxt][2];
vector<int> ID[maxn];

struct Point
{
	int x, y, id;

	bool operator < (const Point &o) const
	{
		return x < o.x;
	}
};
Point a[maxn];
void Rotate(int t, int x)
{
	int k = ch[t][x];
	ch[t][x] = ch[k][1-x];
	ch[k][1-x] = t;
	if (p[t]) ch[p[t]][t == ch[p[t]][1]] = k;
	p[k] = p[t];
	p[t] = k;
	p[ch[t][x]] = t;
	opt[k] = opt[t];
	opt[t] = min(min(opt[ch[t][0]], opt[ch[t][1]]), val[t]);
}
void Splay(int t, int gnd)
{
	if (t == gnd) return;
	for ( ; ; ) {
		if (p[t] == gnd) {
			Rotate(p[t], t == ch[p[t]][1]);
			return;
		}
		int flag = p[p[t]] == gnd, k = p[t];
		if ((t == ch[p[t]][1]) ^ (k == ch[p[k]][1])) {
			Rotate(p[t], t == ch[p[t]][1]);
			Rotate(p[t], t == ch[p[t]][1]);
		}
		else {
			Rotate(p[k], k == ch[p[k]][1]);
			Rotate(p[t], t == ch[p[t]][1]);
		}
		if (flag) return;
	}
}
struct Tree
{
	int l, r, t;
	Tree *lc, *rc;

	void Insert(int x, int y)
	{
		int k;
		for (k = t; ch[k][x > key[k]]; k = ch[k][x > key[k]])
			opt[k] = min(opt[k], y);
		ch[k][x > key[k]] = ++tt;
		p[tt] = k; ch[tt][0] = ch[tt][1] = 0;
		key[tt] = x; val[tt] = opt[tt] = y;
		if (y != inf) ID[y].pb(tt);
		Splay(tt, ch[t][0]);
	}
	int Lower(int x)
	{
		int k;
		for (k = t; ; )
			if (key[k] >= x)
				k = ch[k][0];
			else {
				//assert(ch[0][0]+ch[0][1] == 0);
				int kk;
				for (kk = ch[k][1]; ch[kk][0]; kk = ch[kk][0]);
				if (kk == 0 || key[kk] >= x) break;
				k = ch[k][1];
			}
		return k;
	}
	int Upper(int x)
	{
		int k;
		for (k = t; ; )
			if (key[k] <= x)
				k = ch[k][1];
			else {
				//assert(ch[0][0]+ch[0][1] == 0);
				int kk;
				for (kk = ch[k][0]; ch[kk][1]; kk = ch[kk][1]);
				if (kk == 0 || key[kk] <= x) break;
				k = ch[k][0];
			}
		return k;
	}
	int Search(int yl, int yr)
	{
		int lt = Lower(yl), rt = Upper(yr);
		Splay(lt, ch[t][0]);
		Splay(rt, ch[ch[t][0]][1]);
		return opt[ch[ch[ch[t][0]][1]][0]];
	}
	void Init()
	{
		t = ++tt;
		opt[0] = inf;
		p[0] = ch[0][0] = ch[0][1] = 0;
		p[t] = 0; ch[t][0] = 0; ch[t][1] = 0; key[t] = inf*2; val[t] = opt[t] = inf;
		Insert(-inf, inf);
		Insert(inf, inf);
		for (int i = l; i < r; i++) Insert(a[i].y, a[i].id);
	}
	Tree(int l_, int r_) : l(l_), r(r_)
	{
		Init();
		lc = l+1 < r ? new Tree(l, (l+r) >> 1) : NULL;
		rc = l+1 < r ? new Tree((l+r) >> 1, r) : NULL;
	}
	int Query(int xl, int xr, int yl, int yr)
	{
		// l..r-1
		// a[l].x, a[r-1].x
		if (xr < a[l].x || xl > a[r-1].x) return inf;
		if (xl <= a[l].x && xr >= a[r-1].x) return Search(yl, yr);
		int res = inf, m = (l+r) >> 1;
		if (xl <= a[m-1].x) res = min(res, lc->Query(xl, xr, yl, yr));
		if (xr >= a[m].x) res = min(res, rc->Query(xl, xr, yl, yr));
		return res;
	}
};
int Prev(int x)
{
	int k;
	if (ch[x][0]) {
		for (k = ch[x][0]; ch[k][1]; k = ch[k][1]);
	}
	else {
		for (k = x; k == ch[p[k]][0]; k = p[k]);
		k = p[k];
	}
	return k;
}
int Succ(int x)
{
	int k;
	if (ch[x][1]) {
		for (k = ch[x][1]; ch[k][0]; k = ch[k][0]);
	}
	else {
		for (k = x; k == ch[p[k]][1]; k = p[k]);
		k = p[k];
	}
	return k;
}
void Fresh(int t)
{
	opt[t] = min(min(opt[ch[t][0]], opt[ch[t][1]]), val[t]);
}
void Delete(int k)
{
	int t, pre = Prev(k), suc = Succ(k);
	for (t = k; p[t]; t = p[t]);
	Splay(pre, ch[t][0]);
	Splay(suc, ch[ch[t][0]][1]);
	ch[ch[ch[t][0]][1]][0] = 0;
	Fresh(ch[ch[t][0]][1]);
	Fresh(ch[t][0]);
	Fresh(t);
	assert(ch[k][0]+ch[k][1] == 0);
}
Tree *root;
struct Quest
{
	int id, xl, xr, yl, yr, z;

	bool operator < (const Quest &o) const
	{
		return z < o.z;
	}
};
Quest q[maxn];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> q[i].xl >> q[i].xr >> q[i].yl >> q[i].yr >> q[i].z;
		q[i].id = i;
	}
	sort(q, q+m);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i].x >> a[i].y;
		a[i].id = i;
	}
	sort(a, a+n);
	tt = 0;
	root = new Tree(0, n);
	memset(res, 0, sizeof(res));
	for (int i = 0; i < m; i++) {
		int cnt = root->Query(q[i].xl, q[i].xr, q[i].yl, q[i].yr);
		if (cnt != inf) {
			res[cnt] = q[i].id+1;
			// cnt = id
			foreach(e, ID[cnt]) Delete(*e);
		}
	}
	for (int i = 0; i < n; i++)
		cout << res[i] << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 100010;
const int inf = 1000000000;

int x[maxn], y[maxn], vx[maxn], vy[maxn], res[maxn];

struct Point
{
	int x, y, id;

	bool operator < (const Point &o) const
	{
		return x < o.x;
	}
};
Point ps[maxn];
struct Rec
{
	int xl, xr, yl, yr, id, z;

	bool operator < (const Rec &o) const
	{
		return z < o.z;
	}
};
Rec q[maxn];
void disc(int *x, int *vx, int n)
{
	static PII ps[maxn];
	for (int i = 0; i < n; i++)
		ps[i] = mp(x[i], i);
	sort(ps, ps+n);
	for (int i = 0; i < n; i++) {
		vx[i] = ps[i].first;
		x[ps[i].second] = i;
	}
}
struct SegmentTree
{
	int pw, *a;

	void Init(int n)
	{
		for (pw = 1; pw <= n+3; pw *= 2);
		a = new int[pw*2];
		for (int i = 0; i < pw*2; i++)
			a[i] = inf;
	}
	void Change(int pos, int x)
	{
		int cnt = pw+pos+1;
		a[cnt] = x;
		for (int k = cnt >> 1; k; k >>= 1)
			a[k] = min(a[k << 1], a[(k << 1)+1]);
	}
	int Ask(int l, int r) // [l, r), l..r-1
	{
		int res = inf;
		for (int x = pw+l, y = pw+r+1; x ^ y ^ 1; x >>= 1, y >>= 1) {
			if (~x & 1) res = min(res, a[x ^ 1]);
			if (y & 1) res = min(res, a[y ^ 1]);
		}
		return res;
	}
};
struct Tree
{
	int l, r;
	Tree *lc, *rc;
	SegmentTree t;
	vector<int> dy;

	Tree(int l_, int r_) : l(l_), r(r_)
	{
		for (int i = l; i < r; i++)
			dy.pb(ps[i].y);
		sort(dy.begin(), dy.end());
		t.Init(r-l);
		for (int i = l; i < r; i++) {
			int ret = lower_bound(dy.begin(), dy.end(), ps[i].y)-dy.begin();
			t.Change(ret, ps[i].id);
		}
		lc = l+1 < r ? new Tree(l, (l+r) >> 1) : NULL;
		rc = l+1 < r ? new Tree((l+r) >> 1, r) : NULL;
	}
	void Modify(int whi)
	{
		t.Change(lower_bound(dy.begin(), dy.end(), y[whi])-dy.begin(), inf);
		if (l+1 < r) {
			if (x[whi] < (l+r) >> 1)
				lc->Modify(whi);
			else
				rc->Modify(whi);
		}
	}
	int Query(int xl, int xr, int yl, int yr)
	{
		if (xl <= l && xr >= r) {
			int ly = lower_bound(dy.begin(), dy.end(), yl)-dy.begin();
			int ry = lower_bound(dy.begin(), dy.end(), yr)-dy.begin();
			return t.Ask(ly, ry);
		}
		int res = inf;
		if (xl < (l+r) >> 1)
			res = min(res, lc->Query(xl, xr, yl, yr));
		if (xr > (l+r) >> 1)
			res = min(res, rc->Query(xl, xr, yl, yr));
		return res;
	}
};
Tree *root;
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> q[i].xl >> q[i].xr >> q[i].yl >> q[i].yr >> q[i].z;
		q[i].id = i;
	}
	sort(q, q+n);
	cin >> m;
	for (int i = 0; i < m; i++)
		cin >> x[i] >> y[i];
	disc(x, vx, m);
	disc(y, vy, m);
	for (int i = 0; i < m; i++) {
		ps[i].x = x[i];
		ps[i].y = y[i];
		ps[i].id = i;
	}
	sort(ps, ps+m);
	root = new Tree(0, m);
	memset(res, 0, sizeof(res));
	for (int i = 0; i < n; i++) {
		int lx = lower_bound(vx, vx+m, q[i].xl)-vx;
		int rx = upper_bound(vx, vx+m, q[i].xr)-vx;
		int ly = lower_bound(vy, vy+m, q[i].yl)-vy;
		int ry = upper_bound(vy, vy+m, q[i].yr)-vy;
		int ret = inf;
		if (lx < rx && ly < ry)
			ret = root->Query(lx, rx, ly, ry);
		if (ret != inf) {
			res[ret] = q[i].id+1;
			root->Modify(ret);
		}
	}
	for (int i = 0; i < m; i++)
		cout << res[i] << endl;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <sstream>
#include <bitset>
#include <numeric>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;

#define foreach(e, x) for (__typeof((x).begin()) e = (x).begin(); e != (x).end(); ++e)

typedef long long int64;

const int INF = int(1e9);
const int MAX_N = int(1e5) + 100;
const int MAX_M = 30 * int(1e5);

int qs[MAX_N][2];
int lc[MAX_M], rc[MAX_M], key[MAX_M], aux[MAX_M], minimal[MAX_M], tot;
int lower[MAX_M], upper[MAX_M];

inline void update(int k) {
	minimal[k] = min(key[k], min(minimal[lc[k]], minimal[rc[k]]));
	if (lc[k]) {
		lower[k] = lower[lc[k]];
	} else {
		lower[k] = qs[key[k]][1];
	}
	if (rc[k]) {
		upper[k] = upper[rc[k]];
	} else {
		upper[k] = qs[key[k]][1];
	}
}

inline void l_rotate(int &x) {
	int y = rc[x];
	rc[x] = lc[y];
	lc[y] = x;
	update(x);
	x = y;
}

inline void r_rotate(int &x) {
	int y = lc[x];
	lc[x] = rc[y];
	rc[y] = x;
	update(x);
	x = y;
}

void insert(int &k, int x) {
	if (!k) {
		k = ++tot;
		key[k] = x;
		aux[k] = rand();
		minimal[k] = x;
		lower[k] = upper[k] = qs[x][1];
		return;
	}
	if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
		insert(lc[k], x);
		if (aux[lc[k]] < aux[k]) {
			r_rotate(k);
		}
	} else {
		insert(rc[k], x);
		if (aux[rc[k]] < aux[k]) {
			l_rotate(k);
		}
	}
	update(k);
}

void erase(int &k, int x) {
	if (key[k] == x) {
		if (lc[k] == 0 && rc[k] == 0) {
			k = 0;
			return;
		}
		if (aux[lc[k]] < aux[rc[k]]) {
			int tk = k;
			r_rotate(k);
			erase(rc[k], x);
		} else {
			l_rotate(k);
			erase(lc[k], x);
		}
	} else {
		if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
			erase(lc[k], x);
		} else {
			erase(rc[k], x);
		}
	}
	update(k);
}

int find_minimal(int k, int l, int r) {
	if (k == 0 || l > upper[k] || r < lower[k]) {
		return INF;
	}
	if (l <= lower[k] && upper[k] <= r) {
		return minimal[k];
	}
	int ret = INF;
	if (l <= qs[key[k]][1] && qs[key[k]][1] <= r) {
		ret = min(ret, key[k]);
	}
	if (l <= upper[lc[k]]) {
		ret = min(ret, find_minimal(lc[k], l, r));
	}
	if (r >= lower[rc[k]]) {
		ret = min(ret, find_minimal(rc[k], l, r));
	}
	return ret;
}

int tree[MAX_N * 4];

void modify(int k, int s, int t, int p, int x, bool v) {
	if (v) {
		insert(tree[k], x);
	} else {
		erase(tree[k], x);
	}
	if (s == t) {
		return;
	}
	int mid = s + t >> 1;
	if (p <= mid) {
		modify(k << 1, s, mid, p, x, v);
	} else {
		modify(k << 1 | 1, mid + 1, t, p, x, v);
	}
}

int query(int k, int s, int t, int l, int r, int d, int u) {
	if (l > t || r < s) {
		return INF;
	}
	if (l <= s && t <= r) {
		return find_minimal(tree[k], d, u);
	}
	int mid = s + t >> 1, ret = INF;
	if (l <= mid) {
		ret = min(ret, query(k << 1, s, mid, l, r, d, u));
	}
	if (mid < r) {
		ret = min(ret, query(k << 1 | 1, mid + 1, t, l, r, d, u));
	}
	return ret;
}

int n, m;
int mat[MAX_N][5], ans[MAX_N];
int order[MAX_N];
vector <int> all_x;

bool cmp(int i, int j) {
	return mat[i][4] < mat[j][4];
}

int main() {
	//srand(time(0));
	minimal[0] = INF;
	aux[0] = INT_MAX;
	upper[0] = -INF, lower[0] = INF;

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		order[i] = i;
		scanf("%d%d%d%d%d", mat[i], mat[i] + 2, mat[i] + 1, mat[i] + 3, mat[i] + 4);
		//all_x.push_back(mat[i][0]);
		//all_x.push_back(mat[i][2]);
	}
	sort(order, order + n, cmp);
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%d%d", qs[i], qs[i] + 1);
		all_x.push_back(qs[i][0]);
	}
	sort(all_x.begin(), all_x.end());
	all_x.resize(unique(all_x.begin(), all_x.end()) - all_x.begin());
	int l = all_x.size();
	for (int i = 0; i < m; i++) {
		int p = lower_bound(all_x.begin(), all_x.end(), qs[i][0]) - all_x.begin() + 1;
		modify(1, 1, l, p, i, true);
	}
	for (int t = 0; t < n; t++) {
		int i = order[t];
		int tl = lower_bound(all_x.begin(), all_x.end(), mat[i][0]) - all_x.begin() + 1;
		int tr = upper_bound(all_x.begin(), all_x.end(), mat[i][2]) - all_x.begin();
		int p = query(1, 1, l, tl, tr, mat[i][1], mat[i][3]);
		if (p < INF) {
			ans[p] = i + 1;
			int q = lower_bound(all_x.begin(), all_x.end(), qs[p][0]) - all_x.begin() + 1;
			modify(1, 1, l, q, p, false);
		}
	}
	for (int i = 0; i < m; i++) {
		printf("%d\n", ans[i]);
	}
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <sstream>
#include <bitset>
#include <numeric>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;

#define foreach(e, x) for (__typeof((x).begin()) e = (x).begin(); e != (x).end(); ++e)

typedef long long int64;

const int INF = int(1e9);
const int MAX_N = int(1e5) + 100;
const int MAX_M = 30 * int(1e5);

int qs[MAX_N][2];
int lc[MAX_M], rc[MAX_M], key[MAX_M], aux[MAX_M], minimal[MAX_M], tot;
int lower[MAX_M], upper[MAX_M];

inline void update(int k) {
	minimal[k] = min(key[k], min(minimal[lc[k]], minimal[rc[k]]));
	if (lc[k]) {
		lower[k] = lower[lc[k]];
	} else {
		lower[k] = qs[key[k]][1];
	}
	if (rc[k]) {
		upper[k] = upper[rc[k]];
	} else {
		upper[k] = qs[key[k]][1];
	}
}

inline void l_rotate(int &x) {
	int y = rc[x];
	rc[x] = lc[y];
	lc[y] = x;
	update(x);
	x = y;
}

inline void r_rotate(int &x) {
	int y = lc[x];
	lc[x] = rc[y];
	rc[y] = x;
	update(x);
	x = y;
}

void insert(int &k, int x) {
	if (!k) {
		k = ++tot;
		key[k] = x;
		aux[k] = rand();
		minimal[k] = x;
		lower[k] = upper[k] = qs[x][1];
		return;
	}
	if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
		insert(lc[k], x);
		if (aux[lc[k]] < aux[k]) {
			r_rotate(k);
		}
	} else {
		insert(rc[k], x);
		if (aux[rc[k]] < aux[k]) {
			l_rotate(k);
		}
	}
	update(k);
}

void erase(int &k, int x) {
	if (key[k] == x) {
		if (lc[k] == 0 && rc[k] == 0) {
			k = 0;
			return;
		}
		if (aux[lc[k]] < aux[rc[k]]) {
			int tk = k;
			r_rotate(k);
			erase(rc[k], x);
		} else {
			l_rotate(k);
			erase(lc[k], x);
		}
	} else {
		if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
			erase(lc[k], x);
		} else {
			erase(rc[k], x);
		}
	}
	update(k);
}

int find_minimal(int k, int l, int r) {
	if (k == 0 || l > upper[k] || r < lower[k]) {
		return INF;
	}
	if (l <= lower[k] && upper[k] <= r) {
		return minimal[k];
	}
	int ret = INF;
	if (l <= qs[key[k]][1] && qs[key[k]][1] <= r) {
		ret = min(ret, key[k]);
	}
	if (l <= upper[lc[k]]) {
		ret = min(ret, find_minimal(lc[k], l, r));
	}
	if (r >= lower[rc[k]]) {
		ret = min(ret, find_minimal(rc[k], l, r));
	}
	return ret;
}

int tree[MAX_N * 4];

void modify(int k, int s, int t, int p, int x, bool v) {
	if (v) {
		insert(tree[k], x);
	} else {
		erase(tree[k], x);
	}
	if (s == t) {
		return;
	}
	int mid = s + t >> 1;
	if (p <= mid) {
		modify(k << 1, s, mid, p, x, v);
	} else {
		modify(k << 1 | 1, mid + 1, t, p, x, v);
	}
}

int query(int k, int s, int t, int l, int r, int d, int u) {
	if (l > t || r < s) {
		return INF;
	}
	if (l <= s && t <= r) {
		return find_minimal(tree[k], d, u);
	}
	int mid = s + t >> 1, ret = INF;
	if (l <= mid) {
		ret = min(ret, query(k << 1, s, mid, l, r, d, u));
	}
	if (mid < r) {
		ret = min(ret, query(k << 1 | 1, mid + 1, t, l, r, d, u));
	}
	return ret;
}

int n, m;
int mat[MAX_N][5], ans[MAX_N];
int order[MAX_N];
vector <int> all_x;

bool cmp(int i, int j) {
	return mat[i][4] < mat[j][4];
}

int main() {
	srand(time(0));
	minimal[0] = INF;
	aux[0] = INT_MAX;
	upper[0] = -INF, lower[0] = INF;

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		order[i] = i;
		scanf("%d%d%d%d%d", mat[i], mat[i] + 2, mat[i] + 1, mat[i] + 3, mat[i] + 4);
		//all_x.push_back(mat[i][0]);
		//all_x.push_back(mat[i][2]);
	}
	sort(order, order + n, cmp);
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%d%d", qs[i], qs[i] + 1);
		all_x.push_back(qs[i][0]);
	}
	sort(all_x.begin(), all_x.end());
	all_x.resize(unique(all_x.begin(), all_x.end()) - all_x.begin());
	int l = all_x.size();
	for (int i = 0; i < m; i++) {
		int p = lower_bound(all_x.begin(), all_x.end(), qs[i][0]) - all_x.begin() + 1;
		modify(1, 1, l, p, i, true);
	}
	for (int t = 0; t < n; t++) {
		int i = order[t];
		int tl = lower_bound(all_x.begin(), all_x.end(), mat[i][0]) - all_x.begin() + 1;
		int tr = upper_bound(all_x.begin(), all_x.end(), mat[i][2]) - all_x.begin();
		int p = query(1, 1, l, tl, tr, mat[i][1], mat[i][3]);
		if (p < INF) {
			ans[p] = i + 1;
			int q = lower_bound(all_x.begin(), all_x.end(), qs[p][0]) - all_x.begin() + 1;
			modify(1, 1, l, q, p, false);
		}
	}
	for (int i = 0; i < m; i++) {
		printf("%d\n", ans[i]);
	}
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <sstream>
#include <bitset>
#include <numeric>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;

#define foreach(e, x) for (__typeof((x).begin()) e = (x).begin(); e != (x).end(); ++e)

typedef long long int64;

typedef pair <int, int> PII;

const int INF = int(1e9);
const int MAX_N = 4 * int(1e5) + 100;
const int MAX_M = 20 * int(1e5);

int qs[MAX_N][2];
int lc[MAX_M], rc[MAX_M], key[MAX_M], aux[MAX_M], minimal[MAX_M] = {INF}, tot;
int lower[MAX_M] = {INF}, upper[MAX_M] = {-INF};

inline void update(int k) {
    minimal[k] = min(key[k], min(minimal[lc[k]], minimal[rc[k]]));
    lower[k] = min(qs[key[k]][1], min(lower[lc[k]], lower[rc[k]]));
    upper[k] = max(qs[key[k]][1], max(upper[lc[k]], upper[rc[k]]));
}

void l_rotate(int &x) {
    int y = rc[x];
    rc[x] = lc[y];
    lc[y] = x;
    update(x);
    x = y;
}

void r_rotate(int &x) {
    int y = lc[x];
    lc[x] = rc[y];
    rc[y] = x;
    update(x);
    x = y;
}

void insert(int &k, int x) {
    if (!k) {
        k = ++tot;
        key[k] = x;
        aux[k] = rand();
        minimal[k] = x;
        lower[k] = upper[k] = qs[x][1];
        return;
    }
    if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
        insert(lc[k], x);
        if (aux[lc[k]] < aux[k]) {
            r_rotate(k);
        }
    } else {
        insert(rc[k], x);
        if (aux[rc[k]] < aux[k]) {
            l_rotate(k);
        }
    }
    update(k);
}

int erase(int &k, int x) {
    if (key[k] == x || x == INF && rc[k] == 0) {
        if (!lc[k] || !rc[k]) {
            int ret = key[k];
            k = lc[k] + rc[k];
            return ret;
        }
        key[k] = erase(lc[k], INF);
        update(k);
        return key[k];
    }
    int ret = erase(qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k] ? lc[k] : rc[k], x);
    update(k);
    return ret;
}

int find_minimal(int k, int l, int r) {
    if (k == 0) {
        return INF;
    }
    if (l <= lower[k] && upper[k] <= r) {
        return minimal[k];
    }
    int ret = INF;
    if (l <= qs[key[k]][1] && qs[key[k]][1] <= r) {
        ret = min(ret, key[k]);
    }
    if (l <= upper[lc[k]]) {
        ret = min(ret, find_minimal(lc[k], l, r));
    }
    if (r >= lower[rc[k]]) {
        ret = min(ret, find_minimal(rc[k], l, r));
    }
    return ret;
}

struct IntervalTree {
    int tree[MAX_N * 4];
    void make_use(int n) {
        fill(tree + 1, tree + 1 + (n * 4), 0);
    }
    void modify(int k, int s, int t, int p, int x, bool v) {
        if (v) {
            insert(tree[k], x);
        } else {
            erase(tree[k], x);
        }
        if (s == t) {
            return;
        }
        int mid = s + t >> 1;
        if (p <= mid) {
            modify(k << 1, s, mid, p, x, v);
        } else {
            modify(k << 1 | 1, mid + 1, t, p, x, v);
        }
    }
    int query(int k, int s, int t, int l, int r, int d, int u) {
        if (l <= s && t <= r) {
            return find_minimal(tree[k], d, u);
        }
        int mid = s + t >> 1, ret = INF;
        if (l <= mid) {
            ret = min(ret, query(k << 1, s, mid, l, r, d, u));
        }
        if (mid < r) {
            ret = min(ret, query(k << 1 | 1, mid + 1, t, l, r, d, u));
        }
        return ret;
    }
}T;

int n, m;
int mat[MAX_N][5], ans[MAX_N];
int order[MAX_N];
vector <int> all_x;

bool cmp(int i, int j) {
    return mat[i][4] < mat[j][4];
}

int main() {
    srand(time(0));
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        order[i] = i;
        scanf("%d%d%d%d%d", mat[i], mat[i] + 2, mat[i] + 1, mat[i] + 3, mat[i] + 4);
        all_x.push_back(mat[i][0]);
        all_x.push_back(mat[i][2]);
    }
    sort(order, order + n, cmp);
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", qs[i], qs[i] + 1);
        all_x.push_back(qs[i][0]);
    }
    sort(all_x.begin(), all_x.end());
    all_x.erase(unique(all_x.begin(), all_x.end()), all_x.end());
    int l = all_x.size();
    T.make_use(l);
    for (int i = 0; i < m; i++) {
        int p = lower_bound(all_x.begin(), all_x.end(), qs[i][0]) - all_x.begin() + 1;
        T.modify(1, 1, l, p, i, true);
    }
    for (int t = 0; t < n; t++) {
        int i = order[t];
        int tl = lower_bound(all_x.begin(), all_x.end(), mat[i][0]) - all_x.begin() + 1;
        int tr = lower_bound(all_x.begin(), all_x.end(), mat[i][2]) - all_x.begin() + 1;
        int p = T.query(1, 1, l, tl, tr, mat[i][1], mat[i][3]);
        if (p < INF) {
            ans[p] = i + 1;
            int q = lower_bound(all_x.begin(), all_x.end(), qs[p][0]) - all_x.begin() + 1;
            T.modify(1, 1, l, q, p, false);
        }
    }
    for (int i = 0; i < m; i++) {
        printf("%d\n", ans[i]);
    }
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <sstream>
#include <bitset>
#include <numeric>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;

#define foreach(e, x) for (__typeof((x).begin()) e = (x).begin(); e != (x).end(); ++e)

typedef long long int64;

const int INF = int(1e9);
const int MAX_N = 3 * int(1e5) + 100;
const int MAX_M = 40 * int(1e5);

int qs[MAX_N][2];
int lc[MAX_M], rc[MAX_M], key[MAX_M], aux[MAX_M], minimal[MAX_M], tot;
int lower[MAX_M], upper[MAX_M];

inline void update(int k) {
    minimal[k] = min(key[k], min(minimal[lc[k]], minimal[rc[k]]));
    if (lc[k]) {
        lower[k] = lower[lc[k]];
    } else {
        lower[k] = qs[key[k]][1];
    }
    if (rc[k]) {
        upper[k] = upper[rc[k]];
    } else {
        upper[k] = qs[key[k]][1];
    }
}

inline void l_rotate(int &x) {
    int y = rc[x];
    rc[x] = lc[y];
    lc[y] = x;
    update(x);
    x = y;
}

inline void r_rotate(int &x) {
    int y = lc[x];
    lc[x] = rc[y];
    rc[y] = x;
    update(x);
    x = y;
}

void insert(int &k, int x) {
    if (!k) {
        k = ++tot;
        key[k] = x;
        aux[k] = 1LL * rand() * rand() % 1111111;
        minimal[k] = x;
        lower[k] = upper[k] = qs[x][1];
        return;
    }
    if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
        insert(lc[k], x);
        if (aux[lc[k]] < aux[k]) {
            r_rotate(k);
        }
    } else {
        insert(rc[k], x);
        if (aux[rc[k]] < aux[k]) {
            l_rotate(k);
        }
    }
    update(k);
}

void erase(int &k, int x) {
    if (key[k] == x) {
        if (lc[k] == 0 && rc[k] == 0) {
            k = 0;
            return;
        }
        if (aux[lc[k]] < aux[rc[k]]) {
            int tk = k;
            r_rotate(k);
            erase(rc[k], x);
        } else {
            l_rotate(k);
            erase(lc[k], x);
        }
    } else {
        if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
            erase(lc[k], x);
        } else {
            erase(rc[k], x);
        }
    }
    update(k);
}

int find_minimal(int k, int l, int r) {
    if (k == 0 || l > upper[k] || r < lower[k]) {
        return INF;
    }
    if (l <= lower[k] && upper[k] <= r) {
        return minimal[k];
    }
    int ret = INF;
    if (l <= qs[key[k]][1] && qs[key[k]][1] <= r) {
        ret = min(ret, key[k]);
    }
    if (l <= upper[lc[k]]) {
        ret = min(ret, find_minimal(lc[k], l, r));
    }
    if (r >= lower[rc[k]]) {
        ret = min(ret, find_minimal(rc[k], l, r));
    }
    return ret;
}

int tree[MAX_N * 4];

void modify(int k, int s, int t, int p, int x, bool v) {
    if (v) {
        insert(tree[k], x);
    } else {
        erase(tree[k], x);
    }
    if (s == t) {
        return;
    }
    int mid = s + t >> 1;
    if (p <= mid) {
        modify(k << 1, s, mid, p, x, v);
    } else {
        modify(k << 1 | 1, mid + 1, t, p, x, v);
    }
}

int query(int k, int s, int t, int l, int r, int d, int u) {
    if (l <= s && t <= r) {
        return find_minimal(tree[k], d, u);
    }
    int mid = s + t >> 1, ret = INF;
    if (l <= mid) {
        ret = min(ret, query(k << 1, s, mid, l, r, d, u));
    }
    if (mid < r) {
        ret = min(ret, query(k << 1 | 1, mid + 1, t, l, r, d, u));
    }
    return ret;
}

int n, m;
int mat[MAX_N][5], ans[MAX_N];
int order[MAX_N];
vector <int> all_x;

bool cmp(int i, int j) {
    return mat[i][4] < mat[j][4];
}

int main() {
//  srand(time(0));
    minimal[0] = aux[0] = INF;
    upper[0] = -INF, lower[0] = INF;

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        order[i] = i;
        scanf("%d%d%d%d%d", mat[i], mat[i] + 2, mat[i] + 1, mat[i] + 3, mat[i] + 4);
        //all_x.push_back(mat[i][0]);
        //all_x.push_back(mat[i][2]);
    }
    sort(order, order + n, cmp);
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", qs[i], qs[i] + 1);
        all_x.push_back(qs[i][0]);
    }
    sort(all_x.begin(), all_x.end());
    all_x.resize(unique(all_x.begin(), all_x.end()) - all_x.begin());
    int l = all_x.size();
    for (int i = 0; i < m; i++) {
        int p = lower_bound(all_x.begin(), all_x.end(), qs[i][0]) - all_x.begin() + 1;
        modify(1, 1, l, p, i, true);
    }
    for (int t = 0; t < n; t++) {
        int i = order[t];
        int tl = lower_bound(all_x.begin(), all_x.end(), mat[i][0]) - all_x.begin() + 1;
        int tr = upper_bound(all_x.begin(), all_x.end(), mat[i][2]) - all_x.begin();
        int p = query(1, 1, l, tl, tr, mat[i][1], mat[i][3]);
        if (p < INF) {
            ans[p] = i + 1;
            int q = lower_bound(all_x.begin(), all_x.end(), qs[p][0]) - all_x.begin() + 1;
            modify(1, 1, l, q, p, false);
        }
    }
    for (int i = 0; i < m; i++) {
        printf("%d\n", ans[i]);
    }
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <sstream>
#include <bitset>
#include <numeric>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;

#define foreach(e, x) for (__typeof((x).begin()) e = (x).begin(); e != (x).end(); ++e)

typedef long long int64;

typedef pair <int, int> PII;

const int INF = int(1e9);
const int MAX_N = 4 * int(1e5) + 100;
const int MAX_M = 18 * int(1e5);

int qs[MAX_N][2];
int lc[MAX_M], rc[MAX_M], key[MAX_M], aux[MAX_M], minimal[MAX_M] = {INF}, tot;
int lower[MAX_M] = {INF}, upper[MAX_M] = {-INF};

inline void update(int k) {
    minimal[k] = min(key[k], min(minimal[lc[k]], minimal[rc[k]]));
    lower[k] = min(qs[key[k]][1], min(lower[lc[k]], lower[rc[k]]));
    upper[k] = max(qs[key[k]][1], max(upper[lc[k]], upper[rc[k]]));
}

void l_rotate(int &x) {
    int y = rc[x];
    rc[x] = lc[y];
    lc[y] = x;
    update(x);
    x = y;
}

void r_rotate(int &x) {
    int y = lc[x];
    lc[x] = rc[y];
    rc[y] = x;
    update(x);
    x = y;
}

void insert(int &k, int x) {
    if (!k) {
        k = ++tot;
        key[k] = x;
        aux[k] = rand();
        minimal[k] = x;
        lower[k] = upper[k] = qs[x][1];
        return;
    }
    if (qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k]) {
        insert(lc[k], x);
        if (aux[lc[k]] < aux[k]) {
            r_rotate(k);
        }
    } else {
        insert(rc[k], x);
        if (aux[rc[k]] < aux[k]) {
            l_rotate(k);
        }
    }
    update(k);
}

int erase(int &k, int x) {
    if (key[k] == x || x == INF && rc[k] == 0) {
        if (!lc[k] || !rc[k]) {
            int ret = key[k];
            k = lc[k] + rc[k];
            return ret;
        }
        key[k] = erase(lc[k], INF);
        update(k);
        return key[k];
    }
    int ret = erase(qs[x][1] < qs[key[k]][1] || qs[x][1] == qs[key[k]][1] && x < key[k] ? lc[k] : rc[k], x);
    update(k);
    return ret;
}

int find_minimal(int k, int l, int r) {
    if (k == 0) {
        return INF;
    }
    if (l <= lower[k] && upper[k] <= r) {
        return minimal[k];
    }
    int ret = INF;
    if (l <= qs[key[k]][1] && qs[key[k]][1] <= r) {
        ret = min(ret, key[k]);
    }
    if (l <= upper[lc[k]]) {
        ret = min(ret, find_minimal(lc[k], l, r));
    }
    if (r >= lower[rc[k]]) {
        ret = min(ret, find_minimal(rc[k], l, r));
    }
    return ret;
}

struct IntervalTree {
    int tree[MAX_N * 4];
    void make_use(int n) {
        fill(tree + 1, tree + 1 + (n * 4), 0);
    }
    void modify(int k, int s, int t, int p, int x, bool v) {
        if (v) {
            insert(tree[k], x);
        } else {
            erase(tree[k], x);
        }
        if (s == t) {
            return;
        }
        int mid = s + t >> 1;
        if (p <= mid) {
            modify(k << 1, s, mid, p, x, v);
        } else {
            modify(k << 1 | 1, mid + 1, t, p, x, v);
        }
    }
    int query(int k, int s, int t, int l, int r, int d, int u) {
        if (l <= s && t <= r) {
            return find_minimal(tree[k], d, u);
        }
        int mid = s + t >> 1, ret = INF;
        if (l <= mid) {
            ret = min(ret, query(k << 1, s, mid, l, r, d, u));
        }
        if (mid < r) {
            ret = min(ret, query(k << 1 | 1, mid + 1, t, l, r, d, u));
        }
        return ret;
    }
}T;

int n, m;
int mat[MAX_N][5], ans[MAX_N];
int order[MAX_N];
vector <int> all_x;

bool cmp(int i, int j) {
    return mat[i][4] < mat[j][4];
}

int main() {
    srand(time(0));
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        order[i] = i;
        scanf("%d%d%d%d%d", mat[i], mat[i] + 2, mat[i] + 1, mat[i] + 3, mat[i] + 4);
        all_x.push_back(mat[i][0]);
        all_x.push_back(mat[i][2]);
    }
    sort(order, order + n, cmp);
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", qs[i], qs[i] + 1);
        all_x.push_back(qs[i][0]);
    }
    sort(all_x.begin(), all_x.end());
    all_x.erase(unique(all_x.begin(), all_x.end()), all_x.end());
    int l = all_x.size();
    T.make_use(l);
    for (int i = 0; i < m; i++) {
        int p = lower_bound(all_x.begin(), all_x.end(), qs[i][0]) - all_x.begin() + 1;
        T.modify(1, 1, l, p, i, true);
    }
    for (int t = 0; t < n; t++) {
        int i = order[t];
        int tl = lower_bound(all_x.begin(), all_x.end(), mat[i][0]) - all_x.begin() + 1;
        int tr = lower_bound(all_x.begin(), all_x.end(), mat[i][2]) - all_x.begin() + 1;
        int p = T.query(1, 1, l, tl, tr, mat[i][1], mat[i][3]);
        if (p < INF) {
            ans[p] = i + 1;
            int q = lower_bound(all_x.begin(), all_x.end(), qs[p][0]) - all_x.begin() + 1;
            T.modify(1, 1, l, q, p, false);
        }
    }
    for (int i = 0; i < m; i++) {
        printf("%d\n", ans[i]);
    }
}