using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// using System.Numerics;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using ll=System.Int64;
using static Contest_E.Lib_IO;
using static Contest_E.Lib_Minifunc;



public static class Contest_E
{

	public static void Main() {
		checked{
			int d,m;
			Lib_IO.rm(out d);
			int[] c,x;
			ra(out c);
			int[][] s;
			rx(d,out s);
			rl(d,out x);
			rm(out m);
			P<int,int>[] dq;
			rl(m,out dq); 

			//BinaryIndexedTree[] dis1 = new BinaryIndexedTree[26];
			Set<int>[] dis2a = new Set<int>[26];
			Set<int>[] dis2d = new Set<int>[26];
			for (int i = 0; i < 26; i++)
			{
				//dis1[i] = new BinaryIndexedTree(d);
				dis2a[i] = new Set<int>();
				dis2d[i] = new Set<int>((x,y)=>y.CompareTo(x));
			}

			Func<int[],int> score = (x)=>{
				int sc = 0;
				for (int i = 0; i < 26; i++)
				{
					//dis1[i] = new BinaryIndexedTree(d);
					dis2a[i] = new Set<int>();
					dis2d[i] = new Set<int>((x,y)=>y.CompareTo(x));
				}
				int[] la = new int[26];
				for (int i = 0; i < d; i++)
				{
					sc += s[i][x[i]];
					la[x[i]] = i+1;
					for (int j = 0; j < la.Length; j++)
					{
						sc -= c[j]*(i+1-la[j]);
					}
					//dis1[x[i]].Add(i,1);
					dis2a[x[i]].Add(i);
					dis2d[x[i]].Add(i);
				}
				return sc;
			};

			Func<int[],int,int,int> change = (x,dt,nv)=>{
				int ov = x[dt];
				int sc = s[dt][nv] - s[dt][ov];
				int t1 = dis2d[ov].LowerBound(dt-1);
				int t2 = dis2d[nv].LowerBound(dt);
				int t3 = dis2a[ov].LowerBound(dt+1);
				int t4 = dis2a[nv].LowerBound(dt);
				int ol = dt-((t1==dis2d[ov].Count)?-1:dis2d[ov][t1]);
				int nl = dt-((t2==dis2d[nv].Count)?-1:dis2d[nv][t2]);
				int on = ((t3==dis2a[ov].Count)?d:dis2a[ov][t3])-dt;
				int nn = ((t4==dis2a[nv].Count)?d:dis2a[nv][t4])-dt;
				sc += c[nv]*(nn*nl)-c[ov]*(on*ol);
				return sc;
			};
/*
			Func<int[],int,int,int> change1 = (x,dt,nv)=>{
				int ov = x[dt];
				int sc = s[dt][nv] - s[dt][ov];
				int ol = dt-BS1(dis1[ov],dis1[ov].Sum(dt)-2,d);
				int nl = dt-BS1(dis1[nv],dis1[nv].Sum(dt)-1,d);
				if(dis1[ov].Sum(dt)==1) ol++;
				if(dis1[nv].Sum(dt)==0) nl++;
				int on = BS1(dis1[ov],dis1[ov].Sum(dt),d)-dt;
				int nn = BS1(dis1[nv],dis1[nv].Sum(dt),d)-dt;
				sc += c[nv]*(nn*nl)-c[ov]*(on*ol);
				return sc;
			};
*/
			Func<int[],int,int,int> change2 = (x,dt,nv)=>{
				int ov = x[dt];
				int sc = s[dt][nv] - s[dt][ov];
				int ol = dt+1;
				int nl = dt+1;
				int on = d-dt;
				int nn = d-dt;
				for (int i = dt-1; 0<=i; i--)
				{
					if(x[i]==ov){
						ol = dt-i;
						break;
					}
				}
				for (int i = dt-1; 0<=i; i--)
				{
					if(x[i]==nv){
						nl = dt-i;
						break;
					}
				}
				for (int i = dt+1; i<d; i++)
				{
					if(x[i]==ov){
						on = i-dt;
						break;
					}
				}
				for (int i = dt+1; i<d; i++)
				{
					if(x[i]==nv){
						nn = i-dt;
						break;
					}
				}
				sc += c[nv]*(nn*nl)-c[ov]*(on*ol);
				return sc;
			};

			for (int i = 0; i < d; i++)
			{
				x[i] = x[i]-1;
			}

			List<ll> ans = new List<ll>();
			int sc = score(x);
			foreach (var e in dq)
			{
				//int dummy = change2(x,e.A-1,e.B-1);
				int dc = change(x,e.A-1,e.B-1);
				sc += dc;
				//dis1[x[e.A-1]].Add(e.A-1,-1);
				dis2a[x[e.A-1]].Remove(e.A-1);
				dis2d[x[e.A-1]].Remove(e.A-1);
				x[e.A-1] = e.B-1;
				//dis1[x[e.A-1]].Add(e.A-1,1);
				dis2a[x[e.A-1]].Add(e.A-1);
				dis2d[x[e.A-1]].Add(e.A-1);
				ans.Add(sc);
			}


			Lib_IO.wl(ans);
		}
	}

