#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int testCases;
  cin >> testCases;
  int answers[testCases];

  for(int i = 0; i < testCases; i++) {
    int sticks, diamonds, ans;
    double constant = 1/3.0;
    cin >> sticks >> diamonds;

    if(sticks == 0 || diamonds == 0) {
      answers[i] = 0;
      continue;
    }

    // 2a + b = 1
    // 2b + a = 1
    ans = round(constant * sticks + constant * diamonds);
    answers[i] = ans;
  }

  for(int i = 0; i < testCases; i++) {
    cout << answers[i] << endl;
  }

  return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int testCases;
  cin >> testCases;
  int answers[testCases];

  for(int i = 0; i < testCases; i++) {
    int sticks, diamonds, ans;
    double constant = 1/3.0;
    cin >> sticks >> diamonds;

    if(sticks == 0 || diamonds == 0) {
      answers[i] = 0;
      continue;
    }

    // 2a + b = 1
    // 2b + a = 1
    ans = floor(constant * sticks + constant * diamonds);
    answers[i] = ans;
  }

  for(int i = 0; i < testCases; i++) {
    cout << answers[i] << endl;
  }

  return 0;
}


#include <iostream>
using namespace std;

int main() {
  int testCases;
  cin >> testCases;
  int answers[testCases];

  for(int i = 0; i < testCases; i++) {
    int sticks, diamonds, ans = 0;
    cin >> sticks >> diamonds;

    while(sticks != 0 && diamonds != 0) {
      if(sticks == diamonds && sticks > 0 && diamonds > 0) {
        if(sticks >= 2 && diamonds >= 1) {
          sticks -= 2;
          diamonds -= 1;
          if(sticks < 0 || diamonds < 0) {
            continue;
          }
          ans++;
        }
        else {
          sticks -= 1;
          diamonds -= 2;
          if(sticks < 0 || diamonds < 0) {
            continue;
          }
          ans++;
        }
        answers[i] = ans;
        continue;
      }

      if(sticks > diamonds) {
        sticks -= 2;
        diamonds -= 1;
        ans++;
      }
      else if(sticks == diamonds) {
        sticks -= 2;
        diamonds -= 1;
        ans++;
      }
      else {
        sticks -= 1;
        diamonds -= 2;
        ans++;
      }
    }

    answers[i] = ans;
  }

  for(int i = 0; i < testCases; i++) {
    cout << answers[i] << endl;
  }

  return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int testCases;
  cin >> testCases;
  int answers[testCases];

  for(int i = 0; i < testCases; i++) {
    int sticks, diamonds, ans;
    double constant = 1/3.0;
    cin >> sticks >> diamonds;

    if(sticks == diamonds) {
      answers[i] == sticks / 2;
      continue;
    }

    if(sticks == 0 || diamonds == 0) {
      answers[i] = 0;
      continue;
    }

    // 2a + b = 1
    // 2b + a = 1
    ans = round(constant * sticks + constant * diamonds);
    answers[i] = ans;
  }

  for(int i = 0; i < testCases; i++) {
    cout << answers[i] << endl;
  }

  return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int testCases, diamonds, sticks, emeralds = 0;
  cin >> testCases;

  while(testCases--) {
    cin >> sticks >> diamonds;

    // 2a + b = 1
    // a + 2b = 1
    // Factors is 1/3 for both A and B.

    if(sticks > 0 && diamonds > 0) {
      emeralds = (diamonds + sticks) / 3;
    }
    else {
      emeralds = 0;
    }

    cout << min(emeralds, min(diamonds, sticks)) << endl;
  }

  return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int testCases, diamonds, sticks, emeralds = 0;
  cin >> testCases;

  while(testCases--) {
    cin >> sticks >> diamonds;

    // 2a + b = 1
    // a + 2b = 1
    // Factors is 1/3 for both A and B.

    if(sticks > 0 && diamonds > 0) {
      emeralds = (diamonds + sticks) / 3;
    }
    else {
      emeralds = 0;
    }

    cout << emeralds << endl;
  }

  return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int testCases;
  cin >> testCases;
  int answers[testCases];

  for(int i = 0; i < testCases; i++) {
    int sticks, diamonds, ans;
    double constant = 1/3.0;
    cin >> sticks >> diamonds;

    if(sticks == diamonds) {
      answers[i] = sticks / 2;
      continue;
    }

    if(sticks == 0 || diamonds == 0) {
      answers[i] = 0;
      continue;
    }

    // 2a + b = 1
    // 2b + a = 1
    ans = round(constant * sticks + constant * diamonds);
    answers[i] = ans;
  }

  for(int i = 0; i < testCases; i++) {
    cout << answers[i] << endl;
  }

  return 0;
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=0;
        ans+=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        if(b==1){
            if(a>=2)ans++;
        }else if(b==2){
            if(a>=3)ans++;
        }
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=0;
        ans=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        if(a>=2 && b>=1)ans++;
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        if(a==b){
            if(a==2)ans++;
            cout<<ans<<"\n";
            continue;
        }
        ans+=1;
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t,a,b;cin>>t;
    while(t--){
        cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        cout<<(a+b)/3<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        if(a==b){
            if(a==2)ans++;
            cout<<ans<<"\n";
            continue;
        }
        ans+=(b!=0);
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=0;
        ans+=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        while(1){
            if(a<b)swap(a,b);
            if(a>=2 && b>=1)a-=2,b--,ans++;
            else break;
        }
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=0;
        ans+=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        if(b==1){
            if(a>=2)ans++;
        }else if(b==2){
            if(a>=2)ans++;
        }
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=0;
        if(a==b){
            ans+=(b/3)*2;
            ans+=(b%3==2);
        }else{
            ans+=(b/3)*2;
            if(b%3==1)ans++;
        }
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>b*2){
            cout<<b<<"\n";
            continue;
        }
        int ans=(b/3)*2;
        a-=b/3*3;
        b-=b/3*3;
        if(a>=2 && b>=1)ans++;
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>=2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans=(b/3)*2;
        a-=(b/3)*3;
        b-=(b/3)*3;
        if(a==b){
            if(a==2)ans++;
            cout<<ans<<"\n";
            continue;
        }
        ans+=b;
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Use __builtin_popcountll for ll;
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

signed main(){
    FASTER;
    int t;cin>>t;
    while(t--){
        int a,b;cin>>a>>b;
        if(a<b)swap(a,b);
        if(a>2*b){
            cout<<b<<"\n";
            continue;
        }
        int ans;
        ans=((b/3)*3)/3*2;
        a-=((b/3)*3);
        b-=((b/3)*3);
        if(a>=2 && b>=1)ans++;
        cout<<ans<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if(a!=0 && b!=0)
        cout<<(a+b)/3<<endl;
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x>y?x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {ll c,d;
        c=min(a,b);
        d=max(a,b);
        ll e=c/2;
        ll f,g;
        f=c-e;
        g=d-2*e;
        ll h=max(f,g);
        cout<<e+h/2<<endl;}
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {
            ll c,d;
            c=min(a,b);
            d=max(a,b);
            if (c!=d && (2*c)>d)
            {
                ll e=c/2;
                ll f,g;
                f=c-e;
                g=d-2*e;
                ll h=max(f,g);
                cout<<e+h/2<<endl;

            }
            else if ((2*c)<d)
                cout<<c<<endl;
            else cout<<c/2<<endl;
        }
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {
            ll c,d;
            c=min(a,b);
            d=max(a,b);
            if (d<(c*2))
            {
                ll e=c/2;
                ll f,g;
                f=c-e;
                g=d-2*e;
                ll h=max(f,g);
                cout<<e+h/2<<endl;

            }
            else
                cout<<c<<endl;
        }
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x <= y? x:y)
#define max(x,y) (x>=y?x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {ll c,d;
        c=min(a,b);
        d=max(a,b);
        if (d<c*2)
        {ll e=c/2;
        ll f,g;
        f=c-e;
        g=d-2*e;
        ll h=max(f,g);
        cout<<e+h/2<<endl;}
            else
            cout<<c<<endl;
        }
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {
            ll c,d;
            c=min(a,b);
            d=max(a,b);
            if ((c+c)>d)
            {
                ll e=c/2;
                ll f,g;
                f=c-e;
                g=d-2*e;
                ll h=max(f,g);
                cout<<e+h/2<<endl;

            }
            else
                cout<<c<<endl;
        }
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x>y?x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        ll c,d;
        c=min(a,b);
        d=max(a,b);
        ll e=c/2;
        ll f,g;
        f=c-e;
        g=d-2*e;
        ll h=max(f,g);
        cout<<e+h/2<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {
            ll c,d;
            c=min(a,b);
            d=max(a,b);
            if ((2*c)>d)
            {
                ll e=c/2;
                ll f,g;
                f=c-e;
                g=d-2*e;
                ll h=max(f,g);
                cout<<e+h/2<<endl;

            }
            else
                cout<<c<<endl;
        }
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if (a!=0 && b!=0)
        {
            ll c,d;
            c=min(a,b);
            d=max(a,b);
            if (d<c*2)
            {
                ll e=c/2;
                ll f,g;
                f=c-e;
                g=d-2*e;
                ll h=max(f,g);
                cout<<e+h/2<<endl;

            }
            else
                cout<<c<<endl;
        }
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define min(x,y) (x < y? x:y)
#define max(x,y) (x > y? x:y)
#define vi vector <ll>
#define pb push_back
#define pob pop_back()
#define pi pair <int,int>
#define F first
#define S second
#define MP make_pair
#define FOR(a,b) for(int a=0;a<b;a++)
#define FORL(a,b,c) for(int a=b;a<=c;a++)
#define FORR(a,b,c) for(int a=b;a>=c;a--)
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if(a!=0 && b!=0 && 2*min(a,b)>max(a,b))
        cout<<(a+b)/3<<endl;
        else if (a!=0 && b!=0 && 2*min(a,b)<=max(a,b))
        cout<<min(a,b)<<endl;
        else
        cout<<"0"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(NULL);cerr.tie(NULL)
#define endl '\n'
#define pb push_back
typedef long long ll;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if(a==0 || b==0)
        cout<<0<<endl;
        else
        cout<<(a+b)/3<<endl;
    }

    return 0;
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;
    int c=0;

    if(a==b)
    return a/2;
   if(a>b)
   {
   while((a-2)>=0 && (b-1)>=0)
   {

       a=a-2;
       b=b-1;
       c++;
       if(b==2*a)
       return c+2;

   }
   }

   else
   {
   while((a-1)>=0 && (b-2)>=0)
   {
       a=a-1;
       b=b-2;
       c++;

   }
   }
    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;

    int c=0;

    if(a==b)
    {
        a=a-2;
        b=b-1;
        c=1;
    }

  int mi=min(a,b);
  int ma=max(a,b);

  if(a%2==1 && b%2==1)
  {
  if(ma>=2*mi)
  c=c+mi;
  else
  c=c+ma/2;
  }
  else if(a%2==0 && b%2==0)
  {
      if(ma>=2*mi)
      c=c+mi;
      else
      c=c+ma/2;
  }

   else
   {
       if(ma>=2*mi)
       c+=mi;
       else
       c+=ma/2+1;
   }

    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(NULL);cerr.tie(NULL)
