#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; vector A[15]; vector B[15]; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int m; cin>>m; vector G(28),H(28); rep(i,28) cin>>G[i]; rep(i,28) cin>>H[i]; for(int bit=0;bit<(1<<14);bit++){ int a=__builtin_popcount(bit); ll sum=0; rep(i,14){ if(bit&(1<>q; while(q--){ int k,x; cin>>k>>x; int ans=0; for(int a=0;a<=14;a++){ if(k-a<0 || k-a>14) continue; for(auto p:A[a]){ if(x>=p){ auto q=upper_bound(ALL(B[k-a]),m-p-1)-lower_bound(ALL(B[k-a]),x-p); ans+=q; } else{ auto q=upper_bound(ALL(B[k-a]),m-p-1)-B[k-a].begin(); auto r=B[k-a].end()-lower_bound(ALL(B[k-a]),m-p+x); ans+=q+r; } } } cout<