	public static int BS1<T>(BinaryIndexedTree seq, T value, int count)  where T : IComparable<T> {
		int left = -1;
		int right = count;
		while (right - left > 1) {
			int mid = left + (right - left) / 2;
			if (0<seq.Sum(mid).CompareTo(value)) right = mid;
			else left = mid;
		}
		return right;
	}

	public class BinaryIndexedTree {
		private int[] data;

		public BinaryIndexedTree(int sz) { data = new int[++sz]; }
		public int Sum(int k) {
			int ret = 0;
			for(++k; k > 0; k -= k & -k) ret += data[k];
			return (ret);
		}
		public void Add(int k, int x) { for(++k; k < data.Length; k += k & -k) data[k] += x; }
	}


public class Set<T> {
    Node root;
    readonly IComparer<T> comparer;
    readonly Node nil;
    public bool IsMultiSet { get; set; }
    public Set(IComparer<T> comparer) {
        nil = new Node(default(T));
        root = nil;
        this.comparer = comparer;
    }
    public Set(Comparison<T> comaprison) : this(Comparer<T>.Create(comaprison)) { }
    public Set() : this(Comparer<T>.Default) { }
    public bool Add(T v) {
        return insert(ref root, v);
    }
    public bool Remove(T v) {
        return remove(ref root, v);
    }
    public T this[int index] { get { return find(root, index); } }
    public int Count { get { return root.Count; } }
    public void RemoveAt(int k) {
        if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
        removeAt(ref root, k);
    }
    public T[] Items {
        get {
            var ret = new T[root.Count];
            var k = 0;
            walk(root, ret, ref k);
            return ret;
        }
    }
    void walk(Node t, T[] a, ref int k) {
        if (t.Count == 0) return;
        walk(t.lst, a, ref k);
        a[k++] = t.Key;
        walk(t.rst, a, ref k);
    }

    bool insert(ref Node t, T key) {
        if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
        var cmp = comparer.Compare(t.Key, key);
        bool res;
        if (cmp > 0)
            res = insert(ref t.lst, key);
        else if (cmp == 0)
        {
            if (IsMultiSet) res = insert(ref t.lst, key);
            else return false;
        }
        else res = insert(ref t.rst, key);
        balance(ref t);
        return res;
    }
    bool remove(ref Node t, T key) {
        if (t.Count == 0) return false;
        var cmp = comparer.Compare(key, t.Key);
        bool ret;
        if (cmp < 0) ret = remove(ref t.lst, key);
        else if (cmp > 0) ret = remove(ref t.rst, key);
        else
        {
            ret = true;
            var k = t.lst.Count;
            if (k == 0) { t = t.rst; return true; }
            if (t.rst.Count == 0) { t = t.lst; return true; }


            t.Key = find(t.lst, k - 1);
            removeAt(ref t.lst, k - 1);
        }
        balance(ref t);
        return ret;
    }
    void removeAt(ref Node t, int k) {
        var cnt = t.lst.Count;
        if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
        else if (cnt > k) removeAt(ref t.lst, k);
        else
        {
            if (cnt == 0) { t = t.rst; return; }
            if (t.rst.Count == 0) { t = t.lst; return; }

            t.Key = find(t.lst, k - 1);
            removeAt(ref t.lst, k - 1);
        }
        balance(ref t);
    }
    void balance(ref Node t) {
        var balance = t.lst.Height - t.rst.Height;
        if (balance == -2)
        {
            if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
            rotL(ref t);
        }
        else if (balance == 2)
        {
            if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
            rotR(ref t);
        }
        else t.Update();
    }

