// Dan Deblasio
// 7/20/07

// Used for template for 2008 BHCSI Assignment: Linked List Addition

import java.io.*;
import java.util.*;


public class LinkListAdd {

	public static void main(String[] args) {
		
		try{
			//prompt the user for the filename
			System.out.println("Please Input the filename:");
			Scanner cin = new Scanner(System.in);
			String fname = cin.next();
			
			//open a scanner to the input file, can throw and exception caught below
			Scanner fin = new Scanner(new File(fname));
			
			int n = fin.nextInt();	//number of instances
			for(int i=0;i<n;i++){
				BHCSIBigInt num1 = new BHCSIBigInt(fin.next().toCharArray());
				BHCSIBigInt num2 = new BHCSIBigInt(fin.next().toCharArray());
				BHCSIBigInt res = num1.add(num2);
				
				//can be printed simple because we overrode toString
				System.out.println("Addition #"+ (i+1) + ": " + num1 + " + " + num2 + " = " + res);
			}
		
		}catch(IOException e){
			// catch file not found exception and print it to the screen
			System.out.println(e.toString());
		}
	}//end main

}//end LinkListAdd

class BHCSIBigInt{
	
	private node end;	//used to point to the front (least sig. digit) of the linked list
	
	public BHCSIBigInt(char[] in){
		
		// Initialize end to null.
		end = null;
		
		for(int i=0;i<in.length;i++){
			
			//gets the integer values of the char and 0, then gets the integer difference
			node temp = new node((int)in[i]-(int)'0');
			
			//advance the pointer
			temp.next = end;
			
			end = temp;
		}
	}//end scanner constructor
	
	public BHCSIBigInt(node in){
		end = in;
	}//end node constructor
	
	/**** FILL IN THIS METHOD - Some comments are given to you to help you out. ****/
	public BHCSIBigInt add(BHCSIBigInt in){
		
		node num1 = in.end;		//the other number
		node num2 = this.end;		//this numbers linked list
		int carry = 0;			//used for calculating the carry value from one digit to the next
		node front=null, back=null; // pointers for the front and back of the result
		
		//keep looping while either of the numbers has not reached 
		//the end (most significant digit)
		while(num1!=null || num2!=null){
			
			// This is when we are only adding in the digit from num2.
			if(num1==null){
				/*** FILL THIS IN ***/
			}
			
			// This is when we are only adding in the digit from num1
			else if(num2==null){
				/*** FILL THIS IN ***/
			}
			
			// Here we are adding both the corresponding digits from num1 and num2.
			else{
				
				/*** FILL THIS IN ***/
			}
		}
		
		// This takes care of the last carry, if necessary.
		if(carry!=0){
			/*** FILL THIS IN ***/	
		}
		
		//convert the LL to a BHCSIBigInt and return it
		return new BHCSIBigInt(front);
		
	}//end add
	
	//return the string representation of the LL
	public String toString(){
		node front = end;	//working pointer
		String rtn = "";	//used as the return value
		
			//append each digit in reverse order
			while(front!=null){
				rtn = front.val + rtn;
				front = front.next;
			}
			
			return rtn;
	}//end toString
	
}//end BHCSIBigInt
	
class node{
	
		public int val;
		public node next;
		
		public node(int i){
			val = i;
			next = null;
		}//end constructor
		
}//end node

