#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=2e18;
ll clamp(ll x,ll min,ll max)
{
	if(x>max)
	{
		return max;
	}
	if(x<min)
	{
		return min;
	}
	return x;
}
int main()
{
	ll n;
	cin>>n;
	ll low=-INF;
	ll high=INF;
	ll bias=0;
	for(ll i=1;i<=n;i++)
	{
		ll a,t;
		cin>>a>>t;
		if(1==t)
		{
			low+=a;
			high+=a;
			bias+=a;
		}
		else if(2==t){
				low=max(low,a);
				high=max(high,a);
			}else{
				low=min(low,a);
				high=min(high,a);
			}
	}
	ll q;
	cin>>q;
	for(ll i=1;i<=q;i++)
	{
		ll x;
		cin>>x;
		cout<<clamp(x+bias,low,high)<<"\n";
	}
}
#include <bits/stdc++.h>
using namespace std;
#define fo(a,b) for(int64_t a=0;a<b;a++)
#define sor(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define ll int64_t
#define mod 1000000007
#define vl vector<int64_t>
#define vc vector<char>
#define vs vector<string>
#define np(vec) next_permutation(vec.begin(),vec.end())==true
#define en endl
#define vvl vector<vector<int64_t>>
#define stl stack<int64_t>
#define qul que<int64_t>
#define ms(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pq priority_queue
#define pql priority_queue<int64_t>
#define pu push
#define pop pop()
#define top top()
#define pqr priority_queue<ll,vector<ll>,greater<ll>>
#define co cout
#define ci cin
#define wh while
#define size size()
#define front front()

int main() {
  ll n;
  ll a=0;
  char c;
  cin>>n;
  vl v1;
  vl v2;
  fo(i,n){
    cin>>c;
    if(c=='0'){v1.push_back(i);}
  }
  fo(i,n){
    cin>>c;
    if(c=='0'){v2.push_back(i);}
  }
 if(v1.size!=v2.size){cout<<-1<<en;}
  else{
    sor(v1);
    sor(v2);
    fo(i,v1.size){
      if(v1.at(i)!=v2.at(i)){a++;}
    }
    cout<<a<<en;
  }
}