    T find(Node t, int k) {
        if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
        for (; ; )
        {
            if (k == t.lst.Count) return t.Key;
            else if (k < t.lst.Count) t = t.lst;
            else { k -= t.lst.Count + 1; t = t.rst; }
        }
    }
    public int LowerBound(T v) {
        var k = 0;
        var t = root;
        for (; ; )
        {
            if (t.Count == 0) return k;
            if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
            else { k += t.lst.Count + 1; t = t.rst; }
        }
    }
    public int UpperBound(T v) {
        var k = 0;
        var t = root;
        for (; ; )
        {
            if (t.Count == 0) return k;
            if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
            else t = t.lst;
        }
    }

    void rotR(ref Node t) {
        var l = t.lst;
        t.lst = l.rst;
        l.rst = t;
        t.Update();
        l.Update();
        t = l;
    }
    void rotL(ref Node t) {
        var r = t.rst;
        t.rst = r.lst;
        r.lst = t;
        t.Update();
        r.Update();
        t = r;
    }


    class Node {
        public Node(T key) {
            Key = key;
        }
        public int Count { get; private set; }
        public sbyte Height { get; private set; }
        public T Key { get; set; }
        public Node lst, rst;
        public void Update() {
            Count = 1 + lst.Count + rst.Count;
            Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
        }
        public override string ToString() {
            return string.Format("Count = {0}, Key = {1}", Count, Key);
        }
    }
}














#region BaseModule


public static class Lib_Minifunc{
	[MethodImpl(256)] public static void swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	[MethodImpl(256)] public static void swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) swap1(ref a, ref b); }  // b の方が小さければ交換
	[MethodImpl(256)] public static bool chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	[MethodImpl(256)] public static bool chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	[MethodImpl(256)] public static bool inside(long i, long n) => (0<=i&&i<n);
	[MethodImpl(256)] public static bool inside(long x, long y, long w, long h) => (inside(x,w)&&inside(y,h));
	[MethodImpl(256)] public static T min<T>(params T[] a) where T : IComparable => a.Min();
	[MethodImpl(256)] public static T max<T>(params T[] a) where T : IComparable => a.Max();
	[MethodImpl(256)] public static long mod(long a, long m=MOD1) { var v = a%m; return (v<0?v+m:v); }
	[MethodImpl(256)] public static long ceiling(long a, long b) => (a%b==0?a/b:a/b+1);	// 整数商の切り上げ
	[MethodImpl(256)] public static P<T,U> initp<T,U>(T a,U b) => new P<T,U>(a,b);
	[MethodImpl(256)] public static P<T,U,V> initp<T,U,V>(T a,U b,V c) => new P<T,U,V>(a,b,c);
	[MethodImpl(256)] public static P<T,U,V,W> initp<T,U,V,W>(T a,U b,V c,W d) => new P<T,U,V,W>(a,b,c,d);
	[MethodImpl(256)] public static bool initd<T,U>(Dictionary<T,U> d, T k) { if(d.ContainsKey(k)) { return false; } else { d[k] = default(U); return true; } }
	public static T[] inita<T>(long len1) where T : new() { var m = new T[len1]; for (int i=0;i<m.Length;i++) m[i] = new T(); return m; }
	public static T[][] initm<T>(long len1,long len2,T val) where T : struct { var m = new T[len1][]; for (int i=0;i<m.Length;i++) m[i] = Enumerable.Repeat(val,(int)len2).ToArray(); return m; }
	public static T[][][] initm<T>(long len1,long len2,long len3,T val) where T : struct { var m = new T[len1][][]; for (int i=0;i<m.Length;i++) m[i] = initm(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;
	public const long INF1 = 1000000000000000;  // 10^15
}

public struct P<T,U>
{
	public T A; public U B;
	public P(T a,U b) { A = a; B = b; }
	public static implicit operator KeyValuePair<T,U>(P<T,U> a) => new KeyValuePair<T,U>(a.A,a.B);
	public static implicit operator P<T,U>(KeyValuePair<T,U> a) => new P<T,U>(a.Key,a.Value);
}
public struct P<T,U,V>
{
	public T A; public U B; public V C;
	public P(T a,U b,V c) { A = a; B = b; C = c; }
}
public struct P<T,U,V,W>
{
	public T A; public U B; public V C; public W D;
	public P(T a,U b,V c,W d) { A = a; B = b; C = c; D = d; }
}

public static class Lib_IO
{
	class Prt : System.IO.StreamWriter
	{
		public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
		public Prt(System.IO.Stream s) : base(s,new UTF8Encoding(false,true)) {}
		public Prt(System.IO.Stream s,Encoding e) : base(s,e) {}
	}
	static Prt sw = new Prt(Console.OpenStandardOutput());
	static char[] sp = new char[] {' '};
	[MethodImpl(256)] static bool eq<T,U>() => typeof(T).Equals(typeof(U));
	[MethodImpl(256)] static T ct<T,U>(U a) => (T)Convert.ChangeType(a,typeof(T));
	[MethodImpl(256)] static T cv<T>(string s) =>
		eq<T,int>()	? ct<T,int>(int.Parse(s))
	  : eq<T,long>()   ? ct<T,long>(long.Parse(s))
	  : eq<T,double>() ? ct<T,double>(double.Parse(s,System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T,char>()   ? ct<T,char>(s[0])
					   : ct<T,string>(s);
	public static string[] rm<T>(out T a) {
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(z[0]);
		return z;
	}
	public static string[] rm<T,U>(out T a,out U b) {
		var z = rm<T>(out a); 
		b = cv<U>(z[1]);
		return z;
	}
	public static string[] rm<T,U,V>(out T a,out U b,out V c) {
		var z = rm<T,U>(out a,out b); 
		c = cv<V>(z[2]);
		return z;
	}
	public static string[] rm<T,U,V,W>(out T a,out U b,out V c,out W d) {
		var z = rm<T,U,V>(out a,out b,out c); 
		d = cv<W>(z[3]);
		return z;
	}
	public static string[] rm<T,U,V,W,X>(out T a,out U b,out V c,out W d,out X e) {
		var z = rm<T,U,V,W>(out a,out b,out c,out d); 
		e = cv<X>(z[4]);
		return z;
	}
	public static string[] rm<T,U,V,W,X,Y>(out T a,out U b,out V c,out W d,out X e,out Y f) {
		var z = rm<T,U,V,W,X>(out a,out b,out c,out d,out e); 
		f = cv<Y>(z[5]);
		return z;
	}
	public static string[] ra<T>(out T[] a) {		
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries);
		a = z.Select(cv<T>).ToArray();
		return z;
	}
	public static string[][] rl<T>(long n,out T[] a) {
		a = new T[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = rm(out a[i]);
		return y;
	}
	public static string[][] rl<T,U>(long n,out P<T,U>[] a) {
		a = new P<T,U>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p;
			y[i] = rm(out o,out p);
			a[i] = new P<T,U>(o,p);
		}
		return y;
	}
	public static string[][] rl<T,U,V>(long n,out P<T,U,V>[] a) {
		a = new P<T,U,V>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q;
			y[i] = rm(out o,out p,out q);
			a[i] = new P<T,U,V>(o,p,q);
		}
		return y;
	}
	public static string[][] rl<T,U,V,W>(long n,out P<T,U,V,W>[] a) {
		a = new P<T,U,V,W>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q; W r;
			y[i] = rm(out o,out p,out q,out r);
			a[i] = new P<T,U,V,W>(o,p,q,r);
		}
		return y;
	}
	public static string[][] rx<T>(long n,out T[][] a) {
		a = new T[n][];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = ra(out a[i]);
		return y;
	}

	static void wwp(Action act){
		sw.AutoFlush = false;
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	[MethodImpl(256)] static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void wm(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length-1;i++) Console.Write(wfm(a[i].GetType())+" ",a[i]);
			Console.WriteLine(wfm(a[a.Length-1].GetType()),a[a.Length-1]);
		});
	}
	public static void wa<T>(IList<T> a) { wxa(new IList<T>[]{a}, " "); }
	public static void wl<T>(IList<T> a) {
		wwp(()=>{
			var f = wfm(typeof(T));
			foreach(T x in a) Console.WriteLine(f,x);
		});
	}
	static void wxa<T>(IList<IList<T>> a, string s) {
		wwp(()=>{
			var f = wfm(typeof(T));
			var g = f + s;
			foreach(var b in a) {
				for(int i=0;i<b.Count-1;i++) Console.Write(g,b[i]);
				Console.WriteLine(f,b[b.Count-1]);
			}
		});
	}
	public static void wx<T>(IList<IList<T>> a) { wxa(a, " "); }
	public static void wg<T>(IList<IList<T>> a) { wxa(a, ""); }
}

#endregion



}



Measured memory_bytes: 58596