#define endl '\n'
#define pb push_back
typedef long long ll;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if(a==0 || b==0)
        {
            cout<<0<<endl;
            continue;
        }
        if(a>b)
        {
            swap(a,b);
        }
        if(2*a<=b)
        cout<<a<<endl;
        else
        cout<<(a+b)/3<<endl;
    }

    return 0;
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;
    int c=0;

    if(a==b)
    return a/2;
  int mi=min(a,b);
  int ma=max(a,b);

  if(a%2==1 && b%2==1)
  c=ma/2;
  if(a%2==0 && b%2==0)
  c=ma/2;

   else
   c=ma/2+1;

    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(NULL);cerr.tie(NULL)
#define endl '\n'
#define pb push_back
typedef long long ll;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if(a==0 || b==0)
        {
            cout<<0<<endl;
            continue;
        }
        if(a>b)
        {
            swap(a,b)
        }
        if(2*a<=b)
        cout<<a<<endl;
        else
        cout<<(a+b)/3<<endl;
    }

    return 0;
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;
    int c=0;

    if(a==b)
    return a/2;
  int mi=min(a,b);
  int ma=max(a,b);

  if(a%2==1 && b%2==1)
  c=ma/2;
  else if(a%2==0 && b%2==0)
  c=ma/2;

   else
   c=ma/2+1;

    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;
    int c=0;

    if(a==b)
    return a/2;
   if(a>b)
   {
   while((a-2)>=0 && (b-1)>=0)
   {

       a=a-2;
       b=b-1;
       c++;
       if(b==2*a)
       return c+2;

   }
   }

   else
   {
   c=a;
   }
    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;

    int c=0;

    // if(a==b)
    // return a/2;

  int mi=min(a,b);
  int ma=max(a,b);

  if(a%2==1 && b%2==1)
  {
  if(ma>=2*mi)
  c=mi;
  else
  c=ma/2;
  }
  else if(a%2==0 && b%2==0)
  {
      if(ma>=2*mi)
      c=mi;
      else
      c=ma/2;
  }

   else
   {
       if(ma>=2*mi)
       c=mi;
       else
       c=ma/2+1;
   }

    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


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

