#include using lint = long long; int main(){ lint n; std::cin >> n; std::vector l(5), r(5); std::vector> t(5, std::vector(n)); for(lint i = 0; i < 5; ++i){ std::cin >> l[i] >> r[i]; --l[i]; --r[i]; for(lint j = l[i]; j < n - 1; ++j){ lint a = 0; if(j < r[i]){ std::cin >> a; } t[i][j + 1] = t[i][j] + a; } } /* for(lint i = 0; i < n; ++i){ std::cout << std::setw(2) << i << " "; } std::cout << std::endl; for(lint i = 0; i < 5; ++i){ for(lint j = 0; j < n; ++j){ std::cout << std::setw(2) << t[i][j] << " "; } std::cout << std::endl; } */ lint m; std::cin >> m; std::vector> s(5); for(lint i = 0; i < m; ++i){ lint b, st; std::cin >> b >> st; --b; s[b].push_back(st); } for(lint i = 0; i < 5; ++i){ std::sort(s[i].begin(), s[i].end()); } /* for(lint i = 0; i < 5; ++i){ for(lint j : s[i]){ std::cout << j << " "; } std::cout << std::endl; } */ lint ans = 0; for(lint i = 0; i < 5; ++i){ for(lint j = i + 1; j < 5; ++j){ if(i == j) continue; lint from = std::max(l[i], l[j]); lint to = std::min(r[i], r[j]); if(from > to) continue; /* printf("%d %d\n", i, j); printf("t[%2d][%2d] = %2d\n", i, from, t[i][from]); printf("t[%2d][%2d] = %2d\n", j, from, t[j][from]); printf("t[%2d][%2d] = %2d\n", i, to, t[i][to]); printf("t[%2d][%2d] = %2d\n", j, to, t[j][to]); */ lint index1 = 0, index2 = 0; for(lint k : s[i]){ while(index1 < s[j].size() && k + t[i][from] >= s[j][index1] + t[j][from]){ // printf("%d + %d >= %d + %d\n", k, t[i][from], s[j][index1], t[j][from]); ++index1; } while(index2 < s[j].size() && k + t[i][to] > s[j][index2] + t[j][to]){ // printf("%d + %d > %d + %d\n", k, t[i][to], s[j][index2], t[j][to]); ++index2; } // std::cout << k << " " << index1 << " " << index2 << std::endl; ans += index1 - index2; } } } std::cout << ans << std::endl; }