#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#define MAXN 1000
int rot[MAXN];
string arr[MAXN];
int N,K;
void rotate(int idx, int dir){
// 시계방향 회전
if (dir == 1){
arr[idx] = arr[idx][7]+arr[idx].substr(0,7);
}
// 반시계회전
else{
arr[idx] = arr[idx].substr(1)+arr[idx][0];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i =0 ;i<N;i ++){
cin >> arr[i];
}
cin >> K;
while (K--){
memset(rot,0,sizeof(rot));
int idx,dir;
cin >> idx >> dir;
idx--;
int tmp_dir = dir;
rot[idx] = dir;
// 오른쪽 검사
for (int i = idx+1; i< N;i++){
if ((arr[i-1][2]-'0')+(arr[i][6]-'0')==1){
rot[i] = -tmp_dir;
tmp_dir *=-1;
}
else break;
}
tmp_dir = dir;
// 왼쪽 검사
for (int i = idx-1; i >=0; i--){
if ((arr[i+1][6]-'0')+(arr[i][2]-'0') == 1){
rot[i] = -tmp_dir;
tmp_dir *= -1;
}
else break;
}
for (int i = 0; i < N; i++){
if (rot[i]==0) continue;
rotate(i,rot[i]);
}
}
int ans = 0;
for (int i =0 ;i <N; i++){
ans += arr[i][0]-'0';
}
cout << ans <<"\n";
return 0;
}