ll ans(ll a,ll b)
{
    if(a==0 || b==0)
    return 0;
    if(a==1 && b==1)
    return 0;
    int c=0;

    if(a==b)
    return a/2;
  int mi=min(a,b);
  int ma=max(a,b);

  if(a%2==1 && b%2==1)
  {
  if(ma>=2*mi)
  c=mi;
  else
  c=ma/2;
  }
  else if(a%2==0 && b%2==0)
  {
      if(ma>=2*mi)
      c=mi;
      else
      c=ma/2;
  }

   else
   {
       if(ma>=2*mi)
       c=mi;
       else
       c=ma/2+1;
   }

    return c;
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       cout<<ans(a,b)<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;
    if(i==1&&j==2)
    {
        cout<<m++<<endl;
continue;}
       if(j>=2)
       {

            k=j/2;

              cout<<m+k<<endl;

          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;

   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   if(d>m)
   {
       j=d-m;
       if(j>=2)
       {
          m--;
          j++;
          i+=2;
          k=j/2;
          if(i>=k)
          {
              cout<<m+k<<endl;
          }
          else cout<<m++<<endl;

       }
   }else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c==d)
   {
       cout<<c/2<<endl;
       continue;
   }
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;

       if(j>=2)
       {

           if(j%2==0)

              cout<<m+(j/2)<<endl;
              else {if(i==1)
                    cout<<m+(j/2)+1<<endl;
              else cout<<m+(j/2)<<endl;

          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   if(d>m)
   {
       j=d-m;
       if(j>=2)
       {
          m--;
          j++;
          i+=2;
          k=j/2;
          if(i>=k)
          {
              cout<<m+k<<endl;
          }
          else cout<<m++<<endl;

       }
   }else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;
    if(i==1&&j==2)
    {
        cout<<m++<<endl;
continue;}
       if(j>2)
       {

            k=j/2;

              cout<<m+k<<endl;

          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;
    if(i==1&&j==2)
    {
        cout<<m++<<endl;
continue;}
       if(j>2)
       {

          k=j/2;
            if(j%2==0)
              cout<<m+k<<endl;
              else if(i==1&&j%2==1) cout<<m+k+1<<endl;
              else if(i==0&&j%2==1)cout<<m+k<<endl;
          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   while(c!=0&&d!=0)
   {
       if(c>d)
       {
           c-=2;
           d--;
           if(c>=0&&d>=0)i++;
       }
       else {
        d-=2;
        c--;
        if(c>=0&&d>=0)i++;

       }
   }
   cout<<i<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c==d)
   {
       cout<<c/2<<endl;
       continue;
   }
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;

       if(j>=2)
       {

           k=1;

              cout<<m+k<<endl;

          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
    cout<<"0"<<endl;
   else {
    c=max(a,b);
    d=min(a,b);
    if(c>=2*d)cout<<d<<endl;
    else cout<<(c+d)/3<<endl;
   }
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
    cout<<"0"<<endl;
   else {
    c=max(a,b);
    d=min(a,b);
    if(a>=2*b)cout<<b<<endl;
    else cout<<(c+d)/3<<endl;
   }
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;

       if(j>=2)
       {

           k=1;

              cout<<m+k<<endl;

          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c==d)
   {
       cout<<c/2<<endl;
       continue;
   }
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;

       if(j>=2)
       {

           if(j%2==0)

              cout<<m+(j/2)<<endl;
              else {if(i==1)
                    cout<<m+(j/2)+1<<endl;
              else cout<<m+(j/2)<<endl;
              }
          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;

   if(d>m)
   {
       j=d-m;
if(i==1&&j==2){cout<<m++<<endl;continue;}
       if(j>2)
       {

          k=j/2;

              cout<<m+k<<endl;
          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
     int t,a,d,n,i,m,c,j,k,count,b;
    string s;
     cin>>t;
   while(t--)
    {cin>>a>>b;

   if(a==0||b==0)
   {
       cout<<"0"<<endl;
continue;
   }
   c=max(a,b);
   d=min(a,b);i=0;
   if(c%2==0)m=c/2;
   else{
    m=c/2;
    i=1;
   }
   k=0;
   if(d>m)
   {
       j=d-m;

       if(j>2)
       {

          k=j/2;

              cout<<m+k<<endl;
          }
          else cout<<m<<endl;

       }
   else if(d==m)
   {
       cout<<m<<endl;

   }
   else cout<<"0"<<endl;

}
}


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
LL a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	LL ans = 0;
	LL d = a - b;
	LL x = min(d + 1ll, b);
	ans += x;
	a -= x + x;
	b -= x;

	if ((a + b) % 3 == 0)
		ans += (a + b) / 3;
	else
	{
		x = (a / 3);
		ans += x + x;
		a -= x * 3;
		b -= x * 3;
		if (b >= 1 && a >= 2)
			ans++;
	}
	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
100 90

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
int a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	int ans = 0;
	int d = a - b;
	int x = min(d + 1, b);
	ans += x;
	a -= x + x;
	b -= x;
	x = (min(a, b) / 3);
	ans += x + x;
	a -= x * 3;
	b -= x * 3;
	if (a >= 2 && b >= 1)
		ans++;
	swap(a, b);
	if (a >= 2 && b >= 1)
		ans++;

	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
12 9

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
LL a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	LL ans = 0;
	LL d = a - b;
	LL x = min(d + 1ll, b);
	ans += x;
	a -= x + x;
	b -= x;

	if ((a + b) % 3 == 0 && a && b)
		ans += (a + b) / 3;
	else
	{
		x = (min(a,b) / 3);
		ans += x + x;
		a -= x * 3;
		b -= x * 3;
		if (b >= 1 && a >= 2)
			ans++;
	}
	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
100 90

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
LL a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	LL ans = 0;
	LL d = a - b;
	LL x = min(d + 1ll, b);
	ans += x;
	a -= x + x;
	b -= x;

	if ((a + b) % 3 == 0 && a && b)
		ans += (a + b) / 3;
	else
	{
		x = (min(a,b) / 3);
		ans += x + x;
		a -= x * 3;
		b -= x * 3;
		swap(a, b);
		if (b >= 1 && a >= 2)
		{
			ans++;
			b--;
			a -= 2;
		}
		swap(a, b);
		if (b >= 1 && a >= 2)
		{
			ans++;
			b--;
			a -= 2;
		}
	}
	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
100 90

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
LL a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	LL ans = 0;
	LL d = a - b;
	LL x = min(d, b);
	ans += x;
	a -= x + x;
	b -= x;

	if (a && b)
		ans += (a + b) / 3;
	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
100 90

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
LL a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	LL ans = 0;
	LL d = a - b;
	LL x = min(d + 1ll, b);
	ans += x;
	a -= x + x;
	b -= x;

	if ((a + b) % 3 == 0)
		ans += (a + b) / 3;
	else
	{
		x = (a / 3);
		ans += x + x;
		a -= x * 3;
		b -= x * 3;
	}
	if (b >= 1 && a >= 2)
		ans++;
	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
100 90

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 500005;

int T;
int a, b;

void solve()
{
	cin >> a >> b;
	if (a < b)
		swap(a, b);
	int ans = 0;
	int d = a - b;
	int x = min(d, b);
	ans += x;
	a -= x + x;
	b -= x;
	x = (min(a, b) / 3);
	ans += x + x;
	if (a >= 2 && b >= 1)
		ans++;
	swap(a, b);
	if (a >= 2 && b >= 1)
		ans++;

	cout << ans << endl;
}

int main()
{
	fastIO;
	cin >> T;
	while (T--)
		solve();

	return 0;
}

/*

1
12 8

*/


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

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

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

#define lf double

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

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

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

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

int a[maxn],b[maxn],p[maxn],q[maxn],n,tot;

struct data {int x1,y1,x2,y2;}r[maxn];

int main() {
    read(n);
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=1;i<=n;i++) read(b[i]);
    for(int i=1;i<=n;i++) p[a[i]]=i,q[b[i]]=i;
    for(int i=1;i<=n;i++) {
        if(a[i]==i&&b[i]==i) continue;
        int x=p[i],y=q[i];
        r[++tot]=(data){x,i,i,y};
        swap(a[i],a[x]),swap(b[y],b[i]);
    }write(tot);
    for(int i=1;i<=tot;i++) printf("%d %d %d %d\n",r[i].x1,r[i].y1,r[i].x2,r[i].y2);
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 2333;
int n;
struct node{
	int p[N],q[N];
	void init(){
		rep(i,1,n)read(p[i]),q[p[i]]=i;
	}
	void f(int x, int y){
		if(x!=y){
			swap(p[x],p[y]);swap(q[p[x]],q[p[y]]);
		}
	}
}x,y;
struct Ans{int a,b,c,d;}ans[N];int sz;
int main() {
	read(n);x.init();y.init();
	rep(i,1,n){
		int a=x.q[i],b=y.q[i];
		if(a!=i||b!=i)ans[++sz]=(Ans){i,b,a,i};
		x.f(i,a);y.f(i,b);
	}
	printf("%d\n",sz);
	rep(i,1,sz)printf("%d %d %d %d\n",ans[i].a,ans[i].b,ans[i].c,ans[i].d);
	return 0;
}


#include<bits/stdc++.h>
#define maxn 2010
using namespace std;
template<typename T> inline void read(T &x)
{
    x=0;char c=getchar();bool flag=false;
    while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    if(flag)x=-x;
}
int n,cnt;
int r[maxn],c[maxn];
struct node
{
    int x1,y1,x2,y2;
}t[maxn];
int main()
{
    read(n);
    for(int i=1;i<=n;++i) read(r[i]);
    for(int i=1;i<=n;++i) read(c[i]);
    for(int i=1;i<n;++i)
    {
        int x,y;
        for(int j=1;j<=n;++j)
        {
            if(r[j]==i) x=j;
            if(c[j]==i) y=j;
        }
        if(x==i&&y==i) continue;
        t[++cnt]={x,i,i,y},swap(r[i],r[x]),swap(c[i],c[y]);
    }
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;++i)
        printf("%d %d %d %d\n",t[i].x1,t[i].y1,t[i].x2,t[i].y2);
    return 0;
}


#include <bits/stdc++.h>
using namespace std ;
const int N = 1005 ;
int a[N],b[N],n ;
vector<pair<pair<int,int>,pair<int,int> > > ans ;
int main(){
	//freopen("in.txt","r",stdin) ;
	ios::sync_with_stdio(false) ;
	cin >> n ;
	for(int i = 1 ; i <= n ; i++) cin >> a[i] ;
	for(int i = 1 ; i <= n ; i++) cin >> b[i] ;
	for(int i = 1 ; i < n ; i++){
		if(a[i] == i && b[i] == i) continue ;
		int c = 0,d = 0 ;
		for(int j = 1 ; j <= n ; j++)
		    if(a[j] == i) c = j ;
		for(int j = 1 ; j <= n ; j++)
		    if(b[j] == i) d = j ;
		ans.push_back(make_pair(make_pair(i,d),make_pair(c,i))) ;
		swap(a[i],a[c]) ;
		swap(b[i],b[d]) ;
	}
	for(int i = 0 ; i < ans.size() ; i++)
	    cout << ans[i].first.first << " " << ans[i].first.second << " " <<
	    ans[i].second.first << " " << ans[i].second.second << endl ;
 	return 0 ;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[1005],b[1005],pa[1005],pb[1005],fromx[1005],fromy[1005],tox[1005],toy[1005];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
		pb[b[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		pa[a[i]]=i;
	}
	for(int i=n;i>1;i--)
	{
		if(a[i]==i&&b[i]==i)continue;
		fromx[++m]=i;
		fromy[m]=a[i];
		a[pa[i]]=a[i];
		pa[a[i]]=pa[i];
		tox[m]=b[i];
		toy[m]=i;
		b[pb[i]]=b[i];
		pb[b[i]]=pb[i];
	}
	printf("%d\n",m);
	for(int i=1;i<=m;i++)printf("%d %d %d %d\n",fromx[i],fromy[i],tox[i],toy[i]);
	return 0;
}


#include <bits/stdc++.h>

inline int read()
{
	int res = 0; bool bo = 0; char c;
	while (((c = getchar()) < '0' || c > '9') && c != '-');
	if (c == '-') bo = 1; else res = c - 48;
	while ((c = getchar()) >= '0' && c <= '9')
		res = (res << 3) + (res << 1) + (c - 48);
	return bo ? ~res + 1 : res;
}

const int N = 1005;

int n, m, ir[N], ic[N], a[N], ia[N], b[N], ib[N],
ans1x[N], ans1y[N], ans2x[N], ans2y[N];

int main()
{
	n = read();
	for (int i = 1; i <= n; i++) ir[read()] = a[i] = ia[i] = i;
	for (int i = 1; i <= n; i++) ic[read()] = b[i] = ib[i] = i;
	for (int i = 1; i <= n; i++)
	{
		if (ir[i] == a[i] && ic[i] == b[i]) continue;
		int x = ia[ir[i]], y = ib[ic[i]];
		ans1x[++m] = x; ans1y[m] = ans2x[m] = i;
		ans2y[m] = y;
		a[x] = a[i]; b[y] = b[i];
		ia[a[i]] = x; ib[b[i]] = y;
	}
	std::cout << m << std::endl;
	for (int i = 1; i <= m; i++)
		printf("%d %d %d %d\n", ans1x[i], ans1y[i], ans2x[i], ans2y[i]);
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define N 1005
int n; int a[N]; int b[N];  int cnt;
pair<  pair<int,int>,pair<int,int>  >ans[N];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d",a+i);
    for(int i=1;i<=n;++i) scanf("%d",b+i);
    for(int i=n;i>=1;--i){
        if(a[i]!=i||b[i]!=i){
            ans[++cnt]=make_pair( make_pair(i, b[i]),make_pair(a[i], i) );
            for(int j=1;j<i;++j) if(a[j]==i) a[j]=a[i];
            for(int j=1;j<i;++j) if(b[j]==i) b[j]=b[i];
        }
    }
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;++i) printf("%d %d %d %d\n",ans[i].first.first,ans[i].first.second,ans[i].second.first,ans[i].second.second);
    return 0;
}


#include<map>
#include<set>
#include<ctime>
#include<queue>
#include<cmath>
#include<bitset>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mp(x,y) make_pair(x,y)
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
inline int read(){
	int ans=0,fh=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') fh=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
		ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
	return ans*fh;
}
const int maxn=1e3+100;
struct quest{
	int x,y,a,b;
}qu[maxn];
int n,p[maxn],q[maxn],r[maxn],c[maxn],Ans;
int main(){
//	freopen("nh.in","r",stdin);
	//freopen("zhy.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
		p[r[i]=read()]=i;
	for(int i=1;i<=n;i++)
		q[c[i]=read()]=i;
	for(int i=1;i<=n;i++){
		if(!(p[i]==i&&q[i]==i))
			qu[++Ans]=(quest){p[i],i,i,q[i]};
		p[r[i]]=p[i],q[c[i]]=q[i],r[p[i]]=r[i],c[q[i]]=c[i];
	}
	printf("%d\n",Ans);
	for(int i=1;i<=Ans;i++)
		printf("%d %d %d %d\n",qu[i].x,qu[i].y,qu[i].a,qu[i].b);
	return 0;
}


#include <bits/stdc++.h>
using namespace std ;
const int N = 1005 ;
int a[N],b[N],n ;
vector<pair<pair<int,int>,pair<int,int> > > ans ;
int main(){
	//freopen("in.txt","r",stdin) ;
	ios::sync_with_stdio(false) ;
	cin >> n ;
	for(int i = 1 ; i <= n ; i++) cin >> a[i] ;
	for(int i = 1 ; i <= n ; i++) cin >> b[i] ;
	for(int i = 1 ; i < n ; i++){
		if(a[i] == i && b[i] == i) continue ;
		int c = 0,d = 0 ;
		for(int j = 1 ; j <= n ; j++)
		    if(a[j] == i) c = j ;
		for(int j = 1 ; j <= n ; j++)
		    if(b[j] == i) d = j ;
		ans.push_back(make_pair(make_pair(i,d),make_pair(c,i))) ;
		swap(a[i],a[c]) ;
		swap(b[i],b[d]) ;
	}
	cout << ans.size() << endl ;
	for(int i = 0 ; i < ans.size() ; i++)
	    cout << ans[i].first.first << " " << ans[i].first.second << " " <<
	    ans[i].second.first << " " << ans[i].second.second << endl ;
 	return 0 ;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vic vector<int>
#define vit vic::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
#define rsort(x,y) sort(x,y),reverse(x,y)
using namespace std;

inline char gc() {
//	static char buf[100000],*p1,*p2;
//	return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
	return getchar();
}

template<class T>
int read(T &ans) {
	ans=0;char ch=gc();T f=1;
	while(!isdigit(ch)) {
		if(ch==EOF) return -1;
		if(ch=='-') f=-1;
		ch=gc();
	}
	while(isdigit(ch))
		ans=ans*10+ch-'0',ch=gc();
	ans*=f;return 1;
}

template<class T1,class T2>
int read(T1 &a,T2 &b) {
	return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}

template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
	return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}

typedef long long ll;
const int Maxn=1100;
const int inf=0x3f3f3f3f;

int a[Maxn],b[Maxn],r[Maxn],c[Maxn];
int n,x,tot;
pair<pir,pir> ans[Maxn];

void work(int x) {
	if(x==n) return ;
	if(a[x]==r[x]&&b[x]==c[x]) work(x+1);
	else {
		int y,z;
		for(int i=x;i<=n;i++) if(a[i]==r[x]) {
			y=i;
			break;
		}
		for(int i=x;i<=n;i++) if(b[i]==c[x]) {
			z=i;
			break;
		}
		swap(a[y],a[x]); swap(b[z],b[x]);
		ans[++tot]=mp(mp(y,x),mp(x,z));
		work(x+1);
	}
}

signed main() {
//	freopen("test.in","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(x),r[x]=i;
	for(int i=1;i<=n;i++) read(x),c[x]=i;
	for(int i=1;i<=n;i++) a[i]=b[i]=i;
	work(1);
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++) {
		printf("%d %d %d %d\n",ans[i].fr.fr,ans[i].fr.sc,ans[i].sc.fr,ans[i].sc.sc);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vic vector<int>
#define vit vic::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
#define rsort(x,y) sort(x,y),reverse(x,y)
using namespace std;

inline char gc() {
//	static char buf[100000],*p1,*p2;
//	return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
	return getchar();
}

template<class T>
int read(T &ans) {
	ans=0;char ch=gc();T f=1;
	while(!isdigit(ch)) {
		if(ch==EOF) return -1;
		if(ch=='-') f=-1;
		ch=gc();
	}
	while(isdigit(ch))
		ans=ans*10+ch-'0',ch=gc();
	ans*=f;return 1;
}

template<class T1,class T2>
int read(T1 &a,T2 &b) {
	return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}

template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
	return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}

typedef long long ll;
const int Maxn=1100;
const int inf=0x3f3f3f3f;

int a[Maxn],b[Maxn],r[Maxn],c[Maxn];
int n,x,tot;
pair<pir,pir> ans[Maxn];

void work(int x) {
	if(x==n) return ;
	if(a[x]==r[x]&&b[x]==c[x]) work(x+1);
	else {
		int y,z;
		for(int i=x;i<=n;i++) if(a[i]==r[x]) {
			y=i;
			break;
		}
		for(int i=x;i<=n;i++) if(a[i]==c[x]) {
			z=i;
			break;
		}
		ans[++tot]=mp(mp(y,x),mp(x,z));
	}
}

signed main() {
//	freopen("test.in","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(x),r[x]=i;
	for(int i=1;i<=n;i++) read(x),c[x]=i;
	for(int i=1;i<=n;i++) a[i]=b[i]=i;
	work(1);
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++) {
		printf("%d %d %d %d\n",ans[i].fr.fr,ans[i].fr.sc,ans[i].sc.fr,ans[i].sc.sc);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vic vector<int>
#define vit vic::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
#define rsort(x,y) sort(x,y),reverse(x,y)
using namespace std;

inline char gc() {
//	static char buf[100000],*p1,*p2;
//	return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
	return getchar();
}

template<class T>
int read(T &ans) {
	ans=0;char ch=gc();T f=1;
	while(!isdigit(ch)) {
		if(ch==EOF) return -1;
		if(ch=='-') f=-1;
		ch=gc();
	}
	while(isdigit(ch))
		ans=ans*10+ch-'0',ch=gc();
	ans*=f;return 1;
}

template<class T1,class T2>
int read(T1 &a,T2 &b) {
	return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}

template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
	return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}

typedef long long ll;
const int Maxn=1100;
const int inf=0x3f3f3f3f;

int a[Maxn],b[Maxn],r[Maxn],c[Maxn];
int n,x,tot;
pair<pir,pir> ans[Maxn];

void work(int x) {
	if(x==n) return ;
	if(a[x]==r[x]&&b[x]==c[x]) work(x+1);
	else {
		int y,z;
		for(int i=x;i<=n;i++) if(a[i]==r[x]) {
			y=i;
			break;
		}
		for(int i=x;i<=n;i++) if(a[i]==c[x]) {
			z=i;
			break;
		}
		swap(a[y],a[x]); swap(b[z],b[x]);
		ans[++tot]=mp(mp(y,x),mp(x,z));
		work(x+1);
	}
}

signed main() {
//	freopen("test.in","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(x),r[x]=i;
	for(int i=1;i<=n;i++) read(x),c[x]=i;
	for(int i=1;i<=n;i++) a[i]=b[i]=i;
	work(1);
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++) {
		printf("%d %d %d %d\n",ans[i].fr.fr,ans[i].fr.sc,ans[i].sc.fr,ans[i].sc.sc);
	}
	return 0;
}


#include <bits/stdc++.h>

const int N = 1005;

int r[N], c[N], ir[N], ic[N];
int x[N], y[N], z[N], w[N];
int n, m;

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i)
		std::cin >> r[i], ir[r[i]] = i;
	for (int i = 1; i <= n; ++i)
		std::cin >> c[i], ic[c[i]] = i;
	for (int i = 1; i <= n; ++i) {
		int p = ir[i], q = ic[i];
		if (p == i && q == i) continue;
		x[m] = i, y[m] = p, z[m] = q, w[m++] = i;
		std::swap(r[i], r[p]), std::swap(ir[r[i]], ir[r[p]]);
		std::swap(c[i], c[q]), std::swap(ic[c[i]], ic[c[p]]);
	}
	std::cout << m << '\n';
	for (int i = 0; i < m; ++i)
		std::cout << x[i] << ' ' << y[i] << ' ' << z[i] << ' ' << w[i] << '\n';
	return 0;
}


#include <bits/stdc++.h>

const int N = 1005;

int r[N], c[N], ir[N], ic[N];
int x[N], y[N], z[N], w[N];
int n, m;

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i)
		std::cin >> r[i], ir[r[i]] = i;
	for (int i = 1; i <= n; ++i)
		std::cin >> c[i], ic[c[i]] = i;
	for (int i = 1; i <= n; ++i) {
		int p = ir[i], q = ic[i];
		if (p == i && q == i) continue;
		x[m] = i, y[m] = q, z[m] = p, w[m++] = i;
		std::swap(r[i], r[p]), std::swap(ir[r[i]], ir[r[p]]);
		std::swap(c[i], c[q]), std::swap(ic[c[i]], ic[c[p]]);
	}
	std::cout << m << '\n';
	for (int i = 0; i < m; ++i)
		std::cout << x[i] << ' ' << y[i] << ' ' << z[i] << ' ' << w[i] << '\n';
	return 0;
}


#include <bits/stdc++.h>

const int N = 1005;

int r[N], c[N], ir[N], ic[N];
int x[N], y[N], z[N], w[N];
int n, m;

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i)
		std::cin >> r[i], ir[r[i]] = i;
	for (int i = 1; i <= n; ++i)
		std::cin >> c[i], ic[c[i]] = i;
	for (int i = 1; i <= n; ++i) {
		int p = ir[i], q = ic[i];
		if (p == i && q == i) continue;
		x[m] = i, y[m] = q, z[m] = p, w[m++] = i;
		std::swap(r[i], r[p]), std::swap(ir[r[i]], ir[r[p]]);
		std::swap(c[i], c[q]), std::swap(ic[c[i]], ic[c[q]]);
	}
	std::cout << m << '\n';
	for (int i = 0; i < m; ++i)
		std::cout << x[i] << ' ' << y[i] << ' ' << z[i] << ' ' << w[i] << '\n';
	return 0;
}


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

