#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, 0);
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {
        while(cur < i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);
        Q.push(dp[i]+C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    deque<pair<int, ll>> Q;
    for (int i = 0; i < m; ++i) {
        while(!Q.empty() && Q.back().second > dp[i]+C[i]) Q.pop_back();
        Q.emplace_back(r[i], dp[i]+C[i]);
        while(!Q.empty() && Q.front().first < i) Q.pop_front();
        dp[i+1] = min(dp[i+1], dp[l[i]]+C[i]);
        if(!Q.empty()) dp[i+1] = min(dp[i+1], Q.front().second);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    deque<pair<int, ll>> Q;
    for (int i = 0; i < m; ++i) {
        while(!Q.empty() && Q.back().second > dp[i]+C[i]) Q.pop_back();
        Q.emplace_back(r[i]+1, dp[i]+C[i]);
        while(!Q.empty() && Q.front().first < i) Q.pop_front();
        dp[i+1] = min(dp[i+1], dp[l[i]]+C[i]);
        if(!Q.empty()) dp[i+1] = min(dp[i+1], Q.front().second);

    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {

        while(cur <= i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);
        Q.push(dp[i]+C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    deque<pair<int, ll>> Q;
    for (int i = 0; i < m; ++i) {
        dp[i+1] = dp[l[i]]+C[i];
        while(!Q.empty() && Q.back().second > dp[i]+C[i]) Q.pop_back();
        while(!Q.empty() && Q.front().first < i) Q.pop_front();
        if(!Q.empty()) dp[i+1] = min(dp[i+1], Q.front().second);
        Q.emplace_back(r[i], dp[i]+C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    deque<pair<int, ll>> Q;
    for (int i = 0; i < m; ++i) {
        dp[i+1] = dp[l[i]]+C[i];
        while(!Q.empty() && Q.back().first < i) Q.pop_back();
        if(!Q.empty()) dp[i+1] = min(dp[i+1], Q.back().second);
        if (Q.empty() || Q.back().second >= dp[i]+C[i]) Q.emplace_back(r[i], dp[i]+C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, 0);
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {
        Q.push(dp[i]+C[i]);
        while(cur < i && r[cur] <= i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);

    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {

        while(cur <= i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);
        Q.push(dp[i]+C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {
        Q.push(dp[i]+C[i]);
        while(cur <= i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {

        while(cur < i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[r[i]+1] = min(dp[r[i]+1], dp[i]+C[i]);
        dp[i+1] = min({dp[i+1], Q.fold(), dp[l[i]] + C[i]});
        Q.push(dp[i]+C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    deque<pair<int, ll>> Q;
    for (int i = 0; i < m; ++i) {
        while(!Q.empty() && Q.back().second >= dp[i]+C[i]) Q.pop_back();
        Q.emplace_back(r[i], dp[i]+C[i]);
        while(!Q.empty() && Q.front().first < i) Q.pop_front();
        dp[i+1] = min(dp[i+1], dp[l[i]]+C[i]);
        if(!Q.empty()) dp[i+1] = min(dp[i+1], Q.front().second);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*7+208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    deque<pair<int, ll>> Q;
    for (int i = 0; i < m; ++i) {
        dp[i+1] = dp[l[i]]+C[i];
        while(!Q.empty() && Q.front().first < i) Q.pop_front();
        if(!Q.empty()) dp[i+1] = min(dp[i+1], Q.front().second);
        while(!Q.empty() && Q.back().second >= dp[i]+C[i]) Q.pop_back();
        Q.emplace_back(r[i], dp[i]+C[i]);

    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, INF<ll>);
    dp[0] = 0;
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {
        Q.push(dp[i]+C[i]);
        while(cur <= i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, 0);
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {
        Q.push(dp[i]+C[i]);
        while(cur <= i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);
    }
    cout << dp.back() << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return INF<ll>; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        v[i].resize(k);
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].first = a;
        }
        for (int j = 0; j < k; ++j) {
            int a;
            scanf("%d", &a);
            v[i][j].second = a;
        }
    }
    vector<int> A(m);
    vector<ll> C(m);
    int q;
    cin >> q;
    m = 0;
    for (int i = 0; i < q; ++i) {
        int id, mul;
        scanf("%d %d", &id, &mul);
        for (auto &&j : v[id-1]) {
            A[m] = j.first;
            C[m] = (ll)j.second*mul;
            m++;
        }
    }
    vector<int> l(m), r(m);
    {
        stack<pair<int, int>> Q;
        for (int i = 0; i < m; ++i) {
            int lb = max(0, i-A[i]+1);
            while(!Q.empty() && Q.top().second >= lb){
                lb = min(lb, Q.top().first);
                Q.pop();
            }
            l[i] = lb;
            Q.emplace(l[i], i);
        }
        while(!Q.empty()) Q.pop();
        for (int i = m-1; i >= 0; --i) {
            int rb = min(m-1, i+A[i]-1);
            while(!Q.empty() && Q.top().second <= rb){
                rb = max(rb, Q.top().first);
                Q.pop();
            }
            r[i] = rb;
            Q.emplace(r[i], i);
        }
        while(!Q.empty()) Q.pop();
    }
    vector<ll> dp(m+1, 0);
    int cur = 0;
    SWAG<Monoid> Q;
    for (int i = 0; i < m; ++i) {
        Q.push(dp[i]+C[i]);
        while(cur < i && r[cur] < i){
            cur++;
            Q.pop();
        }
        dp[i+1] = min(Q.fold(), dp[l[i]] + C[i]);

    }
    cout << dp.back() << "\n";
    return 0;
}


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

typedef long long ll;

const int MAXN=250005;
const int MAXM=10000005;
const ll INF=(1LL<<60)-1;

vector<int> ta[MAXN],tc[MAXN];

int a[MAXM];
ll c[MAXM],dp[MAXM];

void init(int n,int m)
{
    for(int i=1;i<=n;i++)
    {
        int k;
        scanf("%d",&k);
        ta[i].resize(k);
        for(int j=0;j<k;j++)
            scanf("%d",&ta[i][j]);
        tc[i].resize(k);
        for(int j=0;j<k;j++)
            scanf("%d",&tc[i][j]);
    }
    int q;
    scanf("%d",&q);
    for(int i=1,s=0;i<=q;i++)
    {
        int id,mul;
        scanf("%d%d",&id,&mul);
        int k=(int)ta[id].size();
        for(int j=0;j<k;j++)
            a[++s]=ta[id][j],c[s]=1LL*mul*tc[id][j];
    }
}

stack<pair<int,ll> > stk_1;
stack<ll> stk_2;

int pop_cnt[MAXM];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    init(n,m);
    stk_1.push({m+1,0});
    stk_2.push(INF);
    for(int i=m;i>=1;i--)
    {
        int r=min(m,i+a[i]-1);
        while(r>=stk_1.top().first)stk_1.pop();
        pop_cnt[stk_1.top().first-1]++;
        stk_1.push({i,0});
    }
    while(!stk_1.empty())stk_1.pop();
    stk_1.push({0,0});
    for(int i=1;i<=m;i++)
    {
        int l=max(1,i-a[i]+1);
        ll val=dp[i-1];
        while(l<=stk_1.top().first)
        {
            val=min(val,stk_1.top().second);
            stk_1.pop();
        }
        stk_1.push({i,val});
        stk_2.push(min(stk_2.top(),dp[i-1]+c[i]));
        dp[i]=min(val+c[i],stk_2.top());
        for(int j=0;j<pop_cnt[i];j++)
            stk_2.pop();
    }
    printf("%lld\n",dp[m]);
    return 0;
}


//Zory-2019
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
#include<deque>
using namespace std;

namespace mine
{
	typedef long long ll;
	#define pr pair<ll,ll>
	#define FR first
	#define SE second
	#define MP make_pair

	void chmin(ll &x,ll y) {x=x<y?x:y;}
	const int MAX_N=310000,MAX_M=1e7+10;
	vector<pr> in[MAX_N];
	pr a[MAX_M];
	ll f[MAX_M],lf[MAX_M],rf[MAX_M];
	int tot=0;
	pr tmp[MAX_M];
	void main()
	{
		int n,m;scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			int k;scanf("%d",&k);in[i].resize(k);
			for(int j=0;j<k;j++) scanf("%lld",&in[i][j].FR);
			for(int j=0;j<k;j++) scanf("%lld",&in[i][j].SE);
		}
		int q;scanf("%d",&q);
		while(q--)
		{
			int id,mul;scanf("%d%d",&id,&mul);
			for(int t=0;t<(int)in[id].size();t++) a[++tot]=in[id][t],a[tot].SE*=mul;
		}

		int cnt=0;lf[1]=1;tmp[++cnt]=MP(lf[1],1);
		for(int i=2;i<=tot;i++)
		{
			lf[i]=i;
			while(cnt>0 and tmp[cnt].SE>=i-a[i].FR+1) lf[i]=tmp[cnt].FR,cnt--;
			while(cnt>0 and tmp[cnt].FR>=lf[i]) cnt--;
			tmp[++cnt]=MP(lf[i],i);
		}
		cnt=0;rf[tot]=tot;tmp[++cnt]=MP(rf[tot],tot);
		for(int i=tot-1;i>=1;i--)
		{
			rf[i]=i;
			while(cnt>0 and tmp[cnt].SE<=i+a[i].FR-1) rf[i]=tmp[cnt].FR,cnt--;
			while(cnt>0 and tmp[cnt].FR<=rf[i]) cnt--;
			tmp[++cnt]=MP(rf[i],i);
		}

		memset(f,63,sizeof f);
		cnt=0;f[0]=0;tmp[++cnt]=MP(f[0],0);
		for(int i=1;i<=tot;i++)
		{
			ll tt=1ll<<62;
			if(cnt>0 and tmp[cnt].SE>=lf[i]-1) chmin(tt,tmp[cnt].FR);
			while(cnt>1 and tmp[cnt-1].SE>=lf[i]-1) chmin(tt,tmp[cnt-1].FR),cnt--;
			if(tt!=(1ll<<62)) f[i]=min(f[i],tt+a[i].SE);
			while(cnt>0 and tmp[cnt].FR>=f[i]) cnt--;
			tmp[++cnt]=MP(f[i],i);//printf("f[%d]=%lld\n",i,f[i]);
			chmin(f[rf[i]],f[i-1]+a[i].SE);
		}
		printf("%lld",f[tot]);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


//Zory-2019
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
#include<deque>
using namespace std;

namespace mine
{
	typedef long long ll;
	#define pr pair<ll,ll>
	#define FR first
	#define SE second
	#define MP make_pair

	void chmin(ll &x,ll y) {x=x<y?x:y;}
	const int MAX_N=310000,MAX_M=1e7+10;
	vector<pr> in[MAX_N];
	pr a[MAX_M];
	ll f[MAX_M],lf[MAX_M],rf[MAX_M];
	int tot=0;
	pr tmp[MAX_M];
	void main()
	{
		int n,m;scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			int k;scanf("%d",&k);in[i].resize(k);
			for(int j=0;j<k;j++) scanf("%lld",&in[i][j].FR);
			for(int j=0;j<k;j++) scanf("%lld",&in[i][j].SE);
		}
		int q;scanf("%d",&q);
		while(q--)
		{
			int id,mul;scanf("%d%d",&id,&mul);
			for(int t=0;t<(int)in[id].size();t++) a[++tot]=in[id][t],a[tot].SE*=mul;
		}

		int cnt=0;lf[1]=1;tmp[++cnt]=MP(lf[1],1);
		for(int i=2;i<=tot;i++)
		{
			lf[i]=i;
			while(cnt>0 and tmp[cnt].SE>=i-a[i].FR+1) lf[i]=tmp[cnt].FR,cnt--;
			while(cnt>0 and tmp[cnt].FR>=lf[i]) cnt--;
			tmp[++cnt]=MP(lf[i],i);
		}
		cnt=0;rf[tot]=tot;tmp[++cnt]=MP(rf[tot],tot);
		for(int i=tot-1;i>=1;i--)
		{
			rf[i]=i;
			while(cnt>0 and tmp[cnt].SE<=i+a[i].FR-1) rf[i]=tmp[cnt].FR,cnt--;
			while(cnt>0 and tmp[cnt].FR<=rf[i]) cnt--;
			tmp[++cnt]=MP(rf[i],i);
		}

		memset(f,63,sizeof f);
		cnt=0;f[1]=a[1].SE;tmp[++cnt]=MP(f[1],1);chmin(f[rf[1]],a[1].SE);
		for(int i=2;i<=tot;i++)
		{
			ll tt=f[0];
			if(cnt>0 and tmp[cnt].SE>=lf[i]-1) chmin(tt,tmp[cnt].FR);
			while(cnt>1 and tmp[cnt-1].SE>=lf[i]-1) chmin(tt,tmp[cnt-1].FR),cnt--;
			f[i]=min(f[i],tt+a[i].SE);while(cnt>0 and tmp[cnt].FR>=f[i]) cnt--;
			tmp[++cnt]=MP(f[i],i);
			chmin(f[rf[i]],f[i-1]+a[i].SE);
		}
		printf("%lld",f[tot]);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


//Zory-2019
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
#include<deque>
using namespace std;

namespace mine
{
	typedef long long ll;
	#define pr pair<ll,ll>
	#define FR first
	#define SE second
	#define MP make_pair

	void chmin(ll &x,ll y) {x=x<y?x:y;}
	const int MAX_N=310000,MAX_M=1e7+10;
	vector<pr> in[MAX_N];
	pr a[MAX_M];
	ll f[MAX_M],lf[MAX_M],rf[MAX_M];
	int tot=0;
	pr tmp[MAX_M];
	void main()
	{
		int n,m;scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			int k;scanf("%d",&k);in[i].resize(k);
			for(int j=0;j<k;j++) scanf("%lld",&in[i][j].FR);
			for(int j=0;j<k;j++) scanf("%lld",&in[i][j].SE);
		}
		int q;scanf("%d",&q);
		while(q--)
		{
			int id,mul;scanf("%d%d",&id,&mul);
			for(int t=0;t<(int)in[id].size();t++) a[++tot]=in[id][t],a[tot].SE*=mul;
		}

		int cnt=0;lf[1]=1;tmp[++cnt]=MP(lf[1],1);
		for(int i=2;i<=tot;i++)
		{
			lf[i]=i;
			while(cnt>0 and tmp[cnt].SE>=i-a[i].FR+1) lf[i]=tmp[cnt].FR,cnt--;
			while(cnt>0 and tmp[cnt].FR>=lf[i]) cnt--;
			tmp[++cnt]=MP(lf[i],i);
			printf("lf=%lld\n",lf[i]);
		}
		cnt=0;rf[tot]=tot;tmp[++cnt]=MP(rf[tot],tot);
		for(int i=tot-1;i>=1;i--)
		{
			rf[i]=i;
			while(cnt>0 and tmp[cnt].SE<=i+a[i].FR-1) rf[i]=tmp[cnt].FR,cnt--;
			while(cnt>0 and tmp[cnt].FR<=rf[i]) cnt--;
			tmp[++cnt]=MP(rf[i],i);
		}

		memset(f,63,sizeof f);
		cnt=0;f[0]=0;tmp[++cnt]=MP(f[0],0);
		for(int i=1;i<=tot;i++)
		{
			ll tt=1ll<<62;
			if(cnt>0 and tmp[cnt].SE>=lf[i]-1) chmin(tt,tmp[cnt].FR);
			while(cnt>1 and tmp[cnt-1].SE>=lf[i]-1) chmin(tt,tmp[cnt-1].FR),cnt--;
			if(tt!=(1ll<<62)) f[i]=min(f[i],tt+a[i].SE);
			while(cnt>0 and tmp[cnt].FR>=f[i]) cnt--;
			tmp[++cnt]=MP(f[i],i);//printf("f[%d]=%lld\n",i,f[i]);
			chmin(f[rf[i]],f[i-1]+a[i].SE);
		}
		printf("%lld",f[tot]);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define rank rank228
#define y1 y1228
#define pb push_back
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
const string FILENAME = "input";
const int MAXN = 250001;
const int MAXM = 10000001;

int n, m;
int k[MAXN];
vector<int> a[MAXN], c[MAXN];
int h[MAXM];
long long cost[MAXM];
int left[MAXM];
int right[MAXM];
long long fen[MAXM];
long long dp[MAXM];

void dec(int i, long long val) {
	for (; i < m; i |= (i + 1)) {
		chkmin(fen[i], val);
	}
}

long long getmin(int i) {
	long long res = 1e18;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmin(res, fen[j]);
	}
	return res;
}

void inc(int i, int val) {
	for (; i < m; i |= (i + 1)) {
		chkmax(fen[i], val);
	}
}

int getmax(int i) {
	int res = -1;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmax(res, fen[j]);
	}
	return res;
}

// vector<int> death[MAXM];
// long long d[MAXM * 4];

// void change(int i, long long x) {
// 	d[i] = x;
// 	while (i >> 1 > 0) {
// 		i >>= 1;
// 		d[i] = min(d[i * 2], d[i * 2 + 1]);
// 	}
// }

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   // read(FILENAME);
  	cin >> n >> m;
  	for (int i = 0; i < n; i++) {
  		cin >> k[i];
  		a[i].resize(k[i]);
  		c[i].resize(k[i]);
  		for (auto &x: a[i]) {
  			cin >> x;
  		}
  		for (auto &x: c[i]) {
  			cin >> x;
  		}
  	}
  	int q;
  	cin >> q;
  	int uk = 0;
  	for (int i = 0; i < q; i++) {
  		int id, mul;
  		cin >> id >> mul;
  		id--;
  		for (int j = 0; j < k[id]; j++) {
  			h[uk] = a[id][j];
  			cost[uk] = 1LL * c[id][j] * mul;
  			uk++;
  		}
  	}
  	assert(uk == m);
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e9;
  	}
  	for (int i = 0; i < m; i++) {
  		if (i < h[i]) {
  			left[i] = -1;
  		} else if (h[i] == 1) {
  			left[i] = i - 1;
  		} else {
  			left[i] = min(i - h[i], (int)getmin(m - (i - h[i] + 1) - 1));
  			chkmax(left[i], -1);
  		}
  		dec(m - i - 1, left[i]);
  	}
  	for (int i = 0; i < m; i++) {
  		fen[i] = -1;
  	}
  	for (int i = m - 1; i >= 0; i--) {
  		if (m - 1 - i < h[i]) {
  			right[i] = m;
  		} else if (h[i] == 1) {
  			right[i] = i + 1;
  		} else {
  			right[i] = max(i + h[i], getmax(i + h[i] - 1));
  			chkmin(right[i], m);
  		}
  		inc(i, right[i]);
  	}
  	for (int i = 0; i <= m; i++) {
  		dp[i] = 1e18;
  	}
  	int ss = 1;
  	while (ss < m) {
  		ss <<= 1;
  	}
  	// for (int i = 0; i < 2 * ss; i++) {
  	// 	d[i] = 1e18;
  	// }
  	dp[0] = 0;
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e18;
  	}
  	for (int i = 0; i < m; i++) {
  		// for (auto x: death[i]) {
  		// 	change(ss + x, 1e18);
  		// }
  		//chkmin(dp[i], d[1]);
  		dec(m - i - 1, dp[i]);
  		//cout << left[i] << ' ' << i << endl;
  		chkmin(dp[i + 1], cost[i] + (left[i] == -1 ? 0LL: getmin(m - left[i] - 2)));
  		chkmin(dp[i + 1], cost[i] + dp[i]);
  	//	death[right[i] + 1].pb(i);
  		chkmin(dp[right[i]], cost[i] + dp[i]);
  	//	change(ss + i, cost[i] + dp[i]);
  	}
  	cout << dp[m] << endl;
    return 0;
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define rank rank228
#define y1 y1228
#define pb push_back
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
const string FILENAME = "input";
const int MAXN = 250001;
const int MAXM = 10000001;

int n, m;
int k[MAXN];
vector<int> a[MAXN], c[MAXN];
int h[MAXM];
long long cost[MAXM];
int left[MAXM];
int right[MAXM];
long long fen[MAXM];
long long dp[MAXM];

void dec(int i, long long val) {
	for (; i < m; i |= (i + 1)) {
		chkmin(fen[i], val);
	}
}

long long getmin(int i) {
	long long res = 1e18;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmin(res, fen[j]);
	}
	return res;
}

void inc(int i, int val) {
	for (; i < m; i |= (i + 1)) {
		chkmax(fen[i], val);
	}
}

int getmax(int i) {
	int res = -1;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmax(res, fen[j]);
	}
	return res;
}

vector<int> death[MAXM];
long long d[MAXM * 4];

void change(int i, long long x) {
	d[i] = x;
	while (i >> 1 > 0) {
		i >>= 1;
		d[i] = min(d[i * 2], d[i * 2 + 1]);
	}
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   // read(FILENAME);
  	cin >> n >> m;
  	for (int i = 0; i < n; i++) {
  		cin >> k[i];
  		a[i].resize(k[i]);
  		c[i].resize(k[i]);
  		for (auto &x: a[i]) {
  			cin >> x;
  		}
  		for (auto &x: c[i]) {
  			cin >> x;
  		}
  	}
  	int q;
  	cin >> q;
  	int uk = 0;
  	for (int i = 0; i < q; i++) {
  		int id, mul;
  		cin >> id >> mul;
  		id--;
  		for (int j = 0; j < k[id]; j++) {
  			h[uk] = a[id][j];
  			cost[uk] = 1LL * c[id][j] * mul;
  			uk++;
  		}
  	}
  	assert(uk == m);
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e9;
  	}
  	for (int i = 0; i < m; i++) {
  		if (i < h[i]) {
  			left[i] = -1;
  		} else if (h[i] == 1) {
  			left[i] = i - 1;
  		} else {
  			left[i] = min(i - h[i], (int)getmin(m - (i - h[i] + 1) - 1));
  			chkmax(left[i], -1);
  		}
  		dec(m - i - 1, left[i]);
  	}
  	for (int i = 0; i < m; i++) {
  		fen[i] = -1;
  	}
  	for (int i = m - 1; i >= 0; i--) {
  		if (m - 1 - i < h[i]) {
  			right[i] = m;
  		} else if (h[i] == 1) {
  			right[i] = i + 1;
  		} else {
  			right[i] = max(i + h[i], getmax(i + h[i] - 1));
  			chkmin(right[i], m);
  		}
  		inc(i, right[i]);
  	}
  	for (int i = 0; i <= m; i++) {
  		dp[i] = 1e18;
  	}
  	int ss = 1;
  	while (ss < m) {
  		ss <<= 1;
  	}
  	for (int i = 0; i < 2 * ss; i++) {
  		d[i] = 1e18;
  	}
  	dp[0] = 0;
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e18;
  	}
  	for (int i = 0; i < m; i++) {
  		for (auto x: death[i]) {
  			change(ss + x, 1e18);
  		}
  		chkmin(dp[i], d[1]);
  		dec(m - i - 1, dp[i]);
  		chkmin(dp[i + 1], cost[i] + (left[i] == -1 ? 0LL: getmin(m - left[i] - 1)));
  		death[right[i] + 1].pb(i);
  		change(ss + i, cost[i] + dp[i]);
  	}
  	cout << dp[m] << endl;
    return 0;
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define rank rank228
#define y1 y1228
#define pb push_back
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
const string FILENAME = "input";
const int MAXN = 250001;
const int MAXM = 10000001;

int n, m;
int k[MAXN];
vector<int> a[MAXN], c[MAXN];
int h[MAXM];
long long cost[MAXM];
int left[MAXM];
int right[MAXM];
long long fen[MAXM];
long long dp[MAXM];

void dec(int i, long long val) {
	for (; i < m; i |= (i + 1)) {
		chkmin(fen[i], val);
	}
}

long long getmin(int i) {
	long long res = 1e18;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmin(res, fen[j]);
	}
	return res;
}

void inc(int i, int val) {
	for (; i < m; i |= (i + 1)) {
		chkmax(fen[i], val);
	}
}

int getmax(int i) {
	int res = -1;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmax(res, fen[j]);
	}
	return res;
}

vector<int> death[MAXM];
long long d[MAXM * 4];

void change(int i, long long x) {
	d[i] = x;
	while (i >> 1 > 0) {
		i >>= 1;
		d[i] = min(d[i * 2], d[i * 2 + 1]);
	}
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   // read(FILENAME);
  	cin >> n >> m;
  	for (int i = 0; i < n; i++) {
  		cin >> k[i];
  		a[i].resize(k[i]);
  		c[i].resize(k[i]);
  		for (auto &x: a[i]) {
  			cin >> x;
  		}
  		for (auto &x: c[i]) {
  			cin >> x;
  		}
  	}
  	int q;
  	cin >> q;
  	int uk = 0;
  	for (int i = 0; i < q; i++) {
  		int id, mul;
  		cin >> id >> mul;
  		id--;
  		for (int j = 0; j < k[id]; j++) {
  			h[uk] = a[id][j];
  			cost[uk] = 1LL * c[id][j] * mul;
  			uk++;
  		}
  	}
  	assert(uk == m);
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e9;
  	}
  	for (int i = 0; i < m; i++) {
  		if (i < h[i]) {
  			left[i] = -1;
  		} else if (h[i] == 1) {
  			left[i] = i - 1;
  		} else {
  			left[i] = min(i - h[i], getmin(m - (i - h[i] + 1) - 1));
  			chkmax(left[i], -1);
  		}
  		dec(m - i - 1, left[i]);
  	}
  	for (int i = 0; i < m; i++) {
  		fen[i] = -1;
  	}
  	for (int i = m - 1; i >= 0; i--) {
  		if (m - 1 - i < h[i]) {
  			right[i] = m;
  		} else if (h[i] == 1) {
  			right[i] = i + 1;
  		} else {
  			right[i] = max(i + h[i], getmax(i + h[i] - 1));
  			chkmin(right[i], m);
  		}
  		inc(i, right[i]);
  	}
  	for (int i = 0; i <= m; i++) {
  		dp[i] = 1e18;
  	}
  	int ss = 1;
  	while (ss < m) {
  		ss <<= 1;
  	}
  	for (int i = 0; i < 2 * ss; i++) {
  		d[i] = 1e18;
  	}
  	dp[0] = 0;
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e18;
  	}
  	for (int i = 0; i < m; i++) {
  		for (auto x: death[i]) {
  			change(ss + x, 1e18);
  		}
  		chkmin(dp[i], d[1]);
  		dec(m - i - 1, dp[i]);
  		chkmin(dp[i + 1], cost[i] + (left[i] == -1 ? 0LL: getmin(m - left[i] - 1)));
  		death[right[i] + 1].pb(i);
  		change(ss + i, cost[i] + dp[i]);
  	}
  	cout << dp[m] << endl;
    return 0;
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define rank rank228
#define y1 y1228
#define pb push_back
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
const string FILENAME = "input";
const int MAXN = 250001;
const int MAXM = 10000001;

int n, m;
int k[MAXN];
vector<int> a[MAXN], c[MAXN];
int h[MAXM];
long long cost[MAXM];
int left[MAXM];
int right[MAXM];
int fen[MAXM];
long long dp[MAXM];

void dec(int i, int val) {
	for (; i < m; i |= (i + 1)) {
		chkmin(fen[i], val);
	}
}

int getmin(int i) {
	int res = 1e9;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmin(res, fen[j]);
	}
	return res;
}

void inc(int i, int val) {
	for (; i < m; i |= (i + 1)) {
		chkmax(fen[i], val);
	}
}

int getmax(int i) {
	int res = -1;
	for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
		chkmax(res, fen[j]);
	}
	return res;
}

vector<int> death[MAXM];
long long d[MAXM * 4];

void change(int i, long long x) {
	d[i] = x;
	while (i >> 1 > 0) {
		i >>= 1;
		d[i] = min(d[i * 2], d[i * 2 + 1]);
	}
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   // read(FILENAME);
  	cin >> n >> m;
  	for (int i = 0; i < n; i++) {
  		cin >> k[i];
  		a[i].resize(k[i]);
  		c[i].resize(k[i]);
  		for (auto &x: a[i]) {
  			cin >> x;
  		}
  		for (auto &x: c[i]) {
  			cin >> x;
  		}
  	}
  	int q;
  	cin >> q;
  	int uk = 0;
  	for (int i = 0; i < q; i++) {
  		int id, mul;
  		cin >> id >> mul;
  		id--;
  		for (int j = 0; j < k[id]; j++) {
  			h[uk] = a[id][j];
  			cost[uk] = 1LL * c[id][j] * mul;
  			uk++;
  		}
  	}
  	assert(uk == m);
  	for (int i = 0; i < m; i++) {
  		fen[i] = 1e9;
  	}
  	for (int i = 0; i < m; i++) {
  		if (i < h[i]) {
  			left[i] = -1;
  		} else if (h[i] == 1) {
  			left[i] = i - 1;
  		} else {
  			left[i] = min(i - h[i], getmin(m - (i - h[i] + 1) - 1));
  			chkmax(left[i], -1);
  		}
  		dec(m - i - 1, left[i]);
  	}
  	for (int i = 0; i < m; i++) {
  		fen[i] = -1;
  	}
  	for (int i = m - 1; i >= 0; i--) {
  		if (m - 1 - i < h[i]) {
  			right[i] = m;
  		} else if (h[i] == 1) {
  			right[i] = i + 1;
  		} else {
  			right[i] = max(i + h[i], getmax(i + h[i] - 1));
  			chkmin(right[i], m);
  		}
  		inc(i, right[i]);
  	}
  	for (int i = 0; i <= m; i++) {
  		dp[i] = 1e18;
  	}
  	int ss = 1;
  	while (ss < m) {
  		ss <<= 1;
  	}
  	for (int i = 0; i < 2 * ss; i++) {
  		d[i] = 1e18;
  	}
  	dp[0] = 0;
  	for (int i = 0; i < m; i++) {
  		for (auto x: death[i]) {
  			change(ss + x, 1e18);
  		}
  		chkmin(dp[i], d[1]);
  		chkmin(dp[i + 1], cost[i] + (left[i] == -1 ? 0LL: dp[left[i]]));
  		death[right[i] + 1].pb(i);
  		change(ss + i, cost[i] + dp[i]);
  	}
  	cout << dp[m] << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define ll long long
#define vi vector<int>
#define V vector
#define pb push_back
#define PB pop_back
#define pii pair<ll,ll>
#define xx first
#define yy second

const int N = 2.5e5+100;
const int NN = 1e7+100;

vector<pii> block[N];
ll dp[NN];

int main() {
	IOS;
	int n,m; cin >> n >> m;
	for (int i=1;i<=n;i++) {
		int k; cin >> k;
		block[i].resize(k);
		for (int j=0;j<k;j++) cin >> block[i][j].xx; // height
		for (int j=0;j<k;j++) cin >> block[i][j].yy; // cost
	}
	stack<pair<int,pii>> tolef; // pos, reach,cost
	stack<pii> tory; // reach,cost
	cin >> n;
	for (int at=0;at<m;) {
		int i,mul; cin >> i >> mul;
		for (pii &dom : block[i]) {
			ll h,c; tie(h,c) = dom; c *= mul; ++at;
			// front kick
			dp[at] = c+dp[at-1];
			// not in reach anymore
			if (tory.size() && tory.top().xx == at) tory.pop();
			// kicked from right
			ll reach = at + h;
			if (tory.size() && tory.top().xx <= reach) {
				pii x = tory.top(); tory.pop();
				x.xx = reach;
				if (x.yy < dp[at]) dp[at] = x.yy;
				else x.yy = dp[at];
				while (tory.size() && tory.top().xx <= reach) tory.pop();
				tory.push(x);
			} else tory.push({reach,dp[at]});
			// kick left
			reach = max(0LL,at - h);
			if (tolef.size() && tolef.top().xx > reach) {
				reach = min(reach,tolef.top().yy.xx); tolef.pop();
			}
			dp[at] = min(dp[at],dp[reach]+c);
			tolef.push({at,{reach,c}});
		}
	}
	cout << dp[m] << '\n';
}


#include <bits/stdc++.h>

using namespace std;

#define xx first
#define yy second
#define ll long long
#define pll pair<ll,ll>

const int N  = 2.5e5 + 500;
const int NN = 1e7 + 50;

vector<pll> block[N];
ll dp[NN];
stack<pll> lef; // <pos,cost>
stack<pll> ry; // <reach,cost>

int main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	ll n,m; cin >> n >> m;
	for (int i=1;i<=n;i++) {
		int k; cin >> k;
		block[i].resize(k);
		for (int j=0;j<k;j++) cin >> block[i][j].xx; // height
		for (int j=0;j<k;j++) cin >> block[i][j].yy; // cost
	}
	cin >> n; // number of blocks
	for (ll at=0;at<m;) {
		int bl; ll mul; cin >> bl >> mul;
		for (pll& dom : block[bl]) {
			ll h,c; tie(h,c) = dom; c *= mul; ++at;
			ll lcost = dp[at-1]; // init cost
			ll reach = at - h; // left reach
			while (lef.size() && lef.top().xx > reach) { // in reach of cur
				lcost = min(lcost,lef.top().yy);
				lef.pop();
			}
			lef.push({at,lcost});
			lcost += c; // total cost

			reach = at + h; // now right reach
			while (ry.size() && ry.top().xx<=at) ry.pop(); // not in reach of these
			ll rcost = dp[at-1] + c; // total cost
			while (ry.size() && ry.top().xx <= reach) { // we can extend those
				rcost = min(rcost,ry.top().yy);
				ry.pop();
			}
			if (ry.empty()) ry.push({reach,rcost});
			else {
				if (ry.top().yy <= rcost) // we already have better range and cost
					rcost = ry.top().yy;
				else ry.push({reach,rcost});
			}

#ifdef debug
			cout << '<' << lcost << ',' << rcost << "> ";
#endif
			dp[at] = min(lcost,rcost);
		}
	}
	cout << dp[m] << '\n';
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

void reverse_string(char *str);
void parsing(char arg[]);

int main(int argc, char *argv[])
{
	int z;
	scanf("%d", &z);
	unsigned char string[256];
	while(z--)
	{
		scanf("%s",&string);
		if(isdigit(string[0])==0)
		{
			parsing(string);
		}
	}
	return 0;
}

void parsing(char arg[])
{
	int change = 0;
	int current;
	int pos[3]={0,0,0};
	int previous = 0; //0 char, 1 number
	char dest1[255];
	char dest2[255];
	unsigned int i = 0;
	for(i;arg[i]!='\0';i++)
	{
		current = arg[i]-'A';
		if(current>=0 && previous!=0)
		{
			pos[change++]=i;
			previous=0;
		}
		if(current<0 &&previous!=1)
		{
			pos[change++]=i;
			previous=1;
		}
	}
	int size_1,size_2,start_1,start_2,bytes1,bytes2;
	switch(change)
		{
			case 1:
				size_1=pos[0] * sizeof(char);
				size_2=(i-pos[0]) * sizeof(char);
				start_1=0;
				start_2=pos[0];
				bytes1=pos[0] * sizeof(char);
				bytes2=(i-pos[0]) * sizeof(char);
				break;
			case 3:
				size_1=(pos[1]-pos[0]) * sizeof(char);
				size_2=(i-pos[2]) * sizeof(char);
				start_1=1;
				start_2=pos[2];
				bytes1=(pos[1]-pos[0]) * sizeof(char);
				bytes2=(i-pos[2]) * sizeof(char);
				break;
			default:
				break;
		}

		char *one = malloc(size_1);
		char *two = malloc(size_2);
		memcpy(one, arg+start_1, bytes1);
		memcpy(two, arg + start_2, bytes2);

//Convertion
	int row,j;
	int n=0;
	int column = 0;
	unsigned char char_col[256];
	if(change==1)
	{
		sscanf(two,"%d",&row);
		for(j=size_1-1;j>=0;j--)
		{
			column=column+((int)(one[j]-'A')+1)*(int)pow(26,n);
			printf("%d\n",(int)(one[j]-'A'+1)*(int)pow(26,n));
			n++;
		}
	//printf("R%dC%d\n",row,column);
	}
	if(change==3)
	{

		int integer,resto;
		int k=0;
		sscanf(one,"%d",&row);
		sscanf(two,"%d",&integer);
		while(integer>0)
		{
			resto=integer%26;
			integer=(integer-resto)/26;
			char_col[k]=(char)(resto+64);
			k++;
		}
		char_col[k]='\0';
		reverse_string(char_col);
		printf("%s%d\n",char_col,row);

	}

}
void reverse_string(char *str)
{
    if (str == 0)
    {
        return;
    }

    /* skip empty string */
    if (*str == 0)
    {
        return;
    }

    /* get range */
    char *start = str;
    char *end = start + strlen(str) - 1; /* -1 for \0 */
    char temp;

    /* reverse */
    while (end > start)
    {
        /* swap */
        temp = *start;
        *start = *end;
        *end = temp;

        /* move */
        ++start;
        --end;
    }
}


/*
B. Spreadsheets
time limit per test10 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
*/

#include <stdio.h>

#define MAX_SIZE 32

const int radix = 26;

void print_column(int col)
{
	if(col--)
	{
		print_column(col / radix);
		putchar((col % radix) + 'A');
	}
}

int main(void)
{
	int coordinates;

	scanf("%d", &coordinates);

	while(coordinates--)
	{
		char numbers[MAX_SIZE];

		scanf("%s", numbers);

		//In RxCx case
		if(numbers[0] == 'R')
		{
			int raw, column;

			sscanf(numbers, "R%dC%d", &raw, &column);
			print_column(column);
			printf("%d\n", raw);
		}
		else
		{
			char *raw = numbers;
			int column;

			for(column = 0; *raw >= 'A'; ++raw)
				column = column * radix + (*raw ^ 64);

			printf("R%sC%d\n", raw, column);
		}
	}

	return 0;
}


/*
B. Spreadsheets
time limit per test10 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 32

void numeration_short_to_long(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);
void numeration_long_to_short(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);

int main(void)
{
	int coordinates;
	int i = 0;

	scanf("%d", &coordinates);
	//char convert_numbers[coordinates][MAX_SIZE];

	for(i = 0; i < coordinates; ++i)
	{
		char numbers[MAX_SIZE];
		char convert_numbers[MAX_SIZE];

		scanf("%s", numbers);

		if(numbers[0] == 'R')
			numeration_long_to_short(numbers, convert_numbers);
		else
			numeration_short_to_long(numbers, convert_numbers);

		printf("%s\n", convert_numbers);
	}

	/*for(i = 0; i < coordinates; ++i)
		printf("%s\n", convert_numbers[i]);*/

	return 0;
}

void numeration_short_to_long(char numbers[], char convert_number[])
{
	char keys[] = "1234567890";
	char pch_numbers[2][MAX_SIZE];
	int int_position = 0;
	int column = 0;
	int col_size = 0;
	int radix = 26;

	int_position = strcspn(numbers, keys);

	//extract column number
	strncpy(pch_numbers[1], numbers, int_position);
	pch_numbers[1][int_position] = '\0';

	//extract raw number
	strcpy(pch_numbers[0], &numbers[int_position]);

	col_size = strlen(pch_numbers[1]);

	int i;
	for(i = col_size-2; i >= 0; --i)
	{
		column += radix * ((int)pch_numbers[1][i] - 64);
		radix *= radix;
	}

	column += ((int)pch_numbers[1][col_size-1] - 64);
	char tmp_col[MAX_SIZE];
	sprintf(tmp_col, "%d", column);

	strcpy(convert_number, "R");
	strcat(convert_number, pch_numbers[0]);
	strcat(convert_number, "C");
	strcat(convert_number, tmp_col);
}

void numeration_long_to_short(char numbers[], char convert_number[])
{
	int column = 0;
	int col_size = 0;
	int i;
	int radix = 26;
	char *pch;
	char pch_numbers[2][MAX_SIZE];
	char tmp_col[MAX_SIZE][2];

	//extract raw number
	pch = strtok(numbers, "RC");
	strcpy(pch_numbers[0], pch);

	//extract column number
	pch = strtok(NULL, "RC");
	strcpy(pch_numbers[1], pch);

	column = atoi(pch_numbers[1]);

	int residue = 0;
	do{
		residue = column % radix;

		if(residue == 0)
		{
			sprintf(tmp_col[col_size], "%c", 'Z');
			column /= radix;
			--column;
		}
		else
		{
			sprintf(tmp_col[col_size], "%c", residue + 64);
			column /= radix;
		}

		++col_size;

	}while(column > 0);

	//reverse to get the real column
	strcpy(convert_number, tmp_col[col_size-1]);
	for(i = col_size-2; i >= 0; --i)
		strcat(convert_number, tmp_col[i]);

	strcat(convert_number, pch_numbers[0]);
}


/*
B. Spreadsheets
time limit per test10 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
*/

#include <stdio.h>

#define MAX_SIZE 32

const int radix = 26;

void print_column(int col)
{
	if(col--)
	{
		print_column(col / radix);
		putchar((col % radix) + 'A');
	}
}

int main(void)
{
	int coordinates;

	scanf("%d", &coordinates);

	while(coordinates--)
	{
		char numbers[MAX_SIZE];

		scanf("%s", numbers);

		//In RxCx case
		if(numbers[0] == 'R')
		{
			int raw, column;

			sscanf(numbers, "R%dC%d", &raw, &column);
			print_column(column);
			printf("%d\n", raw);
		}
		//In BCxx case
		else
		{
			char *raw = numbers;
			int column;

			for(column = 0; *raw >= 'A'; ++raw)
				column = column * radix + (*raw ^ 64);

			printf("R%sC%d\n", raw, column);
		}
	}

	return 0;
}


/*
B. Spreadsheets
time limit per test10 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 32

void numeration_short_to_long(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);
void numeration_long_to_short(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);

int main(void)
{
	int coordinates;
	int i = 0;

	scanf("%d", &coordinates);
	char convert_numbers[coordinates][MAX_SIZE];

	for(i = 0; i < coordinates; ++i)
	{
		char numbers[MAX_SIZE];

		scanf("%s", numbers);

		if(numbers[0] == 'R')
			numeration_long_to_short(numbers, convert_numbers[i]);
		else
			numeration_short_to_long(numbers, convert_numbers[i]);
	}

	for(i = 0; i < coordinates; ++i)
		printf("%s\n", convert_numbers[i]);

	return 0;
}

void numeration_short_to_long(char numbers[], char convert_number[])
{
	char keys[] = "1234567890";
	char pch_numbers[2][MAX_SIZE];
	int int_position = 0;
	int column = 0;
	int col_size = 0;
	int radix = 26;

	int_position = strcspn(numbers, keys);

	//extract column number
	strncpy(pch_numbers[1], numbers, int_position);
	pch_numbers[1][int_position] = '\0';

	//extract raw number
	strcpy(pch_numbers[0], &numbers[int_position]);

	col_size = strlen(pch_numbers[1]);

	int i;
	for(i = col_size-2; i >= 0; --i)
	{
		column += radix * ((int)pch_numbers[1][i] - 64);
		radix *= radix;
	}

	column += ((int)pch_numbers[1][col_size-1] - 64);
	char tmp_col[MAX_SIZE];
	sprintf(tmp_col, "%d", column);

	strcpy(convert_number, "R");
	strcat(convert_number, pch_numbers[0]);
	strcat(convert_number, "C");
	strcat(convert_number, tmp_col);
}

void numeration_long_to_short(char numbers[], char convert_number[])
{
	int column = 0;
	int col_size = 0;
	int i;
	int radix = 26;
	char *pch;
	char pch_numbers[2][MAX_SIZE];
	char tmp_col[MAX_SIZE][2];

	//extract raw number
	pch = strtok(numbers, "RC");
	strcpy(pch_numbers[0], pch);

	//extract column number
	pch = strtok(NULL, "RC");
	strcpy(pch_numbers[1], pch);

	column = atoi(pch_numbers[1]);

	int residue = 0;
	do{
		residue = column % radix;

		if(residue == 0)
		{
			sprintf(tmp_col[col_size], "%c", 'Z');
			column /= radix;
			--column;
		}
		else
		{
			sprintf(tmp_col[col_size], "%c", residue + 64);
			column /= radix;
		}

		++col_size;

	}while(column / radix > radix);

	if(column % radix == 0)
		sprintf(tmp_col[col_size++], "%c", 'Z');
	else
	{
		if(column / radix == 0)
			sprintf(tmp_col[col_size++], "%c", column + 64);
		else
			sprintf(tmp_col[col_size++], "%c", (column/radix) + 64);
	}

	//reverse to get the real column
	strcpy(convert_number, tmp_col[col_size-1]);
	for(i = col_size-2; i >= 0; --i)
		strcat(convert_number, tmp_col[i]);

	strcat(convert_number, pch_numbers[0]);
}


/*
B. Spreadsheets
time limit per test10 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 32

void numeration_short_to_long(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);
void numeration_long_to_short(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);

int main(void)
{
	int coordinates;
	int i = 0;

	scanf("%d", &coordinates);
	char convert_numbers[coordinates][MAX_SIZE];

	for(i = 0; i < coordinates; ++i)
	{
		char numbers[MAX_SIZE];

		scanf("%s", numbers);

		if(numbers[0] == 'R')
			numeration_long_to_short(numbers, convert_numbers[i]);
		else
			numeration_short_to_long(numbers, convert_numbers[i]);
	}

	for(i = 0; i < coordinates; ++i)
		printf("%s\n", convert_numbers[i]);

	return 0;
}

void numeration_short_to_long(char numbers[], char convert_number[])
{
	char keys[] = "1234567890";
	char pch_numbers[2][MAX_SIZE];
	int int_position = 0;
	int column = 0;
	int col_size = 0;
	int radix = 26;

	int_position = strcspn(numbers, keys);

	//extract column number
	strncpy(pch_numbers[1], numbers, int_position);
	pch_numbers[1][int_position] = '\0';

	//extract raw number
	strcpy(pch_numbers[0], &numbers[int_position]);

	col_size = strlen(pch_numbers[1]);

	int i;
	for(i = col_size-2; i >= 0; --i)
	{
		column += radix * ((int)pch_numbers[1][i] - 64);
		radix *= radix;
	}

	column += ((int)pch_numbers[1][col_size-1] - 64);
	char tmp_col[MAX_SIZE];
	sprintf(tmp_col, "%d", column);

	strcpy(convert_number, "R");
	strcat(convert_number, pch_numbers[0]);
	strcat(convert_number, "C");
	strcat(convert_number, tmp_col);
}

void numeration_long_to_short(char numbers[], char convert_number[])
{
	int column = 0;
	int col_size = 0;
	int i;
	int radix = 26;
	char *pch;
	char pch_numbers[2][MAX_SIZE];
	char tmp_col[MAX_SIZE][2];

	//extract raw number
	pch = strtok(numbers, "RC");
	strcpy(pch_numbers[0], pch);

	//extract column number
	pch = strtok(NULL, "RC");
	strcpy(pch_numbers[1], pch);

	column = atoi(pch_numbers[1]);

	int residue = 0;
	do{
		residue = column % radix;

		if(residue == 0)
		{
			sprintf(tmp_col[col_size], "%c", 'Z');
			column /= radix;
			--column;
		}
		else
		{
			sprintf(tmp_col[col_size], "%c", residue + 64);
			column /= radix;
		}

		++col_size;

	}while(column > 0);
	//while(column / radix > radix);

	/*if(column % radix == 0)
		sprintf(tmp_col[col_size++], "%c", 'Z');
	else
	{
		if(column / radix == 0)
			sprintf(tmp_col[col_size++], "%c", column + 64);
		else
			sprintf(tmp_col[col_size++], "%c", (column/radix) + 64);
	}*/

	//reverse to get the real column
	strcpy(convert_number, tmp_col[col_size-1]);
	for(i = col_size-2; i >= 0; --i)
		strcat(convert_number, tmp_col[i]);

	strcat(convert_number, pch_numbers[0]);
}


/*
B. Spreadsheets
time limit per test10 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 32

void numeration_short_to_long(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);
void numeration_long_to_short(char numbers[MAX_SIZE], char convert_number[MAX_SIZE]);

char const convert_table[26][2] = \
{
	"Z\0", "A\0", "B\0", "C\0", "D\0", "E\0", "F\0",
	"G\0", "H\0", "I\0", "J\0", "K\0", "L\0",
	"M\0", "N\0", "O\0", "P\0", "Q\0", "R\0",
	"S\0", "T\0", "U\0", "V\0", "W\0", "X\0", "Y\0"
};

int main(void)
{
	int coordinates;
	int i = 0;

	scanf("%d", &coordinates);
	char convert_numbers[coordinates][MAX_SIZE];

	for(i = 0; i < coordinates; ++i)
	{
		char numbers[MAX_SIZE];

		scanf("%s", numbers);

		if(numbers[0] == 'R')
			numeration_long_to_short(numbers, convert_numbers[i]);
		else
			numeration_short_to_long(numbers, convert_numbers[i]);
	}

	for(i = 0; i < coordinates; ++i)
		printf("%s\n", convert_numbers[i]);

	return 0;
}

void numeration_short_to_long(char numbers[], char convert_number[])
{
	char keys[] = "1234567890";
	char pch_numbers[2][MAX_SIZE];
	int int_position = 0;
	int column = 0;
	int col_size = 0;
	int i;
	int radix = 26;

	int_position = strcspn(numbers, keys);

	//extract column number
	strncpy(pch_numbers[1], numbers, int_position);
	pch_numbers[1][int_position] = '\0';

	//extract raw number
	strcpy(pch_numbers[0], &numbers[int_position]);

	col_size = strlen(pch_numbers[1]);

	for(i = col_size-2; i >= 0; --i)
	{
		column += radix * ((int)pch_numbers[1][i] - 64);
		radix *= radix;
	}

	column += ((int)pch_numbers[1][col_size-1] - 64);
	char tmp_col[MAX_SIZE];
	sprintf(tmp_col, "%d", column);

	strcpy(convert_number, "R");
	strcat(convert_number, pch_numbers[0]);
	strcat(convert_number, "C");
	strcat(convert_number, tmp_col);
}

void numeration_long_to_short(char numbers[], char convert_number[])
{
	int column = 0;
	int col_size = 0;
	int i;
	int radix = 26;
	char *pch;
	char pch_numbers[2][MAX_SIZE];
	char tmp_col[MAX_SIZE][2];

	//extract raw number
	pch = strtok(numbers, "RC");
	strcpy(pch_numbers[0], pch);

	//extract column number
	pch = strtok(NULL, "RC");
	strcpy(pch_numbers[1], pch);

	column = atoi(pch_numbers[1]);
	col_size = strlen(pch_numbers[1]);

	for(i = 0; i < col_size-1; ++i)
	{
		strcpy(tmp_col[i], convert_table[(column % radix)]);
		column /= radix;

		if(!strcmp(tmp_col[i], "Z"))
			--column;
	}

	//reverse to get the real column
	strcpy(convert_number, tmp_col[col_size-1]);
	for(i = col_size-2; i >= 0; --i)
		strcat(convert_number, tmp_col[i]);

	strcat(convert_number, pch_numbers[0]);
}