const int MAXN = 1005;
int r[MAXN], c[MAXN], r1[MAXN], c1[MAXN], n;
struct Node { int x1, y1, x2, y2; };
vector<Node> ans;
void push(int x1, int y1, int x2, int y2) {
	ans.push_back((Node) { x1, y1, x2, y2 });
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", r + i);
		r1[r[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		scanf("%d", c + i);
		c1[c[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		if (r[i] == i && c[i] == i) continue;
		push(i, c[i], r[i], i);
		if (r1[i] > i) r[r1[i]] = r[i], r1[r[i]] = r1[i];
		if (c1[i] > i) c[c1[i]] = c[i], c1[c[i]] = c1[i];
	}
	printf("%d\n", (int)ans.size());
	for (Node d : ans) printf("%d %d %d %d\n", d.x1, d.y1, d.x2, d.y2);
	return 0;
}


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

const int MAXN = 1005;
int r[MAXN], c[MAXN], r1[MAXN], c1[MAXN], n;
struct Node { int x1, y1, x2, y2; };
vector<Node> ans;
void push(int x1, int y1, int x2, int y2) {
	ans.push_back((Node) { x1, y1, x2, y2 });
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", r1 + i);
		r[r1[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		scanf("%d", c1 + i);
		c[c1[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		if (r[i] == i && c[i] == i) continue;
		push(i, c[i], r[i], i);
		if (r1[i] > i) r[r1[i]] = r[i], r1[r[i]] = r1[i];
		if (c1[i] > i) c[c1[i]] = c[i], c1[c[i]] = c1[i];
	}
	printf("%d\n", (int)ans.size());
	for (Node d : ans) printf("%d %d %d %d\n", d.x1, d.y1, d.x2, d.y2);
	return 0;
}


#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

typedef long long LL;

int read() {
  int ans = 0, c, f = 1;
  while (!isdigit(c = getchar()))
    if (c == '-') f *= -1;
  do ans = ans * 10 + c - '0';
  while (isdigit(c = getchar()));
  return ans * f;
}

const int N = 10050;

int n, A[N], B[N];
int m, a[N], b[N], c[N], d[N];

void por(int x1, int y1, int x2, int y2) {
  a[m] = x1; b[m] = y1; c[m] = x2; d[m] = y2; ++m;
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i) A[i] = read();
  for (int i = 1; i <= n; ++i) B[i] = read();
  for (int k = 1; k <= n; ++k) {
    int ti = 0, tj = 0;
    for (int i = k; i <= n; ++i) {
      if (A[i] == k) ti = i;
      if (B[i] == k) tj = i;
    }
    if (ti == k && tj == k) ;
    else if (ti == k) por(k, k, k, tj);
    else if (tj == k) por(k, k, ti, k);
    else por(ti, k, k, tj);
    std::swap(A[k], A[ti]);
    std::swap(B[k], B[tj]);
  }
  printf("%d\n", m);
  for (int i = 0; i < m; ++i)
    printf("%d %d %d %d\n", a[i], b[i], c[i], d[i]);
  return 0;
}


#include<algorithm>
#include<chrono>
#include<climits>
#include<numeric>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<random>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;

#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v) {for(auto x : v) cout << x << ' '; cout << endl;}
#define printn(v, n) {for(int _i = 0; _i < n; _i++) cout << *(v + _i) << ' '; cout << endl;}

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e3 + 10;

int a[MAXN], b[MAXN], ma[MAXN], mb[MAXN];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> b[i];
        b[i]--;
        mb[b[i]] = i;
    }
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        ma[a[i]] = i;
    }

    vector<vector<int>> res;
    for(int i = 0; i < n; i++) {
        if(i != ma[i] || i != mb[i]) {
            res.push_back({i + 1, a[i] + 1, b[i] + 1, i + 1});
            swap(a[i], a[ma[i]]);
            swap(ma[i], ma[a[ma[i]]]);
            swap(b[i], b[mb[i]]);
            swap(mb[i], mb[b[mb[i]]]);
            // printn(a, n);
            // printn(b, n);
            // cout << endl;
        }
    }
    cout << res.size() << endl;
    for(auto & v : res) {
        for(auto x : v)
            cout << x << ' ';
        cout << endl;
    }

    return 0;
}


#include<algorithm>
#include<chrono>
#include<climits>
#include<numeric>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<random>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;

#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v) {for(auto x : v) cout << x << ' '; cout << endl;}
#define printn(v, n) {for(int _i = 0; _i < n; _i++) cout << *(v + _i) << ' '; cout << endl;}

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e3 + 10;

int a[MAXN], b[MAXN], ma[MAXN], mb[MAXN], aa[MAXN], bb[MAXN];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, tmp;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> b[i];
        b[i]--;
        mb[b[i]] = i;
    }
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        ma[a[i]] = i;
    }

    vector<vector<int>> res;
    for(int i = 0; i < n; i++) {
        if(i != ma[i] || i != mb[i]) {
            res.push_back({i + 1, ma[i] + 1, mb[i] + 1, i + 1});
            swap(a[i], a[ma[i]]);
            swap(ma[i], ma[a[ma[i]]]);
            swap(b[i], b[mb[i]]);
            swap(mb[i], mb[b[mb[i]]]);
            // printn(a, n);
            // printn(b, n);
            // cout << endl;
        }
    }
    cout << res.size() << endl;
    for(auto & v : res) {
        for(auto x : v)
            cout << x << ' ';
        cout << endl;
    }

    return 0;
}


#include<algorithm>
#include<chrono>
#include<climits>
#include<numeric>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<random>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;

#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v) {for(auto x : v) cout << x << ' '; cout << endl;}
#define printn(v, n) {for(int _i = 0; _i < n; _i++) cout << *(v + _i) << ' '; cout << endl;}

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e3 + 10;

int a[MAXN], b[MAXN], ma[MAXN], mb[MAXN], aa[MAXN], bb[MAXN];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, tmp;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> tmp;
        tmp--;
        b[tmp] = i;
        mb[i] = tmp;
    }
    for(int i = 0; i < n; i++) {
        cin >> tmp;
        tmp--;
        a[tmp] = i;
        ma[i] = tmp;
    }

    vector<vector<int>> res;
    for(int i = 0; i < n; i++) {
        if(i != ma[i] || i != mb[i]) {
            res.push_back({i + 1, ma[i] + 1, mb[i] + 1, i + 1});
            swap(a[i], a[ma[i]]);
            swap(ma[i], ma[a[ma[i]]]);
            swap(b[i], b[mb[i]]);
            swap(mb[i], mb[b[mb[i]]]);
            // printn(a, n);
            // printn(b, n);
            // cout << endl;
        }
    }
    cout << res.size() << endl;
    for(auto & v : res) {
        for(auto x : v)
            cout << x << ' ';
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define maxn 2010
using namespace std;
template<typename T> inline void read(T &x)
{
    x=0;char c=getchar();bool flag=false;
    while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    if(flag)x=-x;
}
int n,cnt;
int r[maxn],c[maxn];
struct node
{
    int x1,y1,x2,y2;
}t[maxn];
int main()
{
    read(n);
    for(int i=1;i<=n;++i) read(r[i]);
    for(int i=1;i<=n;++i) read(c[i]);
    for(int i=1;i<n;++i)
    {
        int x,y;
        for(int j=1;j<=n;++j)
        {
            if(r[j]==i) x=j;
            if(c[j]==i) y=j;
        }
        if(x==i&&y==i) continue;
        t[++cnt]={x,i,i,y},swap(r[i],r[x]),swap(c[i],c[y]);
    }
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;++i)
        printf("%d %d %d %d\n",t[i].x1,t[i].y1,t[i].x2,t[i].y2);
    return 0;
}


#include <algorithm>
// #include <cassert>
#include <cctype>
// #include <cmath>
#include <cstdio>
// #include <cstdlib>
#include <cstring>
// #include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define fir first
#define sec second
#define pb push_back
#define pch putchar
#define rg register
#define SZ(_) (int)_.size()
#define SORT(_) std::sort(_.begin(), _.end())
#define LB(_, __) std::lower_bound(_.begin(), _.end(), __)
#define UB(_, __) std::upper_bound(_.begin(), _.end(), __)

typedef long long LL;
typedef double DB;
typedef std::pair<DB, DB> Pdd;
typedef std::pair<int, int> Pii;
typedef std::pair<LL, int> Pli;
typedef std::pair<int, LL> Pil;
typedef std::pair<LL, LL> Pll;
typedef std::vector<int> Vi;
typedef std::vector<LL> Vl;
typedef std::map<int, int> Mii;
typedef std::map<LL, int> Mli;
typedef std::map<int, LL> Mil;
typedef std::map<LL, LL> Mll;
typedef std::set<int> Si;
typedef std::set<LL> Sl;

const int kInf = 0x3f3f3f3f, kMod = 1e9 + 7;
const LL kInf64 = 0x3f3f3f3f3f3f3f3f;
// const double kPie = acos(-1), kEps = 1e-9;

template <typename T> inline
void UMin(T & x, const T & y) { if (x > y) x = y; }
template <typename T> inline
void UMax(T & x, const T & y) { if (x < y) x = y; }
inline int Add(int a, int b) { return (a += b) >= kMod ? a - kMod : a; }
inline int Sub(int a, int b) { return (a -= b) < 0 ? a + kMod : a; }
inline int Mul(int a, int b) { return 1ll * a * b % kMod; }
int FPow(int bs, int ex = kMod - 2) {
	int res = 1;
	for (; ex; bs = Mul(bs, bs), ex >>= 1)
		if (ex & 1) res = Mul(res, bs);
	return res;
}

struct Door { int x1, y1, x2, y2; };

const int kMaxn = 1005;

int n;
int R[kMaxn], C[kMaxn];
std::vector<Door> dr;

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", R + i);
	for (int i = 1; i <= n; ++i) scanf("%d", C + i);
	for (int i = 1; i < n; ++i) {
		int x, y;
		for (int j = 1; j <= n; ++j) {
			if (R[j] == i) x = j;
			if (C[j] == i) y = j;
		}
		if (x == i && y == i) continue;
		dr.pb({i, y, x, i});
		std::swap(R[i], R[x]);
		std::swap(C[i], C[y]);
	}
	printf("%d\n", SZ(dr));
	for (auto & d : dr) printf("%d %d %d %d\n", d.x1, d.y1, d.x2, d.y2);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 1001;

int r[maxn], c[maxn];
int pr[maxn], pc[maxn];
int R[maxn], C[maxn];
int n;

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &r[i]);
		pr[r[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		pc[c[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		if (pr[i] == pc[i] && pr[i] == i) continue;
		R[i] = pr[i];
		C[i] = pc[i];
		r[pr[i]] = r[i];
		c[pc[i]] = c[i];
		pr[r[i]] = R[i];
		pc[c[i]] = C[i];
	}
	int m = 0;
	for (int i = 1; i <= n; i++) {
		if (R[i]) m++;
	}
	printf("%d\n", m);
	for (int i = 1; i <= n; i++) {
		if (R[i]) printf("%d %d %d %d\n", R[i], i, i, C[i]);
	}
	return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647

int inp(){
    char c = getchar();
    while(c < '0' || c > '9')
        c = getchar();
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c -'0';
        c = getchar();
    }
    return sum;
}

struct Node{
    int a;
    int b;
    int c;
    int d;
};
std::vector<Node> vec;

int a[1010], b[1010], p1[1010], p2[1010];

int main(){
    int n = inp();
    for(int i = 1; i <= n; i++)
        a[i] = inp();
    for(int i = 1; i <= n; i++)
        b[i] = inp();
    for(int i = 1; i <= n; i++)
        p1[i] = p2[i] = i;

    for(int i = 1; i < n; i++){
        vec.push_back((Node){i, p2[i], a[i], i});
        std::swap(p1[a[i]], p1[i]);
        std::swap(p2[b[i]], p2[i]);
    }
    printf("%d\n", (int)(vec.size()));
    for(int i = 0; i < vec.size(); i++)
        printf("%d %d %d %d\n", vec[i].a, vec[i].b, vec[i].c, vec[i].d);
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647

int inp(){
    char c = getchar();
    while(c < '0' || c > '9')
        c = getchar();
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c -'0';
        c = getchar();
    }
    return sum;
}

struct Node{
    int a;
    int b;
    int c;
    int d;
};
std::vector<Node> vec;

int a[1010], b[1010], p1[1010], p2[1010];

int main(){
    int n = inp();
    for(int i = 1; i <= n; i++)
        a[i] = inp();
    for(int i = 1; i <= n; i++)
        b[i] = inp();
    for(int i = 1; i <= n; i++)
        p1[i] = p2[i] = i;
    for(int i = 1; i < n; i++){
        vec.push_back((Node){p1[i], p2[i], a[i], b[i]});
        p1[a[i]] = p1[i];
        p2[b[i]] = p2[i];
    }
    printf("%d\n", (int)(vec.size()));
    for(int i = 0; i < vec.size(); i++)
        printf("%d %d %d %d\n", vec[i].a, vec[i].b, vec[i].c, vec[i].d);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,r[1010],c[1010];
int st[1010][4],tp;
int main()
{
	scanf("%d",&n);
	int x;
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&x);
		r[x]=i;
	}
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&x);
		c[x]=i;
	}
	for(int i=1;i<=n;++i)
	{
		if(r[i]==i&&c[i]==i)continue;
		st[++tp][0]=i,st[tp][1]=c[i],st[tp][2]=r[i],st[tp][3]=i;
	}
	printf("%d\n",tp);
	for(int i=1;i<=tp;++i)
		printf("%d %d %d %d\n",st[i][0],st[i][1],st[i][2],st[i][3]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define y1 qwe1
#define y2 ewq2
#define N 2005
using namespace std;
int n,cnt=0;
int a[N],b[N],x1[N],x2[N],y1[N],y2[N];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",a+i);
for(int i=1;i<=n;i++)scanf("%d",b+i);
for(int i=1;i<=n;i++){
    int pos1=i,pos2=i;
    for(int r=i;r<=n;r++){
        if(a[r]<a[pos1])pos1=r;
        if(b[r]<b[pos2])pos2=r;
    }
    if(pos1!=i||pos2!=i){
        cnt++;
        x1[cnt]=pos1,y1[cnt]=x2[cnt]=i;
        y2[cnt]=pos2;
        // cout<<x1[cnt]<<" "<<x2[cnt]
        swap(a[pos1],a[i]);
        swap(b[pos2],b[i]);
    }
}
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++)printf("%d %d %d %d\n",x1[i],y1[i],x2[i],y2[i]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define y1 qwe1
#define y2 ewq2
#define N 2005
using namespace std;
int n,cnt=0;
int a[N],b[N],x1[N],x2[N],y1[N],y2[N];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",a+i);
for(int i=1;i<=n;i++)scanf("%d",b+i);
for(int i=1;i<=n;i++){
    int pos1=i,pos2=i;
    for(int r=i;r<=n;r++){
        if(a[r]<a[pos1])pos1=r;
        if(b[r]<b[pos2])pos2=r;
    }
    if(pos1!=i||pos2!=i){
    // cout<<pos1<<" "<<pos2<<endl;
        cnt++;
        x1[cnt]=pos1,y1[cnt]=x2[cnt]=i;
        y2[cnt]=pos2;
        // cout<<x1[cnt]<<" "<<x2[cnt]
        swap(a[pos1],a[i]);
        swap(b[pos2],b[i]);
    }
}
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++)printf("%d %d %d %d\n",x1[i],y1[i],x2[i],y2[i]);
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
int a[1005],b[1005];
int ans[1005][4];
void gao(int a1,int a2,int a3,int a4)
{
	m++;
	ans[m][0]=a1,ans[m][1]=a2,ans[m][2]=a3,ans[m][3]=a4;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
	}
	for(int i=n;i;i--)
	{
		if(a[i]==i&&b[i]==i);
		else
		{
			for(int j=1;j<i;j++)
			{
				if(a[j]==i)a[j]=a[i];
			}
			for(int j=1;j<i;j++)
			{
				if(b[j]==i)b[j]=b[i];
			}
			gao(a[i],i,i,b[i]);
		}
	}
	printf("%d\n",m);
	for(int i=1;i<=m;i++)
	{
		printf("%d %d %d %d\n",ans[i][0],ans[i][1],ans[i][2],ans[i][3]);
	}
}


#include <iostream>
#include <cstdio>

#define rin(i,a,b) for(int i=(a);i<=(b);++i)
#define irin(i,a,b) for(int i=(a);i>=(b);--i)
#define trav(i,a) for(int i=head[a];i;i=e[i].nxt)
#define Size(a) (int)a.size()
#define pb push_back
#define mkpr std::make_pair
#define fi first
#define se second
#define lowbit(a) ((a)&(-(a)))
typedef long long LL;
typedef std::pair<int,int> pii;

using std::cerr;
using std::endl;

inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

const int MAXN=1005;

int n,r[MAXN],c[MAXN],posr[MAXN],posc[MAXN];
int cnt,x1[MAXN],y1[MAXN],x2[MAXN],y2[MAXN];

int main(){
	n=read();
	rin(i,1,n)r[i]=read(),posr[r[i]]=i;
	rin(i,1,n)c[i]=read(),posc[c[i]]=i;
	rin(i,1,n){
		cerr<<posc[2]<<endl;
//		printf("%d %d %d %d\n",posr[i],i,i,posc[i]);
		++cnt;x1[cnt]=posr[i];y1[cnt]=i;x2[cnt]=i;y2[cnt]=posc[i];
		if(x1[cnt]==x2[cnt]&&y1[cnt]==y2[cnt])--cnt;
		r[posr[i]]=r[i];posr[r[i]]=posr[i];
		c[posc[i]]=c[i];posc[c[i]]=posc[i];
	}
	printf("%d\n",cnt);
	rin(i,1,cnt)printf("%d %d %d %d\n",x1[i],y1[i],x2[i],y2[i]);
	return 0;
}


#include <cstdio>
#include <algorithm>

const int N = 1005;

int n, m, a[N], b[N], r[N], c[N], pa[N], pb[N], pr[N], pc[N], sx[N], sy[N], tx[N], ty[N];

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) { scanf("%d", &pr[i]); }
    for (int i = 1; i <= n; i++) { scanf("%d", &pc[i]); }
    for (int i = 1; i <= n; i++) { a[i] = b[i] = pa[i] = pb[i] = r[pr[i]] = c[pc[i]] = i; }
    for (int i = 1; i <= n; i++) {
        if (a[i] != r[i] || b[i] != c[i]) {
            m++; sx[m] = ty[m] = i; tx[m] = pa[r[i]]; sy[m] = pb[c[i]];
            std::swap(a[i], a[tx[m]]); pa[a[i]] = a[i]; pa[a[tx[m]]] = tx[m];
            std::swap(b[i], b[sy[m]]); pb[b[i]] = b[i]; pb[b[sy[m]]] = sy[m];
        }
    }
    printf("%u\n", m);
    for (int i = 1; i <= m; i++) { printf("%d %d %d %d\n", sx[i], sy[i], tx[i], ty[i]); }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,i,j,cnt,a[10001],b[10001],ans[10001][5],x,y;
int main(){
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=1;i<=n;i++)scanf("%d",&b[i]);
	for(i=1;i<n;i++){
		for(j=1;j<=n;j++)if(a[j]==i)x=j;
		for(j=1;j<=n;j++)if(b[j]==i)y=j;
		if(x==i&&y==i)continue;
		ans[++cnt][0]=x;ans[cnt][1]=i;
		ans[cnt][2]=i;ans[cnt][3]=y;
		swap(a[i],a[x]);swap(b[y],b[i]);
	}
	printf("%d\n",cnt);
	for(i=1;i<=cnt;i++)printf("%d %d %d %d\n",ans[i][0],ans[i][1],ans[i][2],ans[i][3]);
}


#include<bits/stdc++.h>
using namespace std;
const int NN=1004;
struct node
{
	int fromx,fromy,tox,toy;
};
int toa[NN],froma[NN],tob[NN],fromb[NN],nowtoa[NN],nowfroma[NN],nowtob[NN],nowfromb[NN];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&tob[i]);
		fromb[tob[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&toa[i]);
		froma[toa[i]]=i;
		nowfroma[i]=nowtoa[i]=nowfromb[i]=nowtob[i]=i;
	}
	vector<node>ans;
	for(int i=1;i<n;i++)
	{
		if(nowfroma[i]==froma[i]&&nowfromb[i]==fromb[i])
			continue;
		ans.push_back((node){i,nowtoa[froma[i]],nowtob[fromb[i]],i});
		int t1=nowfroma[i],t2=nowfromb[i];
		swap(nowfroma[i],nowfroma[nowtoa[froma[i]]]);
		swap(nowfromb[i],nowfromb[nowtob[fromb[i]]]);
		swap(nowtoa[froma[i]],nowtoa[t1]);
		swap(nowtob[fromb[i]],nowtob[t2]);
	}
	printf("%d\n",ans.size());
	for(int i=0;i<ans.size();i++)
		printf("%d %d %d %d\n",ans[i].fromx,ans[i].fromy,ans[i].tox,ans[i].toy);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int NN=1005;
struct node
{
	int fromx,fromy,tox,toy;
};
int toa[NN],froma[NN],tob[NN],fromb[NN],nowtoa[NN],nowfroma[NN],nowtob[NN],nowfromb[NN];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&tob[i]);
		fromb[tob[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&toa[i]);
		froma[toa[i]]=i;
		nowfroma[i]=nowtoa[i]=nowfromb[i]=nowtob[i]=i;
	}
	vector<node>ans;
	for(int i=1;i<n;i++)
	{
		if(nowfroma[i]==froma[i]&&nowfromb[i]==fromb[i])
			continue;
		ans.push_back((node){i,nowtoa[froma[i]],nowtob[fromb[i]],i});
		int t1=nowfroma[i],t2=nowfromb[i];
		swap(nowfroma[i],nowfroma[nowtoa[froma[i]]]);
		swap(nowfromb[i],nowfromb[nowtob[fromb[i]]]);
		swap(nowtoa[froma[i]],nowtoa[t1]);
		swap(nowtob[fromb[i]],nowtob[t2]);
	}
	printf("%d\n",ans.size());
	for(int i=0;i<ans.size();i++)
		printf("%d %d %d %d\n",ans[i].fromx,ans[i].fromy,ans[i].tox,ans[i].toy);
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 1050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= y; ++i)
#define repd(i, x, y) for(register int i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, r[MAXN], c[MAXN];
vector <pair <pii, pii> > ans;
int main() {
	read(n);
	rep(i, 1, n) read(r[i]);
	rep(i, 1, n) read(c[i]);
	repd(i, n, 1) {
		if(r[i] == n && c[i] == n) continue;
		rep(j, 1, i-1) if(r[j] == i) r[j] = r[i];
		rep(j, 1, i-1) if(c[j] == i) c[j] = c[i];
		ans.pb(mp(mp(r[i], i), mp(i, c[i])));
	}
	cout << ans.size() << endl;
	for(auto v: ans)
		cout << v.fir.fir << ' ' << v.fir.sec << ' ' << v.sec.fir << ' ' << v.sec.sec << endl;
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 1050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= y; ++i)
#define repd(i, x, y) for(register int i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, r[MAXN], c[MAXN];
vector <pair <pii, pii> > ans;
int main() {
	read(n);
	rep(i, 1, n) read(r[i]);
	rep(i, 1, n) read(c[i]);
	repd(i, n, 1) {
		if(r[i] == i && c[i] == i) continue;
		rep(j, 1, i-1) if(r[j] == i) r[j] = r[i];
		rep(j, 1, i-1) if(c[j] == i) c[j] = c[i];
		ans.pb(mp(mp(r[i], i), mp(i, c[i])));
	}
	cout << ans.size() << endl;
	for(auto v: ans)
		cout << v.fir.fir << ' ' << v.fir.sec << ' ' << v.sec.fir << ' ' << v.sec.sec << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int max_n = 71, inf = 1000111222;
const int max_c = 17;

int n, m, a, b, d[max_n][max_n], ans[max_n], dist[max_n][max_n];
int num, comp[max_n], sz[max_n], p[max_n], rp[max_n], dp[1 << max_c][max_n];
vector<int> g[2][max_n], all[max_n];

void bfs(int v, int d[]) {
    for (int i = 0; i < n; ++i) {
        d[i] = inf;
    }
    queue<int> q;
    q.push(v);
    d[v] = 0;
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int to : g[0][v]) {
            if (d[to] == inf) {
                d[to] = d[v] + a;
                q.push(to);
            }
        }
    }
}

void dfs(int v) {
    comp[v] = num;
    ++sz[num];
    for (int to : g[0][v]) {
        if (comp[to] == -1) {
            dfs(to);
        }
    }
}

bool cmp(int c1, int c2) {
    return sz[c1] > sz[c2];
}

int comps;

int comp_mask(int comp) {
    if (comp < comps) {
        return 1 << comp;
    }
    return 0;
}

bool get_bit(int mask, int pos) {
    return (bool) (mask & (1 << pos));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n >> m >> a >> b;
    for (int i = 0; i < m; ++i) {
        int u, v, d;
        cin >> u >> v >> d;
        --u;
        --v;
        int tp = 1;
        if (d == a) {
            tp = 0;
        }
        g[tp][u].push_back(v);
        g[tp][v].push_back(u);
    }
    memset(comp, -1, sizeof(comp));
    for (int i = 0; i < n; ++i) {
        bfs(i, d[i]);
        ans[i] = inf;
        if (comp[i] == -1) {
            dfs(i);
            ++num;
        }
    }
    for (int i = 0; i < num; ++i) {
        p[i] = i;
    }
    sort(p, p + num, cmp);
    for (int i = 0; i < num; ++i) {
        rp[p[i]] = i;
        if (sz[i] > 3) {
            ++comps;
        }
    }
    for (int i = 0; i < n; ++i) {
        comp[i] = rp[comp[i]];
        //cout << i << " " << comp[i] << " " << comps << endl;
    }
    for (int a = 0; a < n; ++a) {
        const int cost = b;
        for (int b = 0; b < n; ++b) {
            dist[a][b] = inf;
            if (comp[a] != comp[b]) {
                for (int c = 0; c < n; ++c) {
                    if (comp[a] == comp[c]) {
                        for (int to : g[1][c]) {
                            if (comp[b] == comp[to]) {
                                dist[a][b] = min(dist[a][b], d[a][c] + cost + d[to][b]);
                            }
                        }
                    }
                }
                if (dist[a][b] != inf) {
                    all[a].push_back(b);
                    //cout << a << " -> " << b << endl;
                }
            }
        }
    }
    for (int mask = 0; mask < (1 << comps); ++mask) {
        for (int last = 0; last < n; ++last) {
            dp[mask][last] = inf;
        }
    }
    dp[comp_mask(comp[0])][0] = 0;
    for (int i = 0; i < n; ++i) {
        if (comp[i] == comp[0]) {
            ans[i] = d[0][i];
        }
    }
    for (int mask = 0; mask < (1 << comps); ++mask) {
        for (int it = 0; it < n; ++it) {
            bool change = false;
            for (int last = 0; last < n; ++last) {
                if (dp[mask][last] == inf) {
                    continue;
                }
                //cout << mask << " " << last << ": " << dp[mask][last] << endl;
                ans[last] = min(ans[last], dp[mask][last]);
                for (int to : all[last]) {
                    if (comp[to] >= comps || !get_bit(mask, comp[to])) {
                        if (dp[mask | comp_mask(comp[to])][to] > dp[mask][last] + dist[last][to]) {
                            dp[mask | comp_mask(comp[to])][to] = dp[mask][last] + dist[last][to];
                            if (comp[to] >= comps) {
                                change = true;
                            }
                        }
                    }
                }
            }
            if (!change) {
                break;
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        printf("%d ", ans[i]);
    }
    puts("");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int max_n = 1011, inf = 1000111222;

int n, r[max_n], c[max_n];
int cnt, ansx[max_n][2], ansy[max_n][2];

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &r[i]);
        --r[i];
    }
    for (int i = 0; i < n; ++i) {
        scanf("%d", &c[i]);
        --c[i];
    }
    for (int i = 0; i < n; ++i) {
        int row, col;
        for (int j = i; j < n; ++j) {
            if (r[j] == i) {
                row = j;
            }
            if (c[j] == i) {
                col = j;
            }
        }
        if (row == i && col == i) {
            continue;
        }
        ansx[cnt][0] = row;
        ansy[cnt][0] = i;
        ansx[cnt][1] = i;
        ansy[cnt][1] = col;
        ++cnt;
        swap(r[i], r[row]);
        swap(c[i], c[col]);
    }
    printf("%d\n", cnt);
    for (int i = 0; i < cnt; ++i) {
        printf("%d %d %d %d\n", 1 + ansx[i][0], 1 + ansy[i][0], 1 + ansx[i][1], 1 + ansy[i][1]);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int N=1005;
int n,R[N],C[N],top,iR[N],iC[N];
struct doors{
	int r1,c1,r2,c2;
}d[N<<1];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>R[i],iR[R[i]]=i;
	for(int i=1;i<=n;++i)cin>>C[i],iC[C[i]]=i;
	for(int i=1;i<n;++i){
		if(R[i]==i&&C[i]==i)continue;
		if(iR[i]==n&&iC[i]==n){
			d[++top]=(doors){i,n,n,i};
			R[n]=R[i],C[n]=C[i];
			continue;
		}
		if(iC[i]==n){
			d[++top]=(doors){iR[i],i,i,n};
			C[n]=C[i];iC[C[n]]=n;
			continue;
		}
		if(iR[i]==n){
			d[++top]=(doors){i,iC[i],n,i};
			R[n]=C[i];iR[R[n]]=n;
			continue;
		}
		d[++top]=(doors){iR[i],i,i,n},
		d[++top]=(doors){i,iC[i],n,i};
		int r=iR[i],c=iC[i];
		R[r]=R[n],R[n]=R[i];
		C[c]=C[n],C[n]=C[i];
		iR[R[r]]=r,iC[C[c]]=c;
		iR[R[n]]=n,iC[C[n]]=n;
	}
	cout<<top<<'\n';
	for(int i=1;i<=top;++i)
	cout<<d[i].r1<<' '<<d[i].c1<<' '<<d[i].r2<<' '<<d[i].c2<<'\n';
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int N=1005;
int n,R[N],C[N],top,iR[N],iC[N];
struct doors{
	int r1,c1,r2,c2;
}d[N<<1];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>R[i],iR[R[i]]=i;
	for(int i=1;i<=n;++i)cin>>C[i],iC[C[i]]=i;
	for(int i=1;i<n;++i){
		if(R[i]==i&&C[i]==i)continue;
		if(iR[i]==n&&iC[i]==n){
			d[++top]=(doors){i,n,n,i};
			R[n]=R[i],C[n]=C[i];
			continue;
		}
		if(iC[i]==n){
			d[++top]=(doors){iR[i],i,i,n};
			C[n]=C[i];iC[C[n]]=n;
			continue;
		}
		if(iR[i]==n){
			d[++top]=(doors){i,iC[i],n,i};
			R[n]=R[i];iR[R[n]]=n;
			continue;
		}
		d[++top]=(doors){iR[i],i,i,n},
		d[++top]=(doors){i,iC[i],n,i};
		int r=iR[i],c=iC[i];
		R[r]=R[n],R[n]=R[i];
		C[c]=C[n],C[n]=C[i];
		iR[R[r]]=r,iC[C[c]]=c;
		iR[R[n]]=n,iC[C[n]]=n;
	}
	cout<<top<<'\n';
	for(int i=1;i<=top;++i)
	cout<<d[i].r1<<' '<<d[i].c1<<' '<<d[i].r2<<' '<<d[i].c2<<'\n';
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int N=1005;
int n,R[N],C[N],top,iR[N],iC[N];
struct doors{
	int r1,c1,r2,c2;
}d[N<<1];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>R[i],iR[R[i]]=i;
	for(int i=1;i<=n;++i)cin>>C[i],iC[C[i]]=i;
	for(int i=1;i<n;++i){
		if(R[i]==i&&C[i]==i)continue;
		if(iR[i]==n&&iC[i]==n){
			d[++top]=(doors){i,n,n,i};
			R[n]=R[i],C[n]=C[i];
			iR[R[n]]=n;iC[C[n]]=n;
			continue;
		}
		if(iC[i]==n){
			d[++top]=(doors){iR[i],i,i,n};
			C[n]=C[i];iC[C[n]]=n;
			R[iR[i]]=R[i],iR[R[iR[i]]]=iR[i];
			continue;
		}
		if(iR[i]==n){
			d[++top]=(doors){i,iC[i],n,i};
			R[n]=R[i];iR[R[n]]=n;
			C[iC[i]]=C[i],iC[C[iC[i]]]=iC[i];
			continue;
		}
		d[++top]=(doors){iR[i],i,i,n},
		d[++top]=(doors){i,iC[i],n,i};
		int r=iR[i],c=iC[i];
		R[r]=R[n],R[n]=R[i];
		C[c]=C[n],C[n]=C[i];
		iR[R[r]]=r,iC[C[c]]=c;
		iR[R[n]]=n,iC[C[n]]=n;
	}
	cout<<top<<'\n';
	for(int i=1;i<=top;++i)
		cout<<d[i].r1<<' '<<d[i].c1<<' '<<d[i].r2<<' '<<d[i].c2<<'\n';
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int N=1005;
int n,R[N],C[N],top,iR[N],iC[N];
struct doors{
	int r1,c1,r2,c2;
}d[N<<1];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>R[i],iR[R[i]]=i;
	for(int i=1;i<=n;++i)cin>>C[i],iC[C[i]]=i;
	for(int i=1;i<n;++i){
		if(R[i]==i&&C[i]==i)continue;
		if(iR[i]==n&&iC[i]==n){
			d[++top]=(doors){i,n,n,i};
			R[n]=R[i],C[n]=C[i];
			iR[R[n]]=n;iC[C[n]]=n;
			continue;
		}
		if(iC[i]==n){
			d[++top]=(doors){iR[i],i,i,n};
			C[n]=C[i];iC[C[n]]=n;
			continue;
		}
		if(iR[i]==n){
			d[++top]=(doors){i,iC[i],n,i};
			R[n]=R[i];iR[R[n]]=n;
			continue;
		}
		d[++top]=(doors){iR[i],i,i,n},
		d[++top]=(doors){i,iC[i],n,i};
		int r=iR[i],c=iC[i];
		R[r]=R[n],R[n]=R[i];
		C[c]=C[n],C[n]=C[i];
		iR[R[r]]=r,iC[C[c]]=c;
		iR[R[n]]=n,iC[C[n]]=n;
	}
	cout<<top<<'\n';
	for(int i=1;i<=top;++i)
	cout<<d[i].r1<<' '<<d[i].c1<<' '<<d[i].r2<<' '<<d[i].c2<<'\n';
	return